/Zend/Filter/Filter.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 245 lines · 101 code · 21 blank · 123 comment · 11 complexity · e2d36723c36efda88e09000d8f4e0d27 MD5 · raw file

  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  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.
  14. *
  15. * @category Zend
  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
  19. * @version $Id: Filter.php 21096 2010-02-19 20:10:54Z thomas $
  20. */
  21. /**
  22. * @namespace
  23. */
  24. namespace Zend\Filter;
  25. use Zend\Loader;
  26. /**
  27. * @see Zend_Filter_Interface
  28. */
  29. require_once 'Zend/Filter/Interface.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_Filter
  33. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. class Filter implements FilterInterface
  37. {
  38. const CHAIN_APPEND = 'append';
  39. const CHAIN_PREPEND = 'prepend';
  40. /**
  41. * Filter chain
  42. *
  43. * @var array
  44. */
  45. protected $_filters = array();
  46. /**
  47. * Default Namespaces
  48. *
  49. * @var array
  50. */
  51. protected static $_defaultNamespaces = array();
  52. /**
  53. * Adds a filter to the chain
  54. *
  55. * @param Zend_Filter_Interface $filter
  56. * @param string $placement
  57. * @return Zend_Filter Provides a fluent interface
  58. */
  59. public function addFilter(FilterInterface $filter, $placement = self::CHAIN_APPEND)
  60. {
  61. if ($placement == self::CHAIN_PREPEND) {
  62. array_unshift($this->_filters, $filter);
  63. } else {
  64. $this->_filters[] = $filter;
  65. }
  66. return $this;
  67. }
  68. /**
  69. * Add a filter to the end of the chain
  70. *
  71. * @param Zend_Filter_Interface $filter
  72. * @return Zend_Filter Provides a fluent interface
  73. */
  74. public function appendFilter(FilterInterface $filter)
  75. {
  76. return $this->addFilter($filter, self::CHAIN_APPEND);
  77. }
  78. /**
  79. * Add a filter to the start of the chain
  80. *
  81. * @param Zend_Filter_Interface $filter
  82. * @return Zend_Filter Provides a fluent interface
  83. */
  84. public function prependFilter(FilterInterface $filter)
  85. {
  86. return $this->addFilter($filter, self::CHAIN_PREPEND);
  87. }
  88. /**
  89. * Get all the filters
  90. *
  91. * @return array
  92. */
  93. public function getFilters()
  94. {
  95. return $this->_filters;
  96. }
  97. /**
  98. * Returns $value filtered through each filter in the chain
  99. *
  100. * Filters are run in the order in which they were added to the chain (FIFO)
  101. *
  102. * @param mixed $value
  103. * @return mixed
  104. */
  105. public function filter($value)
  106. {
  107. $valueFiltered = $value;
  108. foreach ($this->_filters as $filter) {
  109. $valueFiltered = $filter->filter($valueFiltered);
  110. }
  111. return $valueFiltered;
  112. }
  113. /**
  114. * Returns the set default namespaces
  115. *
  116. * @return array
  117. */
  118. public static function getDefaultNamespaces()
  119. {
  120. return self::$_defaultNamespaces;
  121. }
  122. /**
  123. * Sets new default namespaces
  124. *
  125. * @param array|string $namespace
  126. * @return null
  127. */
  128. public static function setDefaultNamespaces($namespace)
  129. {
  130. if (!is_array($namespace)) {
  131. $namespace = array((string) $namespace);
  132. }
  133. self::$_defaultNamespaces = $namespace;
  134. }
  135. /**
  136. * Adds a new default namespace
  137. *
  138. * @param array|string $namespace
  139. * @return null
  140. */
  141. public static function addDefaultNamespaces($namespace)
  142. {
  143. if (!is_array($namespace)) {
  144. $namespace = array((string) $namespace);
  145. }
  146. self::$_defaultNamespaces = array_unique(array_merge(self::$_defaultNamespaces, $namespace));
  147. }
  148. /**
  149. * Returns true when defaultNamespaces are set
  150. *
  151. * @return boolean
  152. */
  153. public static function hasDefaultNamespaces()
  154. {
  155. return (!empty(self::$_defaultNamespaces));
  156. }
  157. /**
  158. * @deprecated
  159. * @see Zend_Filter::filterStatic()
  160. *
  161. * @param mixed $value
  162. * @param string $classBaseName
  163. * @param array $args OPTIONAL
  164. * @param array|string $namespaces OPTIONAL
  165. * @return mixed
  166. * @throws Zend_Filter_Exception
  167. */
  168. public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
  169. {
  170. trigger_error(
  171. 'Zend_Filter::get() is deprecated as of 1.9.0; please update your code to utilize Zend_Filter::filterStatic()',
  172. E_USER_NOTICE
  173. );
  174. return self::filterStatic($value, $classBaseName, $args, $namespaces);
  175. }
  176. /**
  177. * Returns a value filtered through a specified filter class, without requiring separate
  178. * instantiation of the filter object.
  179. *
  180. * The first argument of this method is a data input value, that you would have filtered.
  181. * The second argument is a string, which corresponds to the basename of the filter class,
  182. * relative to the Zend_Filter namespace. This method automatically loads the class,
  183. * creates an instance, and applies the filter() method to the data input. You can also pass
  184. * an array of constructor arguments, if they are needed for the filter class.
  185. *
  186. * @param mixed $value
  187. * @param string $classBaseName
  188. * @param array $args OPTIONAL
  189. * @param array|string $namespaces OPTIONAL
  190. * @return mixed
  191. * @throws Zend_Filter_Exception
  192. */
  193. public static function filterStatic($value, $classBaseName, array $args = array(), $namespaces = array())
  194. {
  195. require_once 'Zend/Loader.php';
  196. $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Filter'));
  197. foreach ($namespaces as $namespace) {
  198. $className = $namespace . '_' . ucfirst($classBaseName);
  199. if (!class_exists($className, false)) {
  200. try {
  201. $file = str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  202. if (Loader\Loader::isReadable($file)) {
  203. Loader\Loader::loadClass($className);
  204. } else {
  205. continue;
  206. }
  207. } catch (\Zend\Exception $ze) {
  208. continue;
  209. }
  210. }
  211. $class = new \ReflectionClass($className);
  212. if ($class->implementsInterface('Zend_Filter_Interface')) {
  213. if ($class->hasMethod('__construct')) {
  214. $object = $class->newInstanceArgs($args);
  215. } else {
  216. $object = $class->newInstance();
  217. }
  218. return $object->filter($value);
  219. }
  220. }
  221. require_once 'Zend/Filter/Exception.php';
  222. throw new Exception("Filter class not found from basename '$classBaseName'");
  223. }
  224. }