Skip to content

Instantly share code, notes, and snippets.

@sasezaki
Created July 6, 2012 14:11
Show Gist options
  • Save sasezaki/3060387 to your computer and use it in GitHub Desktop.
Save sasezaki/3060387 to your computer and use it in GitHub Desktop.
ZF2 Alpha/Alnum 's depend on Locale refactor
diff --git library/Zend/I18n/Filter/Alnum.php library/Zend/I18n/Filter/Alnum.php
index a18e4e6..c42d5c6 100644
--- library/Zend/I18n/Filter/Alnum.php
+++ library/Zend/I18n/Filter/Alnum.php
@@ -38,6 +38,7 @@ class Alnum extends AbstractLocale
protected $options = array(
'locale' => null,
'allow_white_space' => false,
+ 'depend_on_locale' => false,
);
/**
@@ -46,7 +47,7 @@ class Alnum extends AbstractLocale
* @param array|Traversable|boolean|null $allowWhiteSpaceOrOptions
* @param string|null $locale
*/
- public function __construct($allowWhiteSpaceOrOptions = null, $locale = null)
+ public function __construct($allowWhiteSpaceOrOptions = null, $locale = null, $depend_on_locale = false)
{
if ($allowWhiteSpaceOrOptions !== null) {
if (static::isOptions($allowWhiteSpaceOrOptions)){
@@ -54,6 +55,7 @@ class Alnum extends AbstractLocale
} else {
$this->setAllowWhiteSpace($allowWhiteSpaceOrOptions);
$this->setLocale($locale);
+ $this->setDependOnLocale($depend_on_locale);
}
}
}
@@ -81,6 +83,31 @@ class Alnum extends AbstractLocale
}
/**
+ * set depend on locale option
+ * if Locale's primary language is ja,ko,zh
+ * use means english Alphabet
+ */
+ public function setDependOnLocale($flag = true)
+ {
+ $this->options['depend_on_locale'] = (boolean) $flag;
+ return $this;
+ }
+
+ /**
+ *
+ * @return boolean
+ */
+ public function meansEnglishAlphabet()
+ {
+ if ($this->options['depend_on_locale']) {
+ $language = Locale::getPrimaryLanguage($this->getLocale());
+ return (boolean) ($language == 'ja'|| $language == 'ko' || $language == 'zh');
+ }
+
+ return false;
+ }
+
+ /**
* Defined by Zend\Filter\FilterInterface
*
* Returns $value as string with all non-alphanumeric characters removed
@@ -91,12 +118,11 @@ class Alnum extends AbstractLocale
public function filter($value)
{
$whiteSpace = $this->options['allow_white_space'] ? '\s' : '';
- $language = Locale::getPrimaryLanguage($this->getLocale());
if (!static::hasPcreUnicodeSupport()) {
// POSIX named classes are not supported, use alternative a-zA-Z0-9 match
$pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/';
- } elseif ($language == 'ja'|| $language == 'ko' || $language == 'zh') {
+ } elseif ($this->meansEnglishAlphabet()) {
// Use english alphabet
$pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/u';
} else {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment