diff --git a/README.md b/README.md index d632d9d..656d560 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ echo Number::ordinalize(0); // "0th" echo Number::ordinalize(1); // "1st" echo Number::ordinalize(2); // "2nd" echo Number::ordinalize(23); // "23rd" -echo Number::ordinalize(1002); // "1002nd" +echo Number::ordinalize(1002, 'nl'); // "1002e" echo Number::ordinalize(-111); // "-111th" ``` @@ -88,7 +88,7 @@ echo Number::ordinal(1); // "st" echo Number::ordinal(2); // "nd" echo Number::ordinal(23); // "rd" echo Number::ordinal(1002); // "nd" -echo Number::ordinal(-111); // "th" +echo Number::ordinal(-111, 'nl'); // "e" ``` **Roman numbers** diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index 89caf87..3e7db4d 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -9,14 +9,25 @@ class Number { - public static function ordinalize($number) + /** + * @param int|float $number + * @param string $locale + * @return string + */ + public static function ordinalize($number, $locale = 'en') { - return $number.self::ordinal($number); + return $number.self::ordinal($number, $locale); } - public static function ordinal($number) + /** + * @param int|float $number + * @param string $locale + * @return string + */ + public static function ordinal($number, $locale = 'en') { - return (string) new Ordinal($number); + $ordinal = new Ordinal($number, $locale); + return (string) $ordinal; } public static function binarySuffix($number, $locale = 'en') diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php index f1ae6be..6058e2a 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -2,34 +2,35 @@ namespace Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\Number\Ordinal\Builder; +use Coduo\PHPHumanizer\Number\Ordinal\StrategyInterface; + class Ordinal { /** - * @var int|float + * @type int|float */ private $number; + /** + * @type StrategyInterface + */ + private $strategy; + /** * @param int|float $number + * @param string $locale */ - public function __construct($number) + public function __construct($number, $locale) { $this->number = $number; + $this->strategy = Builder::build($locale); } public function __toString() { - $absNumber = abs((integer) $this->number); - - if (in_array(($absNumber % 100), array(11, 12, 13))) { - return 'th'; - } - - switch ($absNumber % 10) { - case 1: return 'st'; - case 2: return 'nd'; - case 3: return 'rd'; - default: return 'th'; - } + return $this + ->strategy + ->ordinalSuffix($this->number); } } diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php b/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php new file mode 100644 index 0000000..f1780b2 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php @@ -0,0 +1,37 @@ +assertEquals($expected, Number::ordinal($number)); } + /** + * @dataProvider ordinalSuffixDutchProvider + * @param $expected + * @param $number + */ + public function test_return_ordinal_suffix_dutch($expected, $number) + { + $this->assertEquals($expected, Number::ordinal($number, 'nl')); + } + /** * @dataProvider ordinalizeDataProvider * @depends test_return_ordinal_suffix @@ -29,6 +39,18 @@ public function test_ordinalize_numbers($expected, $number) $this->assertEquals($expected, Number::ordinalize($number)); } + /** + * @dataProvider ordinalizeDataDutchProvider + * @depends test_return_ordinal_suffix_dutch + * + * @param $expected + * @param $number + */ + public function test_ordinalize_numbers_dutch($expected, $number) + { + $this->assertEquals($expected, Number::ordinalize($number, 'nl')); + } + /** * @dataProvider binarySuffixDataProvider * @@ -156,6 +178,20 @@ public function ordinalizeDataProvider() ); } + /** + * @return array + */ + public function ordinalizeDataDutchProvider() + { + return array( + array('1e', 1), + array('2e', 2), + array('23e', 23), + array('1002e', 1002), + array('-111e', -111), + ); + } + /** * @return array */ @@ -170,6 +206,20 @@ public function ordinalSuffixProvider() ); } + /** + * @return array + */ + public function ordinalSuffixDutchProvider() + { + return array( + array('e', 1), + array('e', 2), + array('e', 23), + array('e', 1002), + array('e', -111), + ); + } + /** * @return array */ @@ -293,4 +343,4 @@ public function arabicExceptionProvider() array("foobar"), ); } -} \ No newline at end of file +}