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