SimpleCharacterReaderFactory.php 3.2 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 = [];
  22. /**
  23. * Factories which have already been loaded.
  24. *
  25. * @var Swift_CharacterReaderFactory[]
  26. */
  27. private static $loaded = [];
  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 = [
  46. 'class' => $prefix.'GenericFixedWidthReader',
  47. 'constructor' => [1],
  48. ];
  49. $doubleByte = [
  50. 'class' => $prefix.'GenericFixedWidthReader',
  51. 'constructor' => [2],
  52. ];
  53. $fourBytes = [
  54. 'class' => $prefix.'GenericFixedWidthReader',
  55. 'constructor' => [4],
  56. ];
  57. // Utf-8
  58. self::$map['utf-?8'] = [
  59. 'class' => $prefix.'Utf8Reader',
  60. 'constructor' => [],
  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 = strtolower(trim($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. }