PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 3ms app.codeStats 0ms

/framework/vendor/swift/lib/classes/Swift/CharacterReaderFactory/SimpleCharacterReaderFactory.php

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