diff --git a/src/View/Helper/DateFormat.php b/src/View/Helper/DateFormat.php index 279f0b5c..da40cddd 100644 --- a/src/View/Helper/DateFormat.php +++ b/src/View/Helper/DateFormat.php @@ -105,18 +105,19 @@ public function getlocale() /** * Format a date. * - * @param DateTime|integer|array $date - * @param integer $dateType - * @param integer $timeType - * @param string $locale + * @param DateTime|integer|array $date + * @param int $dateType + * @param int $timeType + * @param string $locale + * @param string|null $pattern * @return string - * @throws Exception\RuntimeException */ public function __invoke( $date, $dateType = IntlDateFormatter::NONE, $timeType = IntlDateFormatter::NONE, - $locale = null + $locale = null, + $pattern = null ) { if ($locale === null) { $locale = $this->getlocale(); @@ -130,7 +131,9 @@ public function __invoke( $locale, $dateType, $timeType, - $timezone + $timezone, + IntlDateFormatter::GREGORIAN, + $pattern ); } diff --git a/src/View/Helper/Plural.php b/src/View/Helper/Plural.php new file mode 100644 index 00000000..10faf2a0 --- /dev/null +++ b/src/View/Helper/Plural.php @@ -0,0 +1,84 @@ +rule = $pluralRule; + + return $this; + } + + /** + * Given an array of strings, a number and, if wanted, an optional locale (the default one is used + * otherwise), this picks the right string according to plural rules of the locale + * + * @param array|string $strings + * @param int $number + * @throws Exception\InvalidArgumentException + * @return string + */ + public function __invoke($strings, $number) + { + if ($this->rule === null) { + throw new Exception\InvalidArgumentException(sprintf( + 'No plural rule was set' + )); + } + + if (!is_array($strings)) { + $strings = (array) $strings; + } + + $pluralIndex = $this->rule->evaluate($number); + + return $strings[$pluralIndex]; + } +} diff --git a/src/View/HelperConfig.php b/src/View/HelperConfig.php index 3dfe2782..2034f194 100644 --- a/src/View/HelperConfig.php +++ b/src/View/HelperConfig.php @@ -29,6 +29,7 @@ class HelperConfig implements ConfigInterface 'currencyformat' => 'Zend\I18n\View\Helper\CurrencyFormat', 'dateformat' => 'Zend\I18n\View\Helper\DateFormat', 'numberformat' => 'Zend\I18n\View\Helper\NumberFormat', + 'plural' => 'Zend\I18n\View\Helper\Plural', 'translate' => 'Zend\I18n\View\Helper\Translate', 'translateplural' => 'Zend\I18n\View\Helper\TranslatePlural', ); diff --git a/test/Validator/FloatTest.php b/test/Validator/FloatTest.php index c9f82e27..802e800b 100644 --- a/test/Validator/FloatTest.php +++ b/test/Validator/FloatTest.php @@ -59,6 +59,7 @@ public function basicProvider() array(0.01, true), array(-0.1, true), array('10.1', true), + array('5.00', true), array('10.0', true), array('10.10', true), array(1, true), diff --git a/test/View/Helper/DateFormatTest.php b/test/View/Helper/DateFormatTest.php index 38873d57..4ed864ec 100644 --- a/test/View/Helper/DateFormatTest.php +++ b/test/View/Helper/DateFormatTest.php @@ -53,7 +53,7 @@ public function tearDown() unset($this->helper); } - public function currencyTestsDataProvider() + public function dateTestsDataProvider() { $date = new DateTime('2012-07-02T22:44:03Z'); return array( @@ -160,19 +160,62 @@ public function currencyTestsDataProvider() ); } + public function dateTestsDataProviderWithPattern() + { + $date = new DateTime('2012-07-02T22:44:03Z'); + return array( + // FULL format varies based on OS + // array( + // 'de_DE', + // 'Europe/Berlin', + // IntlDateFormatter::FULL, + // IntlDateFormatter::FULL, + // $date, + // 'Dienstag, 3. Juli 2012 00:44:03 Deutschland', + // ), + array( + 'de_DE', + 'Europe/Berlin', + null, + null, + 'MMMM', + $date, + 'Juli', + ), + array( + 'de_DE', + 'Europe/Berlin', + null, + null, + 'MMMM.Y', + $date, + 'Juli.2012', + ), + array( + 'de_DE', + 'Europe/Berlin', + null, + null, + 'dd/Y', + $date, + '03/2012', + ), + ); + } + /** - * @dataProvider currencyTestsDataProvider + * @dataProvider dateTestsDataProvider */ public function testBasic($locale, $timezone, $timeType, $dateType, $date, $expected) { $this->helper->setTimezone($timezone); $this->assertMbStringEquals($expected, $this->helper->__invoke( - $date, $dateType, $timeType, $locale + $date, $dateType, $timeType, $locale, null )); } /** - * @dataProvider currencyTestsDataProvider + * @dataProvider dateTestsDataProvider */ public function testSettersProvideDefaults($locale, $timezone, $timeType, $dateType, $date, $expected) { @@ -185,6 +228,17 @@ public function testSettersProvideDefaults($locale, $timezone, $timeType, $dateT )); } + /** + * @dataProvider dateTestsDataProviderWithPattern + */ + public function testUseCustomPattern($locale, $timezone, $timeType, $dateType, $pattern, $date, $expected) + { + $this->helper->setTimezone($timezone); + $this->assertMbStringEquals($expected, $this->helper->__invoke( + $date, $dateType, $timeType, $locale, $pattern + )); + } + public function testDefaultLocale() { $this->assertEquals(Locale::getDefault(), $this->helper->getLocale()); diff --git a/test/View/Helper/PluralTest.php b/test/View/Helper/PluralTest.php new file mode 100644 index 00000000..dbb0d37b --- /dev/null +++ b/test/View/Helper/PluralTest.php @@ -0,0 +1,68 @@ +helper = new PluralHelper(); + } + + /** + * @return array + */ + public function pluralsTestProvider() + { + return array( + array('nplurals=1; plural=0', 'かさ', 0, 'かさ'), + array('nplurals=1; plural=0', 'かさ', 10, 'かさ'), + + array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 0, 'umbrellas'), + array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 1, 'umbrella'), + array('nplurals=2; plural=(n==1 ? 0 : 1)', array('umbrella', 'umbrellas'), 2, 'umbrellas'), + + array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 0, 'parapluie'), + array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 1, 'parapluie'), + array('nplurals=2; plural=(n==0 || n==1 ? 0 : 1)', array('parapluie', 'parapluies'), 2, 'parapluies'), + ); + } + + /** + * @dataProvider pluralsTestProvider + */ + public function testGetCorrectPlurals($pluralRule, $strings, $number, $expected) + { + $this->helper->setPluralRule($pluralRule); + $result = $this->helper->__invoke($strings, $number); + $this->assertEquals($expected, $result); + } +}