Skip to content

Commit

Permalink
Fix the handling of null as locale in the stub intl classes
Browse files Browse the repository at this point in the history
The Intl extension accepts null as locale in formatters and the
collator and will use the default locale in such case. Given that the
stub implementation considers that the default locale is always 'en', it
should be supported here too instead of forcing libraries to pass the
default locale explicitly.
  • Loading branch information
stof committed Jul 30, 2015
1 parent 6b02601 commit d6db6ad
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 15 deletions.
10 changes: 5 additions & 5 deletions src/Symfony/Component/Intl/Collator/Collator.php
Expand Up @@ -70,25 +70,25 @@ class Collator
/**
* Constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en".
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
*/
public function __construct($locale)
{
if ('en' != $locale) {
if ('en' !== $locale && null !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
}
}

/**
* Static constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en".
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
*
* @return Collator
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
*/
public static function create($locale)
{
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Intl/DateFormatter/IntlDateFormatter.php
Expand Up @@ -129,7 +129,7 @@ class IntlDateFormatter
/**
* Constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en".
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
* @param int $datetype Type of date formatting, one of the format type constants
* @param int $timetype Type of time formatting, one of the format type constants
* @param string $timezone Timezone identifier
Expand All @@ -140,12 +140,12 @@ class IntlDateFormatter
* @see http://www.php.net/manual/en/intldateformatter.create.php
* @see http://userguide.icu-project.org/formatparse/datetime
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
* @throws MethodArgumentValueNotImplementedException When $calendar different than GREGORIAN is passed
*/
public function __construct($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
{
if ('en' !== $locale) {
if ('en' !== $locale && null !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
}

Expand All @@ -163,7 +163,7 @@ public function __construct($locale, $datetype, $timetype, $timezone = null, $ca
/**
* Static constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en".
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
* @param int $datetype Type of date formatting, one of the format type constants
* @param int $timetype Type of time formatting, one of the format type constants
* @param string $timezone Timezone identifier
Expand All @@ -176,7 +176,7 @@ public function __construct($locale, $datetype, $timetype, $timezone = null, $ca
* @see http://www.php.net/manual/en/intldateformatter.create.php
* @see http://userguide.icu-project.org/formatparse/datetime
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
* @throws MethodArgumentValueNotImplementedException When $calendar different than GREGORIAN is passed
*/
public static function create($locale, $datetype, $timetype, $timezone = null, $calendar = self::GREGORIAN, $pattern = null)
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/Intl/NumberFormatter/NumberFormatter.php
Expand Up @@ -245,7 +245,7 @@ class NumberFormatter
/**
* Constructor.
*
* @param string $locale The locale code. The only currently supported locale is "en".
* @param string $locale The locale code. The only currently supported locale is "en" (or null using the default locale, i.e. "en").
* @param int $style Style of the formatting, one of the format style constants.
* The only supported styles are NumberFormatter::DECIMAL
* and NumberFormatter::CURRENCY.
Expand All @@ -257,13 +257,13 @@ class NumberFormatter
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
* @see http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
* @throws MethodArgumentValueNotImplementedException When the $style is not supported
* @throws MethodArgumentNotImplementedException When the pattern value is different than null
*/
public function __construct($locale = 'en', $style = null, $pattern = null)
{
if ('en' != $locale) {
if ('en' !== $locale && null !== $locale) {
throw new MethodArgumentValueNotImplementedException(__METHOD__, 'locale', $locale, 'Only the locale "en" is supported');
}

Expand All @@ -282,7 +282,7 @@ public function __construct($locale = 'en', $style = null, $pattern = null)
/**
* Static constructor.
*
* @param string $locale The locale code. The only supported locale is "en".
* @param string $locale The locale code. The only supported locale is "en" (or null using the default locale, i.e. "en").
* @param int $style Style of the formatting, one of the format style constants.
* The only currently supported styles are NumberFormatter::DECIMAL
* and NumberFormatter::CURRENCY.
Expand All @@ -296,7 +296,7 @@ public function __construct($locale = 'en', $style = null, $pattern = null)
* @see http://www.icu-project.org/apiref/icu4c/classDecimalFormat.html#_details
* @see http://www.icu-project.org/apiref/icu4c/classRuleBasedNumberFormat.html#_details
*
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" is passed
* @throws MethodArgumentValueNotImplementedException When $locale different than "en" or null is passed
* @throws MethodArgumentValueNotImplementedException When the $style is not supported
* @throws MethodArgumentNotImplementedException When the pattern value is different than null
*/
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Intl/Tests/Collator/CollatorTest.php
Expand Up @@ -60,6 +60,12 @@ public function testGetLocale()
$this->assertEquals('en', $collator->getLocale());
}

public function testConstructWithoutLocale()
{
$collator = $this->getCollator(null);
$this->assertInstanceOf('\Symfony\Component\Intl\Collator\Collator', $collator);
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodNotImplementedException
*/
Expand Down
Expand Up @@ -22,6 +22,12 @@ public function testConstructor()
$this->assertEquals('y-M-d', $formatter->getPattern());
}

public function testConstructorWithoutLocale()
{
$formatter = new IntlDateFormatter(null, IntlDateFormatter::MEDIUM, IntlDateFormatter::SHORT, 'UTC', IntlDateFormatter::GREGORIAN, 'y-M-d');
$this->assertEquals('y-M-d', $formatter->getPattern());
}

/**
* @expectedException \Symfony\Component\Intl\Exception\MethodArgumentValueNotImplementedException
*/
Expand Down
Expand Up @@ -70,6 +70,14 @@ public function testSetAttributeInvalidRoundingMode()
$formatter->setAttribute(NumberFormatter::ROUNDING_MODE, null);
}

public function testConstructWithoutLocale()
{
$this->assertInstanceOf(
'\Symfony\Component\Intl\NumberFormatter\NumberFormatter',
$this->getNumberFormatter(null, NumberFormatter::DECIMAL)
);
}

public function testCreate()
{
$this->assertInstanceOf(
Expand Down

0 comments on commit d6db6ad

Please sign in to comment.