Filter.php 6.0 KB

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