diff --git a/src/AbstractLocale.php b/src/AbstractLocale.php deleted file mode 100644 index 998cd897..00000000 --- a/src/AbstractLocale.php +++ /dev/null @@ -1,57 +0,0 @@ -options['locale'] = $locale; - return $this; - } - - /** - * Returns the locale option - * - * @return string - */ - public function getLocale() - { - if (!isset($this->options['locale'])) { - $this->options['locale'] = Locale::getDefault(); - } - return $this->options['locale']; - } -} diff --git a/src/Alnum.php b/src/Alnum.php deleted file mode 100644 index 58b28379..00000000 --- a/src/Alnum.php +++ /dev/null @@ -1,109 +0,0 @@ - null, - 'allow_white_space' => false, - ); - - /** - * Sets default option values for this instance - * - * @param array|Traversable|boolean|null $allowWhiteSpaceOrOptions - * @param string|null $locale - */ - public function __construct($allowWhiteSpaceOrOptions = null, $locale = null) - { - if ($allowWhiteSpaceOrOptions !== null) { - if (static::isOptions($allowWhiteSpaceOrOptions)){ - $this->setOptions($allowWhiteSpaceOrOptions); - } else { - $this->setAllowWhiteSpace($allowWhiteSpaceOrOptions); - $this->setLocale($locale); - } - } - } - - /** - * Sets the allowWhiteSpace option - * - * @param boolean $flag - * @return Alnum Provides a fluent interface - */ - public function setAllowWhiteSpace($flag = true) - { - $this->options['allow_white_space'] = (boolean) $flag; - return $this; - } - - /** - * Whether white space is allowed - * - * @return boolean - */ - public function getAllowWhiteSpace() - { - return $this->options['allow_white_space']; - } - - /** - * Defined by Zend\Filter\FilterInterface - * - * Returns $value as string with all non-alphanumeric characters removed - * - * @param mixed $value - * @return string - */ - 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') { - // Use english alphabet - $pattern = '/[^a-zA-Z0-9' . $whiteSpace . ']/u'; - } else { - // Use native language alphabet - $pattern = '/[^\p{L}\p{N}' . $whiteSpace . ']/u'; - } - - return preg_replace($pattern, '', (string) $value); - } -} diff --git a/src/Alpha.php b/src/Alpha.php deleted file mode 100644 index 3670467d..00000000 --- a/src/Alpha.php +++ /dev/null @@ -1,59 +0,0 @@ -options['allow_white_space'] ? '\s' : ''; - $language = Locale::getPrimaryLanguage($this->getLocale()); - - if (!static::hasPcreUnicodeSupport()) { - // POSIX named classes are not supported, use alternative [a-zA-Z] match - $pattern = '/[^a-zA-Z' . $whiteSpace . ']/'; - } elseif ($language == 'ja' || $language == 'ko' || $language == 'zh') { - // Use english alphabet - $pattern = '/[^a-zA-Z' . $whiteSpace . ']/u'; - } else { - // Use native language alphabet - $pattern = '/[^\p{L}' . $whiteSpace . ']/u'; - } - - return preg_replace($pattern, '', (string) $value); - } -} diff --git a/src/Boolean.php b/src/Boolean.php index 415b2eef..85b2c8cc 100644 --- a/src/Boolean.php +++ b/src/Boolean.php @@ -23,14 +23,13 @@ use Traversable; use Zend\Stdlib\ArrayUtils; - /** * @category Zend * @package Zend_Filter * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class Boolean extends AbstractLocale +class Boolean extends AbstractFilter { const TYPE_BOOLEAN = 1; const TYPE_INTEGER = 2; @@ -67,35 +66,38 @@ class Boolean extends AbstractLocale protected $options = array( 'type' => self::TYPE_PHP, 'casting' => true, - 'locale' => null, 'translations' => array(), ); /** * Constructor * - * @param string|array|Traversable $options OPTIONAL + * @param array|Traversable|int|null $typeOrOptions + * @param bool $casting + * @param array $translations */ - public function __construct($options = null) + public function __construct($typeOrOptions = null, $casting = true, $translations = array()) { - if ($options !== null) { - if ($options instanceof Traversable) { - $options = ArrayUtils::iteratorToArray($options); + if ($typeOrOptions !== null) { + if ($typeOrOptions instanceof Traversable) { + $typeOrOptions = ArrayUtils::iteratorToArray($typeOrOptions); } - if (!is_array($options)) { - $args = func_get_args(); - if (isset($args[0])) { - $this->setType($args[0]); - } - if (isset($args[1])) { - $this->setCasting($args[1]); - } - if (isset($args[2])) { - $this->setLocale($args[2]); + if (is_array($typeOrOptions)) { + if (isset($typeOrOptions['type']) + || isset($typeOrOptions['casting']) + || isset($typeOrOptions['translations'])) + { + $this->setOptions($typeOrOptions); + } else { + $this->setType($typeOrOptions); + $this->setCasting($casting); + $this->setTranslations($translations); } } else { - $this->setOptions($options); + $this->setType($typeOrOptions); + $this->setCasting($casting); + $this->setTranslations($translations); } } } @@ -171,14 +173,23 @@ public function getCasting() } /** - * @param array $translations + * @param array|Traversable $translations * @return Boolean */ - public function setTranslations(array $translations) + public function setTranslations($translations) { - foreach ($translations as $locale => $translation) { - $this->addTranslation($locale, $translation); + if (!is_array($translations) && !$translations instanceof Traversable) { + throw new Exception\InvalidArgumentException(sprintf( + '"%s" expects an array or Traversable; received "%s"', + __METHOD__, + (is_object($translations) ? get_class($translations) : gettype($translations)) + )); } + + foreach ($translations as $message => $flag) { + $this->options['translations'][$message] = (bool) $flag; + } + return $this; } @@ -190,21 +201,6 @@ public function getTranslations() return $this->options['translations']; } - /** - * @param string $locale - * @param array $translation - * @return Boolean - * @throws Exception\InvalidArgumentException - */ - public function addTranslation($locale, array $translation) - { - foreach ($translation as $message => $flag) { - $this->options['translations'][$locale][$message] = (bool) $flag; - } - - return $this; - } - /** * Defined by Zend\Filter\FilterInterface * @@ -222,9 +218,8 @@ public function filter($value) if ($type >= self::TYPE_LOCALIZED) { $type -= self::TYPE_LOCALIZED; if (is_string($value)) { - $locale = $this->getLocale(); - if (isset($this->options['translations'][$locale][$value])) { - return (bool) $this->options['translations'][$locale][$value]; + if (isset($this->options['translations'][$value])) { + return (bool) $this->options['translations'][$value]; } } } diff --git a/src/FilterPluginManager.php b/src/FilterPluginManager.php index 9c56eab6..64108b59 100644 --- a/src/FilterPluginManager.php +++ b/src/FilterPluginManager.php @@ -42,8 +42,8 @@ class FilterPluginManager extends AbstractPluginManager * @var array */ protected $invokableClasses = array( - 'alnum' => 'Zend\Filter\Alnum', - 'alpha' => 'Zend\Filter\Alpha', + 'alnum' => 'Zend\I18n\Filter\Alnum', + 'alpha' => 'Zend\I18n\Filter\Alpha', 'basename' => 'Zend\Filter\BaseName', 'boolean' => 'Zend\Filter\Boolean', 'callback' => 'Zend\Filter\Callback', diff --git a/src/Null.php b/src/Null.php index f3b9704a..405f9fa4 100644 --- a/src/Null.php +++ b/src/Null.php @@ -64,17 +64,21 @@ class Null extends AbstractFilter * * @param string|array|Traversable $options OPTIONAL */ - public function __construct($options = null) + public function __construct($typeOrOptions = null) { - if ($options !== null) { - if ($options instanceof Traversable) { - $options = iterator_to_array($options); + if ($typeOrOptions !== null) { + if ($typeOrOptions instanceof Traversable) { + $typeOrOptions = iterator_to_array($typeOrOptions); } - if (!is_array($options)) { - $this->setType($options); + if (is_array($typeOrOptions)) { + if (isset($typeOrOptions['type'])) { + $this->setOptions($typeOrOptions); + } else { + $this->setType($typeOrOptions); + } } else { - $this->setOptions($options); + $this->setType($typeOrOptions); } } } diff --git a/test/AlnumTest.php b/test/AlnumTest.php deleted file mode 100644 index 411f6d78..00000000 --- a/test/AlnumTest.php +++ /dev/null @@ -1,174 +0,0 @@ -filter = new AlnumFilter(); - - $this->locale = Locale::getDefault(); - $language = Locale::getPrimaryLanguage($this->locale); - self::$meansEnglishAlphabet = in_array($language, array('ja')); - self::$unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; - } - - /** - * Ensures that the filter follows expected behavior - * - * @return void - */ - public function testBasic() - { - if (!self::$unicodeEnabled) { - // POSIX named classes are not supported, use alternative a-zA-Z match - $valuesExpected = array( - 'abc123' => 'abc123', - 'abc 123' => 'abc123', - 'abcxyz' => 'abcxyz', - 'AZ@#4.3' => 'AZ43', - '' => '' - ); - } elseif (self::$meansEnglishAlphabet) { - // The Alphabet means english alphabet. - - /** - * The first element contains multibyte alphabets and digits. - * But , AlnumFilter is expected to return only singlebyte alphabets and digits. - * - * The second contains multibyte or singebyte space. - * The third contains various multibyte or singebyte characters. - */ - $valuesExpected = array( - 'aABb3456' => 'aB35', - 'z7 Y8 x9' => 'z8x', - ',s1.2r3#:q,' => 's12rq', - ); - } else { - //The Alphabet means each language's alphabet. - $valuesExpected = array( - 'abc123' => 'abc123', - 'abc 123' => 'abc123', - 'abcxyz' => 'abcxyz', - 'če2t3ně' => 'če2t3ně', - 'grz5e4gżółka' => 'grz5e4gżółka', - 'Be3l5gië' => 'Be3l5gië', - '' => '' - ); - } - - foreach ($valuesExpected as $input => $expected) { - $actual = $this->filter->filter($input); - $this->assertEquals($expected, $actual); - } - } - - /** - * Ensures that the allowWhiteSpace option works as expected - * - * @return void - */ - public function testAllowWhiteSpace() - { - $this->filter->setAllowWhiteSpace(true); - - if (!self::$unicodeEnabled) { - // POSIX named classes are not supported, use alternative a-zA-Z match - $valuesExpected = array( - 'abc123' => 'abc123', - 'abc 123' => 'abc 123', - 'abcxyz' => 'abcxyz', - 'AZ@#4.3' => 'AZ43', - '' => '', - "\n" => "\n", - " \t " => " \t " - ); - } elseif (self::$meansEnglishAlphabet) { - //The Alphabet means english alphabet. - $valuesExpected = array( - 'a B 45' => 'a B 5', - 'z3 x' => 'z3x' - ); - } else { - //The Alphabet means each language's alphabet. - $valuesExpected = array( - 'abc123' => 'abc123', - 'abc 123' => 'abc 123', - 'abcxyz' => 'abcxyz', - 'če2 t3ně' => 'če2 t3ně', - 'gr z5e4gżółka' => 'gr z5e4gżółka', - 'Be3l5 gië' => 'Be3l5 gië', - '' => '', - ); - } - - foreach ($valuesExpected as $input => $expected) { - $actual = $this->filter->filter($input); - $this->assertEquals($expected, $actual); - } - } -} diff --git a/test/AlphaTest.php b/test/AlphaTest.php deleted file mode 100644 index 2e16ebaf..00000000 --- a/test/AlphaTest.php +++ /dev/null @@ -1,178 +0,0 @@ -filter = new AlphaFilter(); - - $this->locale = Locale::getDefault(); - $language = Locale::getPrimaryLanguage($this->locale); - self::$meansEnglishAlphabet = in_array($language, array('ja')); - self::$unicodeEnabled = (@preg_match('/\pL/u', 'a')) ? true : false; - } - - /** - * Ensures that the filter follows expected behavior - * - * @return void - */ - public function testBasic() - { - if (!self::$unicodeEnabled) { - // POSIX named classes are not supported, use alternative a-zA-Z match - $valuesExpected = array( - 'abc123' => 'abc', - 'abc 123' => 'abc', - 'abcxyz' => 'abcxyz', - '' => '' - ); - } elseif (self::$meansEnglishAlphabet) { - //The Alphabet means english alphabet. - /** - * The first element contains multibyte alphabets. - * But , AlphaFilter is expected to return only singlebyte alphabets. - * The second contains multibyte or singlebyte space. - * The third contains multibyte or singlebyte digits. - * The forth contains various multibyte or singlebyte characters. - * The last contains only singlebyte alphabets. - */ - $valuesExpected = array( - 'aABbc' => 'aBc', - 'z Y x' => 'zx', - 'W1v3U4t' => 'vt', - ',sй.rλ:qν_p' => 'srqp', - 'onml' => 'onml' - ); - } else { - //The Alphabet means each language's alphabet. - $valuesExpected = array( - 'abc123' => 'abc', - 'abc 123' => 'abc', - 'abcxyz' => 'abcxyz', - 'četně' => 'četně', - 'لعربية' => 'لعربية', - 'grzegżółka' => 'grzegżółka', - 'België' => 'België', - '' => '' - ); - } - - foreach ($valuesExpected as $input => $expected) { - $actual = $this->filter->filter($input); - $this->assertEquals($expected, $actual); - } - } - - /** - * Ensures that the filter follows expected behavior - * - * @return void - */ - public function testAllowWhiteSpace() - { - $this->filter->setAllowWhiteSpace(true); - - if (!self::$unicodeEnabled) { - // POSIX named classes are not supported, use alternative a-zA-Z match - $valuesExpected = array( - 'abc123' => 'abc', - 'abc 123' => 'abc ', - 'abcxyz' => 'abcxyz', - '' => '', - "\n" => "\n", - " \t " => " \t " - ); - } if (self::$meansEnglishAlphabet) { - //The Alphabet means english alphabet. - $valuesExpected = array( - 'a B' => 'a B', - 'zY x' => 'zx' - ); - } else { - //The Alphabet means each language's alphabet. - $valuesExpected = array( - 'abc123' => 'abc', - 'abc 123' => 'abc ', - 'abcxyz' => 'abcxyz', - 'četně' => 'četně', - 'لعربية' => 'لعربية', - 'grzegżółka' => 'grzegżółka', - 'België' => 'België', - '' => '', - "\n" => "\n", - " \t " => " \t " - ); - } - - foreach ($valuesExpected as $input => $expected) { - $actual = $this->filter->filter($input); - $this->assertEquals($expected, $actual); - } - } -} diff --git a/test/BooleanTest.php b/test/BooleanTest.php index 28bf342a..0c40b87e 100644 --- a/test/BooleanTest.php +++ b/test/BooleanTest.php @@ -22,7 +22,6 @@ namespace ZendTest\Filter; use Zend\Filter\Boolean as BooleanFilter; -use Zend\Locale\Locale; /** * @category Zend @@ -37,22 +36,20 @@ class BooleanTest extends \PHPUnit_Framework_TestCase public function testConstructorOptions() { $filter = new BooleanFilter(array( - 'type' => BooleanFilter::TYPE_INTEGER, + 'type' => BooleanFilter::TYPE_INTEGER, 'casting' => false, - 'locale' => 'en_EN', )); + $this->assertEquals(BooleanFilter::TYPE_INTEGER, $filter->getType()); - $this->assertEquals(false, $filter->getCasting()); - $this->assertEquals('en_EN', $filter->getLocale()); + $this->assertFalse($filter->getCasting()); } public function testConstructorParams() { - $filter = new BooleanFilter(BooleanFilter::TYPE_INTEGER, false, 'en_EN'); + $filter = new BooleanFilter(BooleanFilter::TYPE_INTEGER, false); $this->assertEquals(BooleanFilter::TYPE_INTEGER, $filter->getType()); - $this->assertEquals(false, $filter->getCasting()); - $this->assertEquals('en_EN', $filter->getLocale()); + $this->assertFalse($filter->getCasting()); } /** @@ -125,28 +122,19 @@ public function testLocalized() { $filter = new BooleanFilter(array( 'type' => BooleanFilter::TYPE_LOCALIZED, - 'locale' => 'en_EN', 'translations' => array( - 'en_EN' => array( - 'yes' => true, - 'y' => true, - 'no' => false, - 'n' => false, - ), - 'en_US' => array( - 'yay' => true, - 'yes' => true, - 'nay' => false, - 'no' => false, - ), - ), + 'yes' => true, + 'y' => true, + 'no' => false, + 'n' => false, + 'yay' => true, + 'nay' => false, + ) )); $this->assertTrue($filter->filter('yes')); - $this->assertFalse($filter->filter('n')); - - $filter->setLocale('en_US'); $this->assertTrue($filter->filter('yay')); + $this->assertFalse($filter->filter('n')); $this->assertFalse($filter->filter('nay')); } diff --git a/test/CompressTest.php b/test/CompressTest.php index 56f2848f..593f4d24 100644 --- a/test/CompressTest.php +++ b/test/CompressTest.php @@ -207,7 +207,7 @@ public function testSetAdapter() $this->assertEquals('Gz', $filter->getAdapterName()); - $filter->setAdapter('\Zend\Filter\Alnum'); + $filter->setAdapter('\Zend\Filter\Boolean'); $this->setExpectedException('\Zend\Filter\Exception\InvalidArgumentException', 'does not implement'); $adapter = $filter->getAdapter();