[I18N] Zend_Filter/Zend_Validate:
[zend.git] / library / Zend / Filter / Alpha.php
blob22fa90b9017e682193fc59e4c60322f26f4eddca
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
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.
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$
22 /**
23 * @see Zend_Filter_Interface
25 require_once 'Zend/Filter/Interface.php';
26 /**
27 * @see Zend_Locale
29 require_once 'Zend/Locale.php';
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
37 class Zend_Filter_Alpha implements Zend_Filter_Interface
39 /**
40 * Whether to allow white space characters; off by default
42 * @var boolean
43 * @deprecated
45 public $allowWhiteSpace;
47 /**
48 * Is PCRE is compiled with UTF-8 and Unicode support
50 * @var mixed
51 **/
52 protected static $_unicodeEnabled;
54 /**
55 * Locale in browser.
57 * @var Zend_Locale object
59 protected $_locale;
61 /**
62 * The Alphabet means english alphabet.
64 * @var boolean
66 protected static $_meansEnglishAlphabet;
68 /**
69 * Sets default option values for this instance
71 * @param boolean $allowWhiteSpace
72 * @return void
74 public function __construct($allowWhiteSpace = false)
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;
86 $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
87 if (null === self::$_unicodeEnabled) {
88 self::$_unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false;
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')
101 * Returns the allowWhiteSpace option
103 * @return boolean
105 public function getAllowWhiteSpace()
107 return $this->allowWhiteSpace;
111 * Sets the allowWhiteSpace option
113 * @param boolean $allowWhiteSpace
114 * @return Zend_Filter_Alpha Provides a fluent interface
116 public function setAllowWhiteSpace($allowWhiteSpace)
118 $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
119 return $this;
123 * Defined by Zend_Filter_Interface
125 * Returns the string $value, removing all but alphabetic characters
127 * @param string $value
128 * @return string
130 public function filter($value)
132 $whiteSpace = $this->allowWhiteSpace ? '\s' : '';
133 if (!self::$_unicodeEnabled) {
134 // POSIX named classes are not supported, use alternative a-zA-Z match
135 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/';
136 } else if (self::$_meansEnglishAlphabet) {
137 //The Alphabet means english alphabet.
138 $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u';
139 } else {
140 //The Alphabet means each language's alphabet.
141 $pattern = '/[^\p{L}' . $whiteSpace . ']/u';
144 return preg_replace($pattern, '', (string) $value);