7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Filter
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
29 * @package Zend_Filter
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Filter
implements Zend_Filter_Interface
36 const CHAIN_APPEND
= 'append';
37 const CHAIN_PREPEND
= 'prepend';
44 protected $_filters = array();
51 protected static $_defaultNamespaces = array();
54 * Adds a filter to the chain
56 * @param Zend_Filter_Interface $filter
57 * @param string $placement
58 * @return Zend_Filter Provides a fluent interface
60 public function addFilter(Zend_Filter_Interface
$filter, $placement = self
::CHAIN_APPEND
)
62 if ($placement == self
::CHAIN_PREPEND
) {
63 array_unshift($this->_filters
, $filter);
65 $this->_filters
[] = $filter;
71 * Add a filter to the end of the chain
73 * @param Zend_Filter_Interface $filter
74 * @return Zend_Filter Provides a fluent interface
76 public function appendFilter(Zend_Filter_Interface
$filter)
78 return $this->addFilter($filter, self
::CHAIN_APPEND
);
82 * Add a filter to the start of the chain
84 * @param Zend_Filter_Interface $filter
85 * @return Zend_Filter Provides a fluent interface
87 public function prependFilter(Zend_Filter_Interface
$filter)
89 return $this->addFilter($filter, self
::CHAIN_PREPEND
);
97 public function getFilters()
99 return $this->_filters
;
103 * Returns $value filtered through each filter in the chain
105 * Filters are run in the order in which they were added to the chain (FIFO)
107 * @param mixed $value
110 public function filter($value)
112 $valueFiltered = $value;
113 foreach ($this->_filters
as $filter) {
114 $valueFiltered = $filter->filter($valueFiltered);
116 return $valueFiltered;
120 * Returns the set default namespaces
124 public static function getDefaultNamespaces()
126 return self
::$_defaultNamespaces;
130 * Sets new default namespaces
132 * @param array|string $namespace
135 public static function setDefaultNamespaces($namespace)
137 if (!is_array($namespace)) {
138 $namespace = array((string) $namespace);
141 self
::$_defaultNamespaces = $namespace;
145 * Adds a new default namespace
147 * @param array|string $namespace
150 public static function addDefaultNamespaces($namespace)
152 if (!is_array($namespace)) {
153 $namespace = array((string) $namespace);
156 self
::$_defaultNamespaces = array_unique(array_merge(self
::$_defaultNamespaces, $namespace));
160 * Returns true when defaultNamespaces are set
164 public static function hasDefaultNamespaces()
166 return (!empty(self
::$_defaultNamespaces));
171 * @see Zend_Filter::filterStatic()
173 * @param mixed $value
174 * @param string $classBaseName
175 * @param array $args OPTIONAL
176 * @param array|string $namespaces OPTIONAL
178 * @throws Zend_Filter_Exception
180 public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
183 'Zend_Filter::get() is deprecated as of 1.9.0; please update your code to utilize Zend_Filter::filterStatic()',
187 return self
::filterStatic($value, $classBaseName, $args, $namespaces);
191 * Returns a value filtered through a specified filter class, without requiring separate
192 * instantiation of the filter object.
194 * The first argument of this method is a data input value, that you would have filtered.
195 * The second argument is a string, which corresponds to the basename of the filter class,
196 * relative to the Zend_Filter namespace. This method automatically loads the class,
197 * creates an instance, and applies the filter() method to the data input. You can also pass
198 * an array of constructor arguments, if they are needed for the filter class.
200 * @param mixed $value
201 * @param string $classBaseName
202 * @param array $args OPTIONAL
203 * @param array|string $namespaces OPTIONAL
205 * @throws Zend_Filter_Exception
207 public static function filterStatic($value, $classBaseName, array $args = array(), $namespaces = array())
209 require_once 'Zend/Loader.php';
210 $namespaces = array_merge((array) $namespaces, self
::$_defaultNamespaces, array('Zend_Filter'));
211 foreach ($namespaces as $namespace) {
212 $className = $namespace . '_' . ucfirst($classBaseName);
213 if (!class_exists($className, false)) {
215 $file = str_replace('_', DIRECTORY_SEPARATOR
, $className) . '.php';
216 if (Zend_Loader
::isReadable($file)) {
217 Zend_Loader
::loadClass($className);
221 } catch (Zend_Exception
$ze) {
226 $class = new ReflectionClass($className);
227 if ($class->implementsInterface('Zend_Filter_Interface')) {
228 if ($class->hasMethod('__construct')) {
229 $object = $class->newInstanceArgs($args);
231 $object = $class->newInstance();
233 return $object->filter($value);
236 require_once 'Zend/Filter/Exception.php';
237 throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'");