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