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  * @see Zend_Locale
0028  */
0029 // require_once 'Zend/Locale.php';
0030 
0031 /**
0032  * @category   Zend
0033  * @package    Zend_Filter
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Filter_Alpha implements Zend_Filter_Interface
0038 {
0039     /**
0040      * Whether to allow white space characters; off by default
0041      *
0042      * @var boolean
0043      * @deprecated
0044      */
0045     public $allowWhiteSpace;
0046 
0047     /**
0048      * Is PCRE is compiled with UTF-8 and Unicode support
0049      *
0050      * @var mixed
0051      **/
0052     protected static $_unicodeEnabled;
0053 
0054     /**
0055      * Locale in browser.
0056      *
0057      * @var Zend_Locale object
0058      */
0059     protected $_locale;
0060 
0061     /**
0062      * The Alphabet means english alphabet.
0063      *
0064      * @var boolean
0065      */
0066     protected static $_meansEnglishAlphabet;
0067 
0068     /**
0069      * Sets default option values for this instance
0070      *
0071      * @param  boolean $allowWhiteSpace
0072      * @return void
0073      */
0074     public function __construct($allowWhiteSpace = false)
0075     {
0076         if ($allowWhiteSpace instanceof Zend_Config) {
0077             $allowWhiteSpace = $allowWhiteSpace->toArray();
0078         } else if (is_array($allowWhiteSpace)) {
0079             if (array_key_exists('allowwhitespace', $allowWhiteSpace)) {
0080                 $allowWhiteSpace = $allowWhiteSpace['allowwhitespace'];
0081             } else {
0082                 $allowWhiteSpace = false;
0083             }
0084         }
0085 
0086         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
0087         if (null === self::$_unicodeEnabled) {
0088             self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
0089         }
0090 
0091         if (null === self::$_meansEnglishAlphabet) {
0092             $this->_locale = new Zend_Locale('auto');
0093             self::$_meansEnglishAlphabet = in_array($this->_locale->getLanguage(),
0094                                                     array('ja', 'ko', 'zh')
0095                                                     );
0096         }
0097 
0098     }
0099 
0100     /**
0101      * Returns the allowWhiteSpace option
0102      *
0103      * @return boolean
0104      */
0105     public function getAllowWhiteSpace()
0106     {
0107         return $this->allowWhiteSpace;
0108     }
0109 
0110     /**
0111      * Sets the allowWhiteSpace option
0112      *
0113      * @param boolean $allowWhiteSpace
0114      * @return Zend_Filter_Alpha Provides a fluent interface
0115      */
0116     public function setAllowWhiteSpace($allowWhiteSpace)
0117     {
0118         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
0119         return $this;
0120     }
0121 
0122     /**
0123      * Defined by Zend_Filter_Interface
0124      *
0125      * Returns the string $value, removing all but alphabetic characters
0126      *
0127      * @param  string $value
0128      * @return string
0129      */
0130     public function filter($value)
0131     {
0132         $whiteSpace = $this->allowWhiteSpace ? '\s' : '';
0133         if (!self::$_unicodeEnabled) {
0134             // POSIX named classes are not supported, use alternative a-zA-Z match
0135             $pattern = '/[^a-zA-Z' . $whiteSpace . ']/';
0136         } else if (self::$_meansEnglishAlphabet) {
0137             //The Alphabet means english alphabet.
0138             $pattern = '/[^a-zA-Z'  . $whiteSpace . ']/u';
0139         } else {
0140             //The Alphabet means each language's alphabet.
0141             $pattern = '/[^\p{L}' . $whiteSpace . ']/u';
0142         }
0143 
0144         return preg_replace($pattern, '', (string) $value);
0145     }
0146 }