Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Alpha.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Filter/Interface.php';
26 
29 #require_once 'Zend/Locale.php';
30 
38 {
46 
52  protected static $_unicodeEnabled;
53 
59  protected $_locale;
60 
66  protected static $_meansEnglishAlphabet;
67 
74  public function __construct($allowWhiteSpace = false)
75  {
76  if ($allowWhiteSpace instanceof Zend_Config) {
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 
105  public function getAllowWhiteSpace()
106  {
107  return $this->allowWhiteSpace;
108  }
109 
117  {
118  $this->allowWhiteSpace = (boolean) $allowWhiteSpace;
119  return $this;
120  }
121 
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-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';
142  }
143 
144  return preg_replace($pattern, '', (string) $value);
145  }
146 }
filter($value)
Definition: Alpha.php:130
$pattern
Definition: website.php:22
static $_unicodeEnabled
Definition: Alpha.php:52
__construct($allowWhiteSpace=false)
Definition: Alpha.php:74
$value
Definition: gender.phtml:16
static $_meansEnglishAlphabet
Definition: Alpha.php:66
setAllowWhiteSpace($allowWhiteSpace)
Definition: Alpha.php:116