File indexing completed on 2024-12-22 05:36:41

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_StringTrim implements Zend_Filter_Interface
0034 {
0035     /**
0036      * List of characters provided to the trim() function
0037      *
0038      * If this is null, then trim() is called with no specific character list,
0039      * and its default behavior will be invoked, trimming whitespace.
0040      *
0041      * @var string|null
0042      */
0043     protected $_charList;
0044 
0045     /**
0046      * Sets filter options
0047      *
0048      * @param  string|array|Zend_Config $options
0049      * @return void
0050      */
0051     public function __construct($options = null)
0052     {
0053         if ($options instanceof Zend_Config) {
0054             $options = $options->toArray();
0055         } else if (!is_array($options)) {
0056             $options          = func_get_args();
0057             $temp['charlist'] = array_shift($options);
0058             $options          = $temp;
0059         }
0060 
0061         if (array_key_exists('charlist', $options)) {
0062             $this->setCharList($options['charlist']);
0063         }
0064     }
0065 
0066     /**
0067      * Returns the charList option
0068      *
0069      * @return string|null
0070      */
0071     public function getCharList()
0072     {
0073         return $this->_charList;
0074     }
0075 
0076     /**
0077      * Sets the charList option
0078      *
0079      * @param  string|null $charList
0080      * @return Zend_Filter_StringTrim Provides a fluent interface
0081      */
0082     public function setCharList($charList)
0083     {
0084         $this->_charList = $charList;
0085         return $this;
0086     }
0087 
0088     /**
0089      * Defined by Zend_Filter_Interface
0090      *
0091      * Returns the string $value with characters stripped from the beginning and end
0092      *
0093      * @param  string $value
0094      * @return string
0095      */
0096     public function filter($value)
0097     {
0098         if (null === $this->_charList) {
0099             return $this->_unicodeTrim((string) $value);
0100         } else {
0101             return $this->_unicodeTrim((string) $value, $this->_charList);
0102         }
0103     }
0104 
0105     /**
0106      * Unicode aware trim method
0107      * Fixes a PHP problem
0108      *
0109      * @param string $value
0110      * @param string $charlist
0111      * @return string
0112      */
0113     protected function _unicodeTrim($value, $charlist = '\\\\s')
0114     {
0115         $chars = preg_replace(
0116             array( '/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'),
0117             array( '\\\\\\0', '\\', '\/' ),
0118             $charlist
0119         );
0120 
0121         $pattern = '^[' . $chars . ']*|[' . $chars . ']*$';
0122         return preg_replace("/$pattern/sSD", '', $value);
0123     }
0124 }