|
1 <?php |
|
2 |
|
3 /* |
|
4 * This file is part of SwiftMailer. |
|
5 * (c) 2004-2009 Chris Corbyn |
|
6 * |
|
7 * For the full copyright and license information, please view the LICENSE |
|
8 * file that was distributed with this source code. |
|
9 */ |
|
10 |
|
11 |
|
12 /** |
|
13 * Standard factory for creating CharacterReaders. |
|
14 * @package Swift |
|
15 * @subpackage Encoder |
|
16 * @author Chris Corbyn |
|
17 */ |
|
18 class Swift_CharacterReaderFactory_SimpleCharacterReaderFactory |
|
19 implements Swift_CharacterReaderFactory |
|
20 { |
|
21 |
|
22 /** |
|
23 * A map of charset patterns to their implementation classes. |
|
24 * @var array |
|
25 * @access private |
|
26 */ |
|
27 private $_map = array(); |
|
28 |
|
29 /** |
|
30 * Factories which have already been loaded. |
|
31 * @var Swift_CharacterReaderFactory[] |
|
32 * @access private |
|
33 */ |
|
34 private $_loaded = array(); |
|
35 |
|
36 /** |
|
37 * Creates a new CharacterReaderFactory. |
|
38 */ |
|
39 public function __construct() |
|
40 { |
|
41 $prefix = 'Swift_CharacterReader_'; |
|
42 |
|
43 $singleByte = array( |
|
44 'class' => $prefix . 'GenericFixedWidthReader', |
|
45 'constructor' => array(1) |
|
46 ); |
|
47 |
|
48 $doubleByte = array( |
|
49 'class' => $prefix . 'GenericFixedWidthReader', |
|
50 'constructor' => array(2) |
|
51 ); |
|
52 |
|
53 $fourBytes = array( |
|
54 'class' => $prefix . 'GenericFixedWidthReader', |
|
55 'constructor' => array(4) |
|
56 ); |
|
57 |
|
58 //Utf-8 |
|
59 $this->_map['utf-?8'] = array( |
|
60 'class' => $prefix . 'Utf8Reader', |
|
61 'constructor' => array() |
|
62 ); |
|
63 |
|
64 //7-8 bit charsets |
|
65 $this->_map['(us-)?ascii'] = $singleByte; |
|
66 $this->_map['(iso|iec)-?8859-?[0-9]+'] = $singleByte; |
|
67 $this->_map['windows-?125[0-9]'] = $singleByte; |
|
68 $this->_map['cp-?[0-9]+'] = $singleByte; |
|
69 $this->_map['ansi'] = $singleByte; |
|
70 $this->_map['macintosh'] = $singleByte; |
|
71 $this->_map['koi-?7'] = $singleByte; |
|
72 $this->_map['koi-?8-?.+'] = $singleByte; |
|
73 $this->_map['mik'] = $singleByte; |
|
74 $this->_map['(cork|t1)'] = $singleByte; |
|
75 $this->_map['v?iscii'] = $singleByte; |
|
76 |
|
77 //16 bits |
|
78 $this->_map['(ucs-?2|utf-?16)'] = $doubleByte; |
|
79 |
|
80 //32 bits |
|
81 $this->_map['(ucs-?4|utf-?32)'] = $fourBytes; |
|
82 |
|
83 //Fallback |
|
84 $this->_map['.*'] = $singleByte; |
|
85 } |
|
86 |
|
87 /** |
|
88 * Returns a CharacterReader suitable for the charset applied. |
|
89 * @param string $charset |
|
90 * @return Swift_CharacterReader |
|
91 */ |
|
92 public function getReaderFor($charset) |
|
93 { |
|
94 $charset = trim(strtolower($charset)); |
|
95 foreach ($this->_map as $pattern => $spec) |
|
96 { |
|
97 $re = '/^' . $pattern . '$/D'; |
|
98 if (preg_match($re, $charset)) |
|
99 { |
|
100 if (!array_key_exists($pattern, $this->_loaded)) |
|
101 { |
|
102 $reflector = new ReflectionClass($spec['class']); |
|
103 if ($reflector->getConstructor()) |
|
104 { |
|
105 $reader = $reflector->newInstanceArgs($spec['constructor']); |
|
106 } |
|
107 else |
|
108 { |
|
109 $reader = $reflector->newInstance(); |
|
110 } |
|
111 $this->_loaded[$pattern] = $reader; |
|
112 } |
|
113 return $this->_loaded[$pattern]; |
|
114 } |
|
115 } |
|
116 } |
|
117 |
|
118 } |