Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge commit 'refs/pull/2936/head' of github.com:zendframework/zf2 in…
Browse files Browse the repository at this point in the history
…to hotfix/ssl-case-sensitive
  • Loading branch information
Show file tree
Hide file tree
Showing 6 changed files with 222 additions and 11 deletions.
17 changes: 10 additions & 7 deletions src/View/Helper/DateFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -130,7 +131,9 @@ public function __invoke(
$locale,
$dateType,
$timeType,
$timezone
$timezone,
IntlDateFormatter::GREGORIAN,
$pattern
);
}

Expand Down
84 changes: 84 additions & 0 deletions src/View/Helper/Plural.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_View
*/

namespace Zend\I18n\View\Helper;

use Zend\I18n\Exception;
use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\View\Helper\AbstractHelper;

/**
* Helper for rendering text based on a count number (like the I18n plural translation helper, but when translation
* is not needed).
*
* Please note that we did not write any hard-coded rules for languages, as languages can evolve, we prefered to
* let the developer define the rules himself, instead of potentially break applications if we change rules in the
* future.
*
* However, you can find most of the up-to-date plural rules for most languages in those links:
* - http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/language_plural_rules.html
* - https://developer.mozilla.org/en-US/docs/Localization_and_Plurals
*
* @category Zend
* @package Zend_I18n
* @subpackage View
*/
class Plural extends AbstractHelper
{
/**
* Rule to use
*
* @var PluralRule
*/
protected $rule;

/**
* Set the plural rule to use
*
* @param PluralRule|string $pluralRule
* @return Plural
*/
public function setPluralRule($pluralRule)
{
if (!$pluralRule instanceof PluralRule) {
$pluralRule = PluralRule::fromString($pluralRule);
}

$this->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];
}
}
1 change: 1 addition & 0 deletions src/View/HelperConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
);
Expand Down
1 change: 1 addition & 0 deletions test/Validator/FloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
62 changes: 58 additions & 4 deletions test/View/Helper/DateFormatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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)
{
Expand All @@ -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());
Expand Down
68 changes: 68 additions & 0 deletions test/View/Helper/PluralTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_I18n
*/

namespace ZendTest\I18n\View\Helper;

use Zend\I18n\Translator\Plural\Rule as PluralRule;
use Zend\I18n\View\Helper\Plural as PluralHelper;

/**
* @category Zend
* @package Zend_View
* @subpackage UnitTests
* @group Zend_View
* @group Zend_View_Helper
*/
class PluralTest extends \PHPUnit_Framework_TestCase
{
/**
* @var PluralHelper
*/
public $helper;

/**
* Sets up the fixture
*
* @return void
*/
public function setUp()
{
$this->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);
}
}

0 comments on commit be1ce44

Please sign in to comment.