File indexing completed on 2025-01-26 05:30:08
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Filter 0017 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0018 * @license http://framework.zend.com/license/new-bsd New BSD License 0019 * @version $Id$ 0020 */ 0021 0022 /** 0023 * @see Zend_Filter_Interface 0024 */ 0025 // require_once 'Zend/Filter/Interface.php'; 0026 0027 /** 0028 * @category Zend 0029 * @package Zend_Filter 0030 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0031 * @license http://framework.zend.com/license/new-bsd New BSD License 0032 */ 0033 class Zend_Filter implements Zend_Filter_Interface 0034 { 0035 0036 const CHAIN_APPEND = 'append'; 0037 const CHAIN_PREPEND = 'prepend'; 0038 0039 /** 0040 * Filter chain 0041 * 0042 * @var array 0043 */ 0044 protected $_filters = array(); 0045 0046 /** 0047 * Default Namespaces 0048 * 0049 * @var array 0050 */ 0051 protected static $_defaultNamespaces = array(); 0052 0053 /** 0054 * Adds a filter to the chain 0055 * 0056 * @param Zend_Filter_Interface $filter 0057 * @param string $placement 0058 * @return Zend_Filter Provides a fluent interface 0059 */ 0060 public function addFilter(Zend_Filter_Interface $filter, $placement = self::CHAIN_APPEND) 0061 { 0062 if ($placement == self::CHAIN_PREPEND) { 0063 array_unshift($this->_filters, $filter); 0064 } else { 0065 $this->_filters[] = $filter; 0066 } 0067 return $this; 0068 } 0069 0070 /** 0071 * Add a filter to the end of the chain 0072 * 0073 * @param Zend_Filter_Interface $filter 0074 * @return Zend_Filter Provides a fluent interface 0075 */ 0076 public function appendFilter(Zend_Filter_Interface $filter) 0077 { 0078 return $this->addFilter($filter, self::CHAIN_APPEND); 0079 } 0080 0081 /** 0082 * Add a filter to the start of the chain 0083 * 0084 * @param Zend_Filter_Interface $filter 0085 * @return Zend_Filter Provides a fluent interface 0086 */ 0087 public function prependFilter(Zend_Filter_Interface $filter) 0088 { 0089 return $this->addFilter($filter, self::CHAIN_PREPEND); 0090 } 0091 0092 /** 0093 * Get all the filters 0094 * 0095 * @return array 0096 */ 0097 public function getFilters() 0098 { 0099 return $this->_filters; 0100 } 0101 0102 /** 0103 * Returns $value filtered through each filter in the chain 0104 * 0105 * Filters are run in the order in which they were added to the chain (FIFO) 0106 * 0107 * @param mixed $value 0108 * @return mixed 0109 */ 0110 public function filter($value) 0111 { 0112 $valueFiltered = $value; 0113 foreach ($this->_filters as $filter) { 0114 $valueFiltered = $filter->filter($valueFiltered); 0115 } 0116 return $valueFiltered; 0117 } 0118 0119 /** 0120 * Returns the set default namespaces 0121 * 0122 * @return array 0123 */ 0124 public static function getDefaultNamespaces() 0125 { 0126 return self::$_defaultNamespaces; 0127 } 0128 0129 /** 0130 * Sets new default namespaces 0131 * 0132 * @param array|string $namespace 0133 * @return null 0134 */ 0135 public static function setDefaultNamespaces($namespace) 0136 { 0137 if (!is_array($namespace)) { 0138 $namespace = array((string) $namespace); 0139 } 0140 0141 self::$_defaultNamespaces = $namespace; 0142 } 0143 0144 /** 0145 * Adds a new default namespace 0146 * 0147 * @param array|string $namespace 0148 * @return null 0149 */ 0150 public static function addDefaultNamespaces($namespace) 0151 { 0152 if (!is_array($namespace)) { 0153 $namespace = array((string) $namespace); 0154 } 0155 0156 self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace)); 0157 } 0158 0159 /** 0160 * Returns true when defaultNamespaces are set 0161 * 0162 * @return boolean 0163 */ 0164 public static function hasDefaultNamespaces() 0165 { 0166 return (!empty(self::$_defaultNamespaces)); 0167 } 0168 0169 /** 0170 * @deprecated 0171 * @see Zend_Filter::filterStatic() 0172 * 0173 * @param mixed $value 0174 * @param string $classBaseName 0175 * @param array $args OPTIONAL 0176 * @param array|string $namespaces OPTIONAL 0177 * @return mixed 0178 * @throws Zend_Filter_Exception 0179 */ 0180 public static function get($value, $classBaseName, array $args = array(), $namespaces = array()) 0181 { 0182 trigger_error( 0183 'Zend_Filter::get() is deprecated as of 1.9.0; please update your code to utilize Zend_Filter::filterStatic()', 0184 E_USER_NOTICE 0185 ); 0186 0187 return self::filterStatic($value, $classBaseName, $args, $namespaces); 0188 } 0189 0190 /** 0191 * Returns a value filtered through a specified filter class, without requiring separate 0192 * instantiation of the filter object. 0193 * 0194 * The first argument of this method is a data input value, that you would have filtered. 0195 * The second argument is a string, which corresponds to the basename of the filter class, 0196 * relative to the Zend_Filter namespace. This method automatically loads the class, 0197 * creates an instance, and applies the filter() method to the data input. You can also pass 0198 * an array of constructor arguments, if they are needed for the filter class. 0199 * 0200 * @param mixed $value 0201 * @param string $classBaseName 0202 * @param array $args OPTIONAL 0203 * @param array|string $namespaces OPTIONAL 0204 * @return mixed 0205 * @throws Zend_Filter_Exception 0206 */ 0207 public static function filterStatic($value, $classBaseName, array $args = array(), $namespaces = array()) 0208 { 0209 // require_once 'Zend/Loader.php'; 0210 $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Filter')); 0211 foreach ($namespaces as $namespace) { 0212 $className = $namespace . '_' . ucfirst($classBaseName); 0213 if (!class_exists($className, false)) { 0214 try { 0215 $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php'; 0216 if (Zend_Loader::isReadable($file)) { 0217 Zend_Loader::loadClass($className); 0218 } else { 0219 continue; 0220 } 0221 } catch (Zend_Exception $ze) { 0222 continue; 0223 } 0224 } 0225 0226 $class = new ReflectionClass($className); 0227 if ($class->implementsInterface('Zend_Filter_Interface')) { 0228 if ($class->hasMethod('__construct')) { 0229 $object = $class->newInstanceArgs($args); 0230 } else { 0231 $object = $class->newInstance(); 0232 } 0233 return $object->filter($value); 0234 } 0235 } 0236 // require_once 'Zend/Filter/Exception.php'; 0237 throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'"); 0238 } 0239 }