TYPO3 API  SVNRelease
SimpleCharacterReaderFactory.php
Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  * This file is part of SwiftMailer.
00005  * (c) 2004-2009 Chris Corbyn
00006  *
00007  * For the full copyright and license information, please view the LICENSE
00008  * file that was distributed with this source code.
00009  */
00010 
00011 //@require 'Swift/CharacterReaderFactory.php';
00012 
00013 /**
00014  * Standard factory for creating CharacterReaders.
00015  * @package Swift
00016  * @subpackage Encoder
00017  * @author Chris Corbyn
00018  */
00019 class Swift_CharacterReaderFactory_SimpleCharacterReaderFactory
00020   implements Swift_CharacterReaderFactory
00021 {
00022 
00023   /**
00024    * A map of charset patterns to their implementation classes.
00025    * @var array
00026    * @access private
00027    */
00028   private $_map = array();
00029 
00030   /**
00031    * Factories which have already been loaded.
00032    * @var Swift_CharacterReaderFactory[]
00033    * @access private
00034    */
00035   private $_loaded = array();
00036 
00037   /**
00038    * Creates a new CharacterReaderFactory.
00039    */
00040   public function __construct()
00041   {
00042     $prefix = 'Swift_CharacterReader_';
00043 
00044     $singleByte = array(
00045       'class' => $prefix . 'GenericFixedWidthReader',
00046       'constructor' => array(1)
00047       );
00048 
00049     $doubleByte = array(
00050       'class' => $prefix . 'GenericFixedWidthReader',
00051       'constructor' => array(2)
00052       );
00053 
00054     $fourBytes = array(
00055       'class' => $prefix . 'GenericFixedWidthReader',
00056       'constructor' => array(4)
00057       );
00058 
00059     //Utf-8
00060     $this->_map['utf-?8'] = array(
00061       'class' => $prefix . 'Utf8Reader',
00062       'constructor' => array()
00063       );
00064 
00065     //7-8 bit charsets
00066     $this->_map['(us-)?ascii'] = $singleByte;
00067     $this->_map['(iso|iec)-?8859-?[0-9]+'] = $singleByte;
00068     $this->_map['windows-?125[0-9]'] = $singleByte;
00069     $this->_map['cp-?[0-9]+'] = $singleByte;
00070     $this->_map['ansi'] = $singleByte;
00071     $this->_map['macintosh'] = $singleByte;
00072     $this->_map['koi-?7'] = $singleByte;
00073     $this->_map['koi-?8-?.+'] = $singleByte;
00074     $this->_map['mik'] = $singleByte;
00075     $this->_map['(cork|t1)'] = $singleByte;
00076     $this->_map['v?iscii'] = $singleByte;
00077 
00078     //16 bits
00079     $this->_map['(ucs-?2|utf-?16)'] = $doubleByte;
00080 
00081     //32 bits
00082     $this->_map['(ucs-?4|utf-?32)'] = $fourBytes;
00083 
00084     //Fallback
00085     $this->_map['.*'] = $singleByte;
00086   }
00087 
00088   /**
00089    * Returns a CharacterReader suitable for the charset applied.
00090    * @param string $charset
00091    * @return Swift_CharacterReader
00092    */
00093   public function getReaderFor($charset)
00094   {
00095     $charset = trim(strtolower($charset));
00096     foreach ($this->_map as $pattern => $spec)
00097     {
00098       $re = '/^' . $pattern . '$/D';
00099       if (preg_match($re, $charset))
00100       {
00101         if (!array_key_exists($pattern, $this->_loaded))
00102         {
00103           $reflector = new ReflectionClass($spec['class']);
00104           if ($reflector->getConstructor())
00105           {
00106             $reader = $reflector->newInstanceArgs($spec['constructor']);
00107           }
00108           else
00109           {
00110             $reader = $reflector->newInstance();
00111           }
00112           $this->_loaded[$pattern] = $reader;
00113         }
00114         return $this->_loaded[$pattern];
00115       }
00116     }
00117   }
00118 
00119 }