SimpleCharacterReaderFactory.php 3.3 KB

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