PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/email/vendor/swift/classes/Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 129 lines | 66 code | 16 blank | 47 comment | 3 complexity | fc2910fc9a93a0eb1b0c98a1aa460ce8 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. /*
  3. The standard factory for creating CharacterReaders in Swift Mailer.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. //@require 'Swift/CharacterReaderFactory.php';
  16. /**
  17. * Standard factory for creating CharacterReaders.
  18. * @package Swift
  19. * @subpackage Encoder
  20. * @author Chris Corbyn
  21. */
  22. class Swift_CharacterReaderFactory_SimpleCharacterReaderFactory
  23. implements Swift_CharacterReaderFactory
  24. {
  25. /**
  26. * A map of charset patterns to their implementation classes.
  27. * @var array
  28. * @access private
  29. */
  30. private $_map = array();
  31. /**
  32. * Factories which have already been loaded.
  33. * @var Swift_CharacterReaderFactory[]
  34. * @access private
  35. */
  36. private $_loaded = array();
  37. /**
  38. * Creates a new CharacterReaderFactory.
  39. */
  40. public function __construct()
  41. {
  42. $prefix = 'Swift_CharacterReader_';
  43. $singleByte = array(
  44. 'class' => $prefix . 'GenericFixedWidthReader',
  45. 'constructor' => array(1)
  46. );
  47. $doubleByte = array(
  48. 'class' => $prefix . 'GenericFixedWidthReader',
  49. 'constructor' => array(2)
  50. );
  51. $fourBytes = array(
  52. 'class' => $prefix . 'GenericFixedWidthReader',
  53. 'constructor' => array(4)
  54. );
  55. //Utf-8
  56. $this->_map['utf-?8'] = array(
  57. 'class' => $prefix . 'Utf8Reader',
  58. 'constructor' => array()
  59. );
  60. //7-8 bit charsets
  61. $this->_map['(us-)?ascii'] = $singleByte;
  62. $this->_map['(iso|iec)-?8859-?[0-9]+'] = $singleByte;
  63. $this->_map['windows-?125[0-9]'] = $singleByte;
  64. $this->_map['cp-?[0-9]+'] = $singleByte;
  65. $this->_map['ansi'] = $singleByte;
  66. $this->_map['macintosh'] = $singleByte;
  67. $this->_map['koi-?7'] = $singleByte;
  68. $this->_map['koi-?8-?.+'] = $singleByte;
  69. $this->_map['mik'] = $singleByte;
  70. $this->_map['(cork|t1)'] = $singleByte;
  71. $this->_map['v?iscii'] = $singleByte;
  72. //16 bits
  73. $this->_map['(ucs-?2|utf-?16)'] = $doubleByte;
  74. //32 bits
  75. $this->_map['(ucs-?4|utf-?32)'] = $fourBytes;
  76. //Fallback
  77. $this->_map['.*'] = $singleByte;
  78. }
  79. /**
  80. * Returns a CharacterReader suitable for the charset applied.
  81. * @param string $charset
  82. * @return Swift_CharacterReader
  83. */
  84. public function getReaderFor($charset)
  85. {
  86. $charset = trim(strtolower($charset));
  87. foreach ($this->_map as $pattern => $spec)
  88. {
  89. $re = '/^' . $pattern . '$/D';
  90. if (preg_match($re, $charset))
  91. {
  92. if (!array_key_exists($pattern, $this->_loaded))
  93. {
  94. $reflector = new ReflectionClass($spec['class']);
  95. if ($reflector->getConstructor())
  96. {
  97. $reader = $reflector->newInstanceArgs($spec['constructor']);
  98. }
  99. else
  100. {
  101. $reader = $reflector->newInstance();
  102. }
  103. $this->_loaded[$pattern] = $reader;
  104. }
  105. return $this->_loaded[$pattern];
  106. }
  107. }
  108. }
  109. }