SimpleCharacterReaderFactory.php 3.1KB

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