web/Zend/Filter/Alnum.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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: Alnum.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Filter_Interface
       
    24  */
       
    25 require_once 'Zend/Filter/Interface.php';
       
    26 /**
       
    27  * @see Zend_Locale
       
    28  */
       
    29 require_once 'Zend/Locale.php';
       
    30 
       
    31 /**
       
    32  * @category   Zend
       
    33  * @package    Zend_Filter
       
    34  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    35  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    36  */
       
    37 class Zend_Filter_Alnum implements Zend_Filter_Interface
       
    38 {
       
    39     /**
       
    40      * Whether to allow white space characters; off by default
       
    41      *
       
    42      * @var boolean
       
    43      * @deprecated
       
    44      */
       
    45     public $allowWhiteSpace;
       
    46 
       
    47     /**
       
    48      * Is PCRE is compiled with UTF-8 and Unicode support
       
    49      *
       
    50      * @var mixed
       
    51      **/
       
    52     protected static $_unicodeEnabled;
       
    53 
       
    54     /**
       
    55      * Locale in browser.
       
    56      *
       
    57      * @var Zend_Locale object
       
    58      */
       
    59     protected $_locale;
       
    60 
       
    61     /**
       
    62      * The Alphabet means english alphabet.
       
    63      *
       
    64      * @var boolean
       
    65      */
       
    66     protected static $_meansEnglishAlphabet;
       
    67 
       
    68     /**
       
    69      * Sets default option values for this instance
       
    70      *
       
    71      * @param  boolean $allowWhiteSpace
       
    72      * @return void
       
    73      */
       
    74     public function __construct($allowWhiteSpace = false)
       
    75     {
       
    76         if ($allowWhiteSpace instanceof Zend_Config) {
       
    77             $allowWhiteSpace = $allowWhiteSpace->toArray();
       
    78         } else if (is_array($allowWhiteSpace)) {
       
    79             if (array_key_exists('allowwhitespace', $allowWhiteSpace)) {
       
    80                 $allowWhiteSpace = $allowWhiteSpace['allowwhitespace'];
       
    81             } else {
       
    82                 $allowWhiteSpace = false;
       
    83             }
       
    84         }
       
    85 
       
    86         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
       
    87         if (null === self::$_unicodeEnabled) {
       
    88             self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
       
    89         }
       
    90 
       
    91         if (null === self::$_meansEnglishAlphabet) {
       
    92             $this->_locale = new Zend_Locale('auto');
       
    93             self::$_meansEnglishAlphabet = in_array($this->_locale->getLanguage(),
       
    94                                                     array('ja', 'ko', 'zh')
       
    95                                                     );
       
    96         }
       
    97 
       
    98     }
       
    99 
       
   100     /**
       
   101      * Returns the allowWhiteSpace option
       
   102      *
       
   103      * @return boolean
       
   104      */
       
   105     public function getAllowWhiteSpace()
       
   106     {
       
   107         return $this->allowWhiteSpace;
       
   108     }
       
   109 
       
   110     /**
       
   111      * Sets the allowWhiteSpace option
       
   112      *
       
   113      * @param boolean $allowWhiteSpace
       
   114      * @return Zend_Filter_Alnum Provides a fluent interface
       
   115      */
       
   116     public function setAllowWhiteSpace($allowWhiteSpace)
       
   117     {
       
   118         $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
       
   119         return $this;
       
   120     }
       
   121 
       
   122     /**
       
   123      * Defined by Zend_Filter_Interface
       
   124      *
       
   125      * Returns the string $value, removing all but alphabetic and digit characters
       
   126      *
       
   127      * @param  string $value
       
   128      * @return string
       
   129      */
       
   130     public function filter($value)
       
   131     {
       
   132         $whiteSpace = $this->allowWhiteSpace ? '\s' : '';
       
   133         if (!self::$_unicodeEnabled) {
       
   134             // POSIX named classes are not supported, use alternative a-zA-Z0-9 match
       
   135             $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/';
       
   136         } else if (self::$_meansEnglishAlphabet) {
       
   137             //The Alphabet means english alphabet.
       
   138             $pattern = '/[^a-zA-Z0-9'  . $whiteSpace . ']/u';
       
   139         } else {
       
   140             //The Alphabet means each language's alphabet.
       
   141             $pattern = '/[^\p{L}\p{N}' . $whiteSpace . ']/u';
       
   142         }
       
   143 
       
   144         return preg_replace($pattern, '', (string) $value);
       
   145     }
       
   146 }