PageRenderTime 29ms CodeModel.GetById 18ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Filter/StringTrim.php

https://bitbucket.org/hamidrezas/melobit
PHP | 124 lines | 47 code | 8 blank | 69 comment | 6 complexity | 97b076fefe2792d187557a0f6fe51354 MD5 | raw file
Possible License(s): AGPL-1.0
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 * @version    $Id: StringTrim.php 24594 2012-01-05 21:27:01Z matthew $
 20 */
 21
 22/**
 23 * @see Zend_Filter_Interface
 24 */
 25require_once 'Zend/Filter/Interface.php';
 26
 27/**
 28 * @category   Zend
 29 * @package    Zend_Filter
 30 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 31 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 32 */
 33class Zend_Filter_StringTrim implements Zend_Filter_Interface
 34{
 35    /**
 36     * List of characters provided to the trim() function
 37     *
 38     * If this is null, then trim() is called with no specific character list,
 39     * and its default behavior will be invoked, trimming whitespace.
 40     *
 41     * @var string|null
 42     */
 43    protected $_charList;
 44
 45    /**
 46     * Sets filter options
 47     *
 48     * @param  string|array|Zend_Config $options
 49     * @return void
 50     */
 51    public function __construct($options = null)
 52    {
 53        if ($options instanceof Zend_Config) {
 54            $options = $options->toArray();
 55        } else if (!is_array($options)) {
 56            $options          = func_get_args();
 57            $temp['charlist'] = array_shift($options);
 58            $options          = $temp;
 59        }
 60
 61        if (array_key_exists('charlist', $options)) {
 62            $this->setCharList($options['charlist']);
 63        }
 64    }
 65
 66    /**
 67     * Returns the charList option
 68     *
 69     * @return string|null
 70     */
 71    public function getCharList()
 72    {
 73        return $this->_charList;
 74    }
 75
 76    /**
 77     * Sets the charList option
 78     *
 79     * @param  string|null $charList
 80     * @return Zend_Filter_StringTrim Provides a fluent interface
 81     */
 82    public function setCharList($charList)
 83    {
 84        $this->_charList = $charList;
 85        return $this;
 86    }
 87
 88    /**
 89     * Defined by Zend_Filter_Interface
 90     *
 91     * Returns the string $value with characters stripped from the beginning and end
 92     *
 93     * @param  string $value
 94     * @return string
 95     */
 96    public function filter($value)
 97    {
 98        if (null === $this->_charList) {
 99            return $this->_unicodeTrim((string) $value);
100        } else {
101            return $this->_unicodeTrim((string) $value, $this->_charList);
102        }
103    }
104
105    /**
106     * Unicode aware trim method
107     * Fixes a PHP problem
108     *
109     * @param string $value
110     * @param string $charlist
111     * @return string
112     */
113    protected function _unicodeTrim($value, $charlist = '\\\\s')
114    {
115        $chars = preg_replace(
116            array( '/[\^\-\]\\\]/S', '/\\\{4}/S', '/\//'),
117            array( '\\\\\\0', '\\', '\/' ),
118            $charlist
119        );
120
121        $pattern = '^[' . $chars . ']*|[' . $chars . ']*$';
122        return preg_replace("/$pattern/sSD", '', $value);
123    }
124}