Skip to content

Commit

Permalink
Merge pull request #7350 from bcrowe/ordinal-numbers
Browse files Browse the repository at this point in the history
Update ordinal number method to reuse existing formatters and code.
  • Loading branch information
markstory committed Sep 4, 2015
2 parents 1493109 + afe56ab commit af3c9c7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
16 changes: 9 additions & 7 deletions src/I18n/Number.php
Expand Up @@ -253,7 +253,7 @@ public static function defaultCurrency($currency = null)
*
* - `locale` - The locale name to use for formatting the number, e.g. fr_FR
* - `type` - The formatter type to construct, set it to `currency` if you need to format
* numbers representing money.
* numbers representing money or a NumberFormatter constant.
* - `places` - Number of decimal places to use. e.g. 2
* - `precision` - Maximum Number of decimal places to use, e.g. 2
* - `pattern` - An ICU number pattern to use for formatting the number. e.g #,###.00
Expand All @@ -272,8 +272,11 @@ public static function formatter($options = [])
}

$type = NumberFormatter::DECIMAL;
if (!empty($options['type']) && $options['type'] === 'currency') {
$type = NumberFormatter::CURRENCY;
if (!empty($options['type'])) {
$type = $options['type'];
if ($options['type'] === 'currency') {
$type = NumberFormatter::CURRENCY;
}
}

if (!isset(static::$_formatters[$locale][$type])) {
Expand Down Expand Up @@ -317,12 +320,11 @@ public static function formatter($options = [])
* Returns a formatted integer as an ordinal number string (e.g. 1st, 2nd, 3rd, 4th, [...])
*
* @param int|float $value An integer
* @param array $options An array with options.
* @return string
*/
public static function ordinal($value)
public static function ordinal($value, array $options = [])
{
$locale = I18n::locale();
$formatter = new NumberFormatter($locale, NumberFormatter::ORDINAL);
return $formatter->format($value);
return static::formatter(['type' => NumberFormatter::ORDINAL] + $options)->format($value);
}
}
5 changes: 3 additions & 2 deletions src/View/Helper/NumberHelper.php
Expand Up @@ -235,10 +235,11 @@ public function implementedEvents()
* Formats a number into locale specific ordinal suffix.
*
* @param int|float $value An integer
* @param array $options An array with options.
* @return string formatted number
*/
public function ordinal($value)
public function ordinal($value, array $options = [])
{
return $this->_engine->ordinal($value);
return $this->_engine->ordinal($value, $options);
}
}
5 changes: 5 additions & 0 deletions tests/TestCase/I18n/NumberTest.php
Expand Up @@ -553,6 +553,11 @@ public function testOrdinal()
$result = $this->Number->ordinal(2);
$this->assertEquals('2nd', $result);

$result = $this->Number->ordinal(2, [
'locale' => 'fr_FR'
]);
$this->assertEquals('2e', $result);

$result = $this->Number->ordinal(3);
$this->assertEquals('3rd', $result);

Expand Down

0 comments on commit af3c9c7

Please sign in to comment.