diff --git a/src/Translator/Plural/Rule.php b/src/Translator/Plural/Rule.php index 05becd51..a0660d24 100644 --- a/src/Translator/Plural/Rule.php +++ b/src/Translator/Plural/Rule.php @@ -182,8 +182,8 @@ protected function evaluateAstPart(array $ast, $number) */ public static function fromString($string) { - if (self::$parser === null) { - self::$parser = new Parser(); + if (static::$parser === null) { + static::$parser = new Parser(); } if (!preg_match('(nplurals=(?P\d+))', $string, $match)) { @@ -202,8 +202,8 @@ public static function fromString($string) )); } - $tree = self::$parser->parse($match['plural']); - $ast = self::createAst($tree); + $tree = static::$parser->parse($match['plural']); + $ast = static::createAst($tree); return new self($numPlurals, $ast); } @@ -230,18 +230,18 @@ protected static function createAst(Symbol $symbol) break; case '!': - $ast['arguments'][] = self::createAst($symbol->first); + $ast['arguments'][] = static::createAst($symbol->first); break; case '?': - $ast['arguments'][] = self::createAst($symbol->first); - $ast['arguments'][] = self::createAst($symbol->second); - $ast['arguments'][] = self::createAst($symbol->third); + $ast['arguments'][] = static::createAst($symbol->first); + $ast['arguments'][] = static::createAst($symbol->second); + $ast['arguments'][] = static::createAst($symbol->third); break; default: - $ast['arguments'][] = self::createAst($symbol->first); - $ast['arguments'][] = self::createAst($symbol->second); + $ast['arguments'][] = static::createAst($symbol->first); + $ast['arguments'][] = static::createAst($symbol->second); break; } diff --git a/src/Translator/TranslatorAwareTrait.php b/src/Translator/TranslatorAwareTrait.php new file mode 100644 index 00000000..b6acf183 --- /dev/null +++ b/src/Translator/TranslatorAwareTrait.php @@ -0,0 +1,120 @@ +translator = $translator; + + if (!is_null($textDomain)) { + $this->setTranslatorTextDomain($textDomain); + } + + return $this; + } + + /** + * Returns translator used in object + * + * @return Translator + */ + public function getTranslator() + { + return $this->translator; + } + + /** + * Checks if the object has a translator + * + * @return bool + */ + public function hasTranslator() + { + return !is_null($this->translator); + } + + /** + * Sets whether translator is enabled and should be used + * + * @param bool $enabled + * @return mixed + */ + public function setTranslatorEnabled($enabled = true) + { + $this->translatorEnabled = $enabled; + + return $this; + } + + /** + * Returns whether translator is enabled and should be used + * + * @return bool + */ + public function isTranslatorEnabled() + { + return $this->translatorEnabled; + } + + /** + * Set translation text domain + * + * @param string $textDomain + * @return mixed + */ + public function setTranslatorTextDomain($textDomain = 'default') + { + $this->translatorTextDomain = $textDomain; + + return $this; + } + + /** + * Return the translation text domain + * + * @return string + */ + public function getTranslatorTextDomain() + { + return $this->translatorTextDomain; + } +} diff --git a/src/Validator/Alnum.php b/src/Validator/Alnum.php index 1f08ab6e..b3b2ab76 100644 --- a/src/Validator/Alnum.php +++ b/src/Validator/Alnum.php @@ -107,13 +107,13 @@ public function isValid($value) return false; } - if (null === self::$filter) { - self::$filter = new AlnumFilter(); + if (null === static::$filter) { + static::$filter = new AlnumFilter(); } - self::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']); + static::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']); - if ($value != self::$filter->filter($value)) { + if ($value != static::$filter->filter($value)) { $this->error(self::NOT_ALNUM); return false; } diff --git a/src/Validator/Alpha.php b/src/Validator/Alpha.php index 53c96adf..0c62cfdb 100644 --- a/src/Validator/Alpha.php +++ b/src/Validator/Alpha.php @@ -70,14 +70,14 @@ public function isValid($value) return false; } - if (null === self::$filter) { - self::$filter = new AlphaFilter(); + if (null === static::$filter) { + static::$filter = new AlphaFilter(); } - //self::$filter->setAllowWhiteSpace($this->allowWhiteSpace); - self::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']); + //static::$filter->setAllowWhiteSpace($this->allowWhiteSpace); + static::$filter->setAllowWhiteSpace($this->options['allowWhiteSpace']); - if ($value !== self::$filter->filter($value)) { + if ($value !== static::$filter->filter($value)) { $this->error(self::NOT_ALPHA); return false; } diff --git a/src/Validator/PostCode.php b/src/Validator/PostCode.php index 6aad8eaf..094c20ab 100644 --- a/src/Validator/PostCode.php +++ b/src/Validator/PostCode.php @@ -344,8 +344,8 @@ public function isValid($value) if ('' === $region) { throw new Exception\InvalidArgumentException("Locale must contain a region"); } - if (isset(self::$postCodeRegex[$region])) { - $format = self::$postCodeRegex[$region]; + if (isset(static::$postCodeRegex[$region])) { + $format = static::$postCodeRegex[$region]; } } if (null === $format || '' === $format) { diff --git a/test/Translator/TranslatorAwareTraitTest.php b/test/Translator/TranslatorAwareTraitTest.php new file mode 100644 index 00000000..acaa62cf --- /dev/null +++ b/test/Translator/TranslatorAwareTraitTest.php @@ -0,0 +1,129 @@ +getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertAttributeEquals(null, 'translator', $object); + + $translator = new Translator; + + $object->setTranslator($translator); + + $this->assertAttributeEquals($translator, 'translator', $object); + } + + public function testSetTranslatorAndTextDomain() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertAttributeEquals(null, 'translator', $object); + $this->assertAttributeEquals('default', 'translatorTextDomain', $object); + + $translator = new Translator; + $textDomain = 'domain'; + + $object->setTranslator($translator, $textDomain); + + $this->assertAttributeEquals($translator, 'translator', $object); + $this->assertAttributeEquals($textDomain, 'translatorTextDomain', $object); + } + + public function testGetTranslator() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertNull($object->getTranslator()); + + $translator = new Translator; + + $object->setTranslator($translator); + + $this->assertEquals($translator, $object->getTranslator()); + } + + public function testHasTranslator() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertFalse($object->hasTranslator()); + + $translator = new Translator; + + $object->setTranslator($translator); + + $this->assertTrue($object->hasTranslator()); + } + + public function testSetTranslatorEnabled() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertAttributeEquals(true, 'translatorEnabled', $object); + + $enabled = false; + + $object->setTranslatorEnabled($enabled); + + $this->assertAttributeEquals($enabled, 'translatorEnabled', $object); + + $object->setTranslatorEnabled(); + + $this->assertAttributeEquals(true, 'translatorEnabled', $object); + } + + public function testIsTranslatorEnabled() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertTrue($object->isTranslatorEnabled()); + + $object->setTranslatorEnabled(false); + + $this->assertFalse($object->isTranslatorEnabled()); + } + + public function testSetTranslatorTextDomain() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertAttributeEquals('default', 'translatorTextDomain', $object); + + $textDomain = 'domain'; + + $object->setTranslatorTextDomain($textDomain); + + $this->assertAttributeEquals($textDomain, 'translatorTextDomain', $object); + } + + public function testGetTranslatorTextDomain() + { + $object = $this->getObjectForTrait('\Zend\I18n\Translator\TranslatorAwareTrait'); + + $this->assertEquals('default', $object->getTranslatorTextDomain()); + + $textDomain = 'domain'; + + $object->setTranslatorTextDomain($textDomain); + + $this->assertEquals($textDomain, $object->getTranslatorTextDomain()); + } +} diff --git a/test/View/Helper/DateFormatTest.php b/test/View/Helper/DateFormatTest.php index 58a541ec..157cd901 100644 --- a/test/View/Helper/DateFormatTest.php +++ b/test/View/Helper/DateFormatTest.php @@ -243,15 +243,15 @@ public function testDefaultLocale() { $this->assertEquals(Locale::getDefault(), $this->helper->getLocale()); } - + public function testBugTwoPatternOnSameHelperInstance() { - $date = new DateTime('2012-07-02T22:44:03Z'); - - $helper = new DateFormatHelper(); + $date = new DateTime('2012-07-02T22:44:03Z'); + + $helper = new DateFormatHelper(); $helper->setTimezone('Europe/Berlin'); - $this->assertEquals('03/2012', $helper->__invoke($date, null, null, 'it_IT', 'dd/Y')); - $this->assertEquals('03-2012', $helper->__invoke($date, null, null, 'it_IT', 'dd-Y')); + $this->assertEquals('03/2012', $helper->__invoke($date, null, null, 'it_IT', 'dd/Y')); + $this->assertEquals('03-2012', $helper->__invoke($date, null, null, 'it_IT', 'dd-Y')); } public function assertMbStringEquals($expected, $test, $message = '')