소스 검색

Added order feature for the Zend_Filter chain.

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@20280 44c647ce-9c0f-0410-b52a-842ac1e357ba
kokx 16 년 전
부모
커밋
73e307fa97
2개의 변경된 파일57개의 추가작업 그리고 3개의 파일을 삭제
  1. 44 3
      library/Zend/Filter.php
  2. 13 0
      tests/Zend/FilterTest.php

+ 44 - 3
library/Zend/Filter.php

@@ -32,6 +32,10 @@ require_once 'Zend/Filter/Interface.php';
  */
 class Zend_Filter implements Zend_Filter_Interface
 {
+
+    const CHAIN_APPEND  = 'append';
+    const CHAIN_PREPEND = 'prepend';
+
     /**
      * Filter chain
      *
@@ -47,18 +51,55 @@ class Zend_Filter implements Zend_Filter_Interface
     protected static $_defaultNamespaces = array();
 
     /**
-     * Adds a filter to the end of the chain
+     * Adds a filter to the chain
      *
      * @param  Zend_Filter_Interface $filter
+     * @param  string $placement
      * @return Zend_Filter Provides a fluent interface
      */
-    public function addFilter(Zend_Filter_Interface $filter)
+    public function addFilter(Zend_Filter_Interface $filter, $placement = self::CHAIN_APPEND)
     {
-        $this->_filters[] = $filter;
+        if ($placement == self::CHAIN_PREPEND) {
+            array_unshift($this->_filters, $filter);
+        } else {
+            $this->_filters[] = $filter;
+        }
         return $this;
     }
 
     /**
+     * Add a filter to the end of the chain
+     *
+     * @param  Zend_Filter_Interface $filter
+     * @return Zend_Filter Provides a fluent interface
+     */
+    public function appendFilter(Zend_Filter_Interface $filter)
+    {
+        return $this->addFilter($filter, self::CHAIN_APPEND);
+    }
+
+    /**
+     * Add a filter to the start of the chain
+     *
+     * @param  Zend_Filter_Interface $filter
+     * @return Zend_Filter Provides a fluent interface
+     */
+    public function prependFilter(Zend_Filter_Interface $filter)
+    {
+        return $this->addFilter($filter, self::CHAIN_PREPEND);
+    }
+
+    /**
+     * Get all the filters
+     *
+     * @return array
+     */
+    public function getFilters()
+    {
+        return $this->_filters;
+    }
+
+    /**
      * Returns $value filtered through each filter in the chain
      *
      * Filters are run in the order in which they were added to the chain (FIFO)

+ 13 - 0
tests/Zend/FilterTest.php

@@ -96,6 +96,19 @@ class Zend_FilterTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * Ensures that filters can be prepended and will be executed in the
+     * expected order
+     */
+    public function testFilterPrependOrder()
+    {
+        $this->_filter->appendFilter(new Zend_FilterTest_StripUpperCase())
+                      ->prependFilter(new Zend_FilterTest_LowerCase());
+        $value = 'AbC';
+        $valueExpected = 'abc';
+        $this->assertEquals($valueExpected, $this->_filter->filter($value));
+    }
+
+    /**
      * Ensures that we can call the static method get()
      * to instantiate a named validator by its class basename
      * and it returns the result of filter() with the input.