Filter.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. * Returns a value filtered through a specified filter class, without requiring separate
  118. * instantiation of the filter object.
  119. *
  120. * The first argument of this method is a data input value, that you would have filtered.
  121. * The second argument is a string, which corresponds to the basename of the filter class,
  122. * relative to the Zend_Filter namespace. This method automatically loads the class,
  123. * creates an instance, and applies the filter() method to the data input. You can also pass
  124. * an array of constructor arguments, if they are needed for the filter class.
  125. *
  126. * @param mixed $value
  127. * @param string $classBaseName
  128. * @param array $args OPTIONAL
  129. * @param array|string $namespaces OPTIONAL
  130. * @return mixed
  131. * @throws Zend_Filter_Exception
  132. */
  133. public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
  134. {
  135. require_once 'Zend/Loader.php';
  136. $namespaces = array_merge((array) $namespaces, self::$_defaultNamespaces, array('Zend_Filter'));
  137. foreach ($namespaces as $namespace) {
  138. $className = $namespace . '_' . ucfirst($classBaseName);
  139. if (!class_exists($className)) {
  140. try {
  141. require_once 'Zend/Loader.php';
  142. Zend_Loader::loadClass($className);
  143. } catch (Zend_Exception $ze) {
  144. continue;
  145. }
  146. }
  147. $class = new ReflectionClass($className);
  148. if ($class->implementsInterface('Zend_Filter_Interface')) {
  149. if ($class->hasMethod('__construct')) {
  150. $object = $class->newInstanceArgs($args);
  151. } else {
  152. $object = $class->newInstance();
  153. }
  154. return $object->filter($value);
  155. }
  156. }
  157. require_once 'Zend/Filter/Exception.php';
  158. throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'");
  159. }
  160. }