TYPO3  7.6
SimpleCharacterReaderFactory.php
Go to the documentation of this file.
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 
17 {
23  private static $_map = array();
24 
30  private static $_loaded = array();
31 
35  public function __construct()
36  {
37  $this->init();
38  }
39 
40  public function __wakeup()
41  {
42  $this->init();
43  }
44 
45  public function init()
46  {
47  if (count(self::$_map) > 0) {
48  return;
49  }
50 
51  $prefix = 'Swift_CharacterReader_';
52 
53  $singleByte = array(
54  'class' => $prefix.'GenericFixedWidthReader',
55  'constructor' => array(1),
56  );
57 
58  $doubleByte = array(
59  'class' => $prefix.'GenericFixedWidthReader',
60  'constructor' => array(2),
61  );
62 
63  $fourBytes = array(
64  'class' => $prefix.'GenericFixedWidthReader',
65  'constructor' => array(4),
66  );
67 
68  // Utf-8
69  self::$_map['utf-?8'] = array(
70  'class' => $prefix.'Utf8Reader',
71  'constructor' => array(),
72  );
73 
74  //7-8 bit charsets
75  self::$_map['(us-)?ascii'] = $singleByte;
76  self::$_map['(iso|iec)-?8859-?[0-9]+'] = $singleByte;
77  self::$_map['windows-?125[0-9]'] = $singleByte;
78  self::$_map['cp-?[0-9]+'] = $singleByte;
79  self::$_map['ansi'] = $singleByte;
80  self::$_map['macintosh'] = $singleByte;
81  self::$_map['koi-?7'] = $singleByte;
82  self::$_map['koi-?8-?.+'] = $singleByte;
83  self::$_map['mik'] = $singleByte;
84  self::$_map['(cork|t1)'] = $singleByte;
85  self::$_map['v?iscii'] = $singleByte;
86 
87  //16 bits
88  self::$_map['(ucs-?2|utf-?16)'] = $doubleByte;
89 
90  //32 bits
91  self::$_map['(ucs-?4|utf-?32)'] = $fourBytes;
92 
93  // Fallback
94  self::$_map['.*'] = $singleByte;
95  }
96 
104  public function getReaderFor($charset)
105  {
106  $charset = trim(strtolower($charset));
107  foreach (self::$_map as $pattern => $spec) {
108  $re = '/^'.$pattern.'$/D';
109  if (preg_match($re, $charset)) {
110  if (!array_key_exists($pattern, self::$_loaded)) {
111  $reflector = new ReflectionClass($spec['class']);
112  if ($reflector->getConstructor()) {
113  $reader = $reflector->newInstanceArgs($spec['constructor']);
114  } else {
115  $reader = $reflector->newInstance();
116  }
117  self::$_loaded[$pattern] = $reader;
118  }
119 
120  return self::$_loaded[$pattern];
121  }
122  }
123  }
124 }