2
0

Filter.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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-2008 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-2008 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. * Adds a filter to the end of the chain
  41. *
  42. * @param Zend_Filter_Interface $filter
  43. * @return Zend_Filter Provides a fluent interface
  44. */
  45. public function addFilter(Zend_Filter_Interface $filter)
  46. {
  47. $this->_filters[] = $filter;
  48. return $this;
  49. }
  50. /**
  51. * Returns $value filtered through each filter in the chain
  52. *
  53. * Filters are run in the order in which they were added to the chain (FIFO)
  54. *
  55. * @param mixed $value
  56. * @return mixed
  57. */
  58. public function filter($value)
  59. {
  60. $valueFiltered = $value;
  61. foreach ($this->_filters as $filter) {
  62. $valueFiltered = $filter->filter($valueFiltered);
  63. }
  64. return $valueFiltered;
  65. }
  66. /**
  67. * Returns a value filtered through a specified filter class, without requiring separate
  68. * instantiation of the filter object.
  69. *
  70. * The first argument of this method is a data input value, that you would have filtered.
  71. * The second argument is a string, which corresponds to the basename of the filter class,
  72. * relative to the Zend_Filter namespace. This method automatically loads the class,
  73. * creates an instance, and applies the filter() method to the data input. You can also pass
  74. * an array of constructor arguments, if they are needed for the filter class.
  75. *
  76. * @param mixed $value
  77. * @param string $classBaseName
  78. * @param array $args OPTIONAL
  79. * @param array|string $namespaces OPTIONAL
  80. * @return mixed
  81. * @throws Zend_Filter_Exception
  82. */
  83. public static function get($value, $classBaseName, array $args = array(), $namespaces = array())
  84. {
  85. require_once 'Zend/Loader.php';
  86. $namespaces = array_merge((array) $namespaces, array('Zend_Filter'));
  87. foreach ($namespaces as $namespace) {
  88. $className = $namespace . '_' . ucfirst($classBaseName);
  89. if (!class_exists($className)) {
  90. try {
  91. require_once 'Zend/Loader.php';
  92. Zend_Loader::loadClass($className);
  93. } catch (Zend_Exception $ze) {
  94. continue;
  95. }
  96. }
  97. $class = new ReflectionClass($className);
  98. if ($class->implementsInterface('Zend_Filter_Interface')) {
  99. if ($class->hasMethod('__construct')) {
  100. $object = $class->newInstanceArgs($args);
  101. } else {
  102. $object = $class->newInstance();
  103. }
  104. return $object->filter($value);
  105. }
  106. }
  107. require_once 'Zend/Filter/Exception.php';
  108. throw new Zend_Filter_Exception("Filter class not found from basename '$classBaseName'");
  109. }
  110. }