Tidy.php

Go to the documentation of this file.
00001 <?php
00002 
00033 class Robo47_Filter_Tidy implements Zend_Filter_Interface
00034 {
00035 
00040     protected $_tidy = null;
00046     protected $_config = array();
00051     protected $_encoding = 'utf8';
00057     protected static $_defaultTidy;
00058 
00066     public function __construct($tidy = null, $config = null,
00067         $encoding = 'utf8')
00068     {
00069         $this->setTidy($tidy);
00070         $this->setConfig($config);
00071         $this->setEncoding($encoding);
00072     }
00073 
00080     public function setConfig($config = null)
00081     {
00082         if (null !== $config) {
00083             if ($config instanceof Zend_Config) {
00084                 $config = $config->toArray();
00085             }
00086             $this->_config = $config;
00087         }
00088         return $this;
00089     }
00090 
00096     public function getConfig()
00097     {
00098         return $this->_config;
00099     }
00100 
00108     public function setEncoding($encoding)
00109     {
00110         $encoding = strtolower($encoding);
00111         switch ($encoding) {
00112             case 'utf-8':
00113                 $encoding = 'utf8';
00114             case 'ascii':
00115             case 'latin0':
00116             case 'latin1':
00117             case 'raw':
00118             case 'utf8':
00119             case 'iso2022':
00120             case 'mac':
00121             case 'win1252':
00122             case 'ibm858':
00123             case 'utf16':
00124             case 'utf16le':
00125             case 'utf16be':
00126             case 'big5':
00127             case 'shiftjis':
00128                 $this->_encoding = $encoding;
00129                 break;
00130             default:
00131                 $message = 'Unknown encoding: ' . $encoding;
00132                 throw new Robo47_Filter_Exception($message);
00133         }
00134         return $this;
00135     }
00136 
00143     public function setTidy($tidy = null)
00144     {
00145         if (null === $tidy) {
00146             if (null === self::$_defaultTidy) {
00147                 $tidy = new Tidy();
00148             } else {
00149                 $tidy = self::$_defaultTidy;
00150             }
00151         } elseif (is_string($tidy)) {
00152             $tidy = $this->_tidyFromRegistry($tidy);
00153         }
00154 
00155         if (!$tidy instanceof Tidy) {
00156             $message = 'Tidy is no instance of class Tidy';
00157             throw new Robo47_Filter_Exception($message);
00158         }
00159         $this->_tidy = $tidy;
00160 
00161         return $this;
00162     }
00163 
00171     protected function _tidyFromRegistry($key)
00172     {
00173         if (Zend_Registry::isRegistered($key)) {
00174             return Zend_Registry::get($key);
00175         } else {
00176             $message = 'Registry key "' . $key .
00177                 '" for Tidy is not registered.';
00178             throw new Robo47_Filter_Exception($message);
00179         }
00180     }
00181 
00187     public function getTidy()
00188     {
00189         return $this->_tidy;
00190     }
00191 
00197     public static function setDefaultTidy(Tidy $tidy = null)
00198     {
00199         self::$_defaultTidy = $tidy;
00200     }
00201 
00207     public function getEncoding()
00208     {
00209         return $this->_encoding;
00210     }
00211 
00217     public static function getDefaultTidy()
00218     {
00219         return self::$_defaultTidy;
00220     }
00221 
00229     public function filter($value)
00230     {
00231         $this->_tidy->parseString(
00232             $value,
00233             $this->getConfig(),
00234             $this->getEncoding()
00235         );
00236         $this->_tidy->cleanRepair();
00237         return (string) $this->_tidy;
00238     }
00239 }