File indexing completed on 2024-05-19 06:03:03

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Filter
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * @see Zend_Filter_Interface
0024  */
0025 // require_once 'Zend/Filter/Interface.php';
0026 
0027 /**
0028  * @category   Zend
0029  * @package    Zend_Filter
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Filter_PregReplace implements Zend_Filter_Interface
0034 {
0035     /**
0036      * Pattern to match
0037      * @var mixed
0038      */
0039     protected $_matchPattern = null;
0040 
0041     /**
0042      * Replacement pattern
0043      * @var mixed
0044      */
0045     protected $_replacement = '';
0046 
0047     /**
0048      * Is unicode enabled?
0049      *
0050      * @var bool
0051      */
0052     static protected $_unicodeSupportEnabled = null;
0053 
0054     /**
0055      * Is Unicode Support Enabled Utility function
0056      *
0057      * @return bool
0058      */
0059     static public function isUnicodeSupportEnabled()
0060     {
0061         if (self::$_unicodeSupportEnabled === null) {
0062             self::_determineUnicodeSupport();
0063         }
0064 
0065         return self::$_unicodeSupportEnabled;
0066     }
0067 
0068     /**
0069      * Method to cache the regex needed to determine if unicode support is available
0070      *
0071      * @return bool
0072      */
0073     static protected function _determineUnicodeSupport()
0074     {
0075         self::$_unicodeSupportEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
0076     }
0077 
0078     /**
0079      * Constructor
0080      * Supported options are
0081      *     'match'   => matching pattern
0082      *     'replace' => replace with this
0083      *
0084      * @param  string|array $options
0085      * @return void
0086      */
0087     public function __construct($options = null)
0088     {
0089         if ($options instanceof Zend_Config) {
0090             $options = $options->toArray();
0091         } else if (!is_array($options)) {
0092             $options = func_get_args();
0093             $temp    = array();
0094             if (!empty($options)) {
0095                 $temp['match'] = array_shift($options);
0096             }
0097 
0098             if (!empty($options)) {
0099                 $temp['replace'] = array_shift($options);
0100             }
0101 
0102             $options = $temp;
0103         }
0104 
0105         if (array_key_exists('match', $options)) {
0106             $this->setMatchPattern($options['match']);
0107         }
0108 
0109         if (array_key_exists('replace', $options)) {
0110             $this->setReplacement($options['replace']);
0111         }
0112     }
0113 
0114     /**
0115      * Set the match pattern for the regex being called within filter()
0116      *
0117      * @param mixed $match - same as the first argument of preg_replace
0118      * @return Zend_Filter_PregReplace
0119      */
0120     public function setMatchPattern($match)
0121     {
0122         $this->_matchPattern = $match;
0123         return $this;
0124     }
0125 
0126     /**
0127      * Get currently set match pattern
0128      *
0129      * @return string
0130      */
0131     public function getMatchPattern()
0132     {
0133         return $this->_matchPattern;
0134     }
0135 
0136     /**
0137      * Set the Replacement pattern/string for the preg_replace called in filter
0138      *
0139      * @param mixed $replacement - same as the second argument of preg_replace
0140      * @return Zend_Filter_PregReplace
0141      */
0142     public function setReplacement($replacement)
0143     {
0144         $this->_replacement = $replacement;
0145         return $this;
0146     }
0147 
0148     /**
0149      * Get currently set replacement value
0150      *
0151      * @return string
0152      */
0153     public function getReplacement()
0154     {
0155         return $this->_replacement;
0156     }
0157 
0158     /**
0159      * Perform regexp replacement as filter
0160      *
0161      * @param  string $value
0162      * @return string
0163      */
0164     public function filter($value)
0165     {
0166         if ($this->_matchPattern == null) {
0167             // require_once 'Zend/Filter/Exception.php';
0168             throw new Zend_Filter_Exception(get_class($this) . ' does not have a valid MatchPattern set.');
0169         }
0170 
0171         return preg_replace($this->_matchPattern, $this->_replacement, $value);
0172     }
0173 
0174 }