From db1a4e99466f8efe6c30523733fcd8fd844d467e Mon Sep 17 00:00:00 2001 From: isnani Date: Tue, 17 Nov 2015 07:40:51 +0100 Subject: [PATCH 01/13] add ordinal number format for indonesian --- src/Coduo/PHPHumanizer/Number.php | 11 +++++++---- src/Coduo/PHPHumanizer/Number/Ordinal.php | 11 ++++++++++- tests/Coduo/PHPHumanizer/Tests/NumberTest.php | 18 ++++++++++++++---- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index 89caf87..52b3b1a 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -9,14 +9,17 @@ class Number { - public static function ordinalize($number) + public static function ordinalize($number, $locale = 'en') { - return $number.self::ordinal($number); + if ($locale == 'id'){ + return self::ordinal($number, 'id').$number; + } + return $number.self::ordinal($number, $locale); } - public static function ordinal($number) + public static function ordinal($number, $locale = 'en') { - return (string) new Ordinal($number); + return (string) new Ordinal($number, $locale); } 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..327cae9 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -9,18 +9,27 @@ class Ordinal */ private $number; + /** + * @var string + */ + private $locale; /** * @param int|float $number */ - public function __construct($number) + public function __construct($number, $locale = 'en') { $this->number = $number; + $this->locale = $locale; } public function __toString() { $absNumber = abs((integer) $this->number); + if ($this->locale == 'id'){ + return 'ke-'; + } + if (in_array(($absNumber % 100), array(11, 12, 13))) { return 'th'; } diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php index db31d65..a79fec3 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php @@ -11,10 +11,11 @@ class NumberTest extends \PHPUnit_Framework_TestCase * * @param $expected * @param $number + * @param $locale */ - public function test_return_ordinal_suffix($expected, $number) + public function test_return_ordinal_suffix($expected, $number, $locale = 'en') { - $this->assertEquals($expected, Number::ordinal($number)); + $this->assertEquals($expected, Number::ordinal($number, $locale)); } /** @@ -23,10 +24,11 @@ public function test_return_ordinal_suffix($expected, $number) * * @param $expected * @param $number + * @param $locale */ - public function test_ordinalize_numbers($expected, $number) + public function test_ordinalize_numbers($expected, $number, $locale = 'en') { - $this->assertEquals($expected, Number::ordinalize($number)); + $this->assertEquals($expected, Number::ordinalize($number, $locale)); } /** @@ -153,6 +155,10 @@ public function ordinalizeDataProvider() array('23rd', 23), array('1002nd', 1002), array('-111th', -111), + + //Locale cases + array('ke-2', 2, 'id'), + array('ke-41', 41, 'id'), ); } @@ -167,6 +173,10 @@ public function ordinalSuffixProvider() array('rd', 23), array('nd', 1002), array('th', -111), + + //Locale cases + array('ke-', 2, 'id'), + array('ke-', 41, 'id'), ); } From d189df4f0f7c9318922e3ce18294e88fd89b012b Mon Sep 17 00:00:00 2001 From: isnani Date: Sun, 29 Nov 2015 04:32:30 +0100 Subject: [PATCH 02/13] add ordinal strategy --- src/Coduo/PHPHumanizer/Number/Ordinal.php | 31 ++++++++++--------- .../PHPHumanizer/Number/Ordinal/Builder.php | 28 +++++++++++++++++ .../PHPHumanizer/Number/Ordinal/Parser.php | 25 +++++++++++++++ src/Coduo/PHPHumanizer/Number/Ordinal/en.xml | 21 +++++++++++++ src/Coduo/PHPHumanizer/Number/Ordinal/id.xml | 8 +++++ 5 files changed, 98 insertions(+), 15 deletions(-) create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/en.xml create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/id.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php index 327cae9..f17999c 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -2,6 +2,8 @@ namespace Coduo\PHPHumanizer\Number; +use Coduo\PHPHumanizer\Number\Ordinal\Parser; + class Ordinal { /** @@ -12,33 +14,32 @@ class Ordinal /** * @var string */ - private $locale; + private $parser; /** * @param int|float $number */ public function __construct($number, $locale = 'en') { $this->number = $number; - $this->locale = $locale; + $this->parser = Parser::parse($locale); } public function __toString() { - $absNumber = abs((integer) $this->number); - - if ($this->locale == 'id'){ - return 'ke-'; + // $this->parser contains array of xml elements + is_regular = False; + foreach $this->parser->irregular as $number + { + if (preg_match($number[pattern], $this->number)) { + return $number->prefix.$this->number.$number->suffix; + } + else { + is_regular = True; + } } - - if (in_array(($absNumber % 100), array(11, 12, 13))) { - return 'th'; + if (is_regular === True) { + return $this->parser->regular->prefix.$this->number.$this->parser->regular->suffix; } - switch ($absNumber % 10) { - case 1: return 'st'; - case 2: return 'nd'; - case 3: return 'rd'; - default: return 'th'; - } } } diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php b/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php new file mode 100644 index 0000000..df64455 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php b/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php new file mode 100644 index 0000000..043148f --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php @@ -0,0 +1,25 @@ +builder = $builder; + $this->locale = $locale; + } + + public function parse() + { + return simplexml_load_file($this->builder->build($this->locale)) or die("Error: Cannot create object"); + } +} +?> \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml new file mode 100644 index 0000000..2eb3fb1 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml @@ -0,0 +1,21 @@ + + + + + + th + + + + st + + + + nd + + + + + th + + \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml new file mode 100644 index 0000000..4332acb --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml @@ -0,0 +1,8 @@ + + + + + ke- + + + \ No newline at end of file From aa91d360c53c7e3f3ba65880976b6b448fc97b49 Mon Sep 17 00:00:00 2001 From: isnani Date: Tue, 8 Dec 2015 08:32:59 +0100 Subject: [PATCH 03/13] try to internationalize Ordinal --- src/Coduo/PHPHumanizer/Number/Ordinal.php | 45 ------------------- .../PHPHumanizer/Number/Ordinal/Builder.php | 28 ------------ .../PHPHumanizer/Number/Ordinal/Parser.php | 25 ----------- 3 files changed, 98 deletions(-) delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal.php delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php deleted file mode 100644 index f17999c..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ /dev/null @@ -1,45 +0,0 @@ -number = $number; - $this->parser = Parser::parse($locale); - } - - public function __toString() - { - // $this->parser contains array of xml elements - is_regular = False; - foreach $this->parser->irregular as $number - { - if (preg_match($number[pattern], $this->number)) { - return $number->prefix.$this->number.$number->suffix; - } - else { - is_regular = True; - } - } - if (is_regular === True) { - return $this->parser->regular->prefix.$this->number.$this->parser->regular->suffix; - } - - } -} diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php b/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php deleted file mode 100644 index df64455..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/Builder.php +++ /dev/null @@ -1,28 +0,0 @@ - \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php b/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php deleted file mode 100644 index 043148f..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/Parser.php +++ /dev/null @@ -1,25 +0,0 @@ -builder = $builder; - $this->locale = $locale; - } - - public function parse() - { - return simplexml_load_file($this->builder->build($this->locale)) or die("Error: Cannot create object"); - } -} -?> \ No newline at end of file From b5d010ceb1cecfb5ba4f6d76f790891908392716 Mon Sep 17 00:00:00 2001 From: isnani Date: Tue, 8 Dec 2015 08:39:57 +0100 Subject: [PATCH 04/13] still try to internationalize Ordinal --- src/Coduo/PHPHumanizer/Number.php | 12 ++--- src/Coduo/PHPHumanizer/Number/Ordinal.php | 51 +++++++++++++++++++ src/Coduo/PHPHumanizer/Number/Ordinal/en.xml | 10 ++-- tests/Coduo/PHPHumanizer/Tests/NumberTest.php | 32 +----------- 4 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal.php diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index 52b3b1a..758779d 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -11,15 +11,9 @@ class Number { public static function ordinalize($number, $locale = 'en') { - if ($locale == 'id'){ - return self::ordinal($number, 'id').$number; - } - return $number.self::ordinal($number, $locale); - } + $ordinalized = new Ordinal($number, $locale); - public static function ordinal($number, $locale = 'en') - { - return (string) new Ordinal($number, $locale); + return $ordinalized->ordinalize(); } public static function binarySuffix($number, $locale = 'en') @@ -56,4 +50,4 @@ public static function fromRoman($number) return $romanNumeral->fromRoman($number); } -} +} \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php new file mode 100644 index 0000000..716be36 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -0,0 +1,51 @@ +number = $number; + $this->locale = $locale; + } + + private function build() + { + if (!preg_match('/^([a-z]{2})(_([A-Z]{2}))?$/', $this->locale, $m)) { + throw new \RuntimeException("Invalid locale specified: '$this->locale'."); + } + $strategy = $m[1]; + if (!empty($m[3])) { + $strategy .= "_$m[3]"; + } + $strategy .= ".xml"; + + return $strategy; + } + + public function ordinalize() + { + $xml = simplexml_load_file(__DIR__.'/Ordinal/'.$this->build()); + foreach ($xml->irregular->children() as $numbers) { + if (preg_match($numbers['pattern'], $this->number)){ + return $numbers->prefix. $this->number. $numbers->suffix; + } + } + return $xml->regular->prefix. $this->number. $xml->regular->suffix; + } +} \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml index 2eb3fb1..d64dedc 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml @@ -1,18 +1,22 @@ - + th - + st - + nd + + + rd + diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php index a79fec3..b123f4e 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php @@ -6,21 +6,8 @@ class NumberTest extends \PHPUnit_Framework_TestCase { - /** - * @dataProvider ordinalSuffixProvider - * - * @param $expected - * @param $number - * @param $locale - */ - public function test_return_ordinal_suffix($expected, $number, $locale = 'en') - { - $this->assertEquals($expected, Number::ordinal($number, $locale)); - } - /** * @dataProvider ordinalizeDataProvider - * @depends test_return_ordinal_suffix * * @param $expected * @param $number @@ -147,6 +134,7 @@ public function test_statically_throw_exception_when_converting_roman_number_is_ /** * @return array */ + public function ordinalizeDataProvider() { return array( @@ -162,24 +150,6 @@ public function ordinalizeDataProvider() ); } - /** - * @return array - */ - public function ordinalSuffixProvider() - { - return array( - array('st', 1), - array('nd', 2), - array('rd', 23), - array('nd', 1002), - array('th', -111), - - //Locale cases - array('ke-', 2, 'id'), - array('ke-', 41, 'id'), - ); - } - /** * @return array */ From 9fd14315f6057cb181ede5d01993e7a73c567f25 Mon Sep 17 00:00:00 2001 From: isnani Date: Tue, 8 Dec 2015 08:39:57 +0100 Subject: [PATCH 05/13] still try to localize Ordinal --- src/Coduo/PHPHumanizer/Number.php | 12 ++--- src/Coduo/PHPHumanizer/Number/Ordinal.php | 51 +++++++++++++++++++ src/Coduo/PHPHumanizer/Number/Ordinal/en.xml | 10 ++-- tests/Coduo/PHPHumanizer/Tests/NumberTest.php | 32 +----------- 4 files changed, 62 insertions(+), 43 deletions(-) create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal.php diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index 52b3b1a..758779d 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -11,15 +11,9 @@ class Number { public static function ordinalize($number, $locale = 'en') { - if ($locale == 'id'){ - return self::ordinal($number, 'id').$number; - } - return $number.self::ordinal($number, $locale); - } + $ordinalized = new Ordinal($number, $locale); - public static function ordinal($number, $locale = 'en') - { - return (string) new Ordinal($number, $locale); + return $ordinalized->ordinalize(); } public static function binarySuffix($number, $locale = 'en') @@ -56,4 +50,4 @@ public static function fromRoman($number) return $romanNumeral->fromRoman($number); } -} +} \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php new file mode 100644 index 0000000..716be36 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -0,0 +1,51 @@ +number = $number; + $this->locale = $locale; + } + + private function build() + { + if (!preg_match('/^([a-z]{2})(_([A-Z]{2}))?$/', $this->locale, $m)) { + throw new \RuntimeException("Invalid locale specified: '$this->locale'."); + } + $strategy = $m[1]; + if (!empty($m[3])) { + $strategy .= "_$m[3]"; + } + $strategy .= ".xml"; + + return $strategy; + } + + public function ordinalize() + { + $xml = simplexml_load_file(__DIR__.'/Ordinal/'.$this->build()); + foreach ($xml->irregular->children() as $numbers) { + if (preg_match($numbers['pattern'], $this->number)){ + return $numbers->prefix. $this->number. $numbers->suffix; + } + } + return $xml->regular->prefix. $this->number. $xml->regular->suffix; + } +} \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml index 2eb3fb1..d64dedc 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml @@ -1,18 +1,22 @@ - + th - + st - + nd + + + rd + diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php index a79fec3..b123f4e 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php @@ -6,21 +6,8 @@ class NumberTest extends \PHPUnit_Framework_TestCase { - /** - * @dataProvider ordinalSuffixProvider - * - * @param $expected - * @param $number - * @param $locale - */ - public function test_return_ordinal_suffix($expected, $number, $locale = 'en') - { - $this->assertEquals($expected, Number::ordinal($number, $locale)); - } - /** * @dataProvider ordinalizeDataProvider - * @depends test_return_ordinal_suffix * * @param $expected * @param $number @@ -147,6 +134,7 @@ public function test_statically_throw_exception_when_converting_roman_number_is_ /** * @return array */ + public function ordinalizeDataProvider() { return array( @@ -162,24 +150,6 @@ public function ordinalizeDataProvider() ); } - /** - * @return array - */ - public function ordinalSuffixProvider() - { - return array( - array('st', 1), - array('nd', 2), - array('rd', 23), - array('nd', 1002), - array('th', -111), - - //Locale cases - array('ke-', 2, 'id'), - array('ke-', 41, 'id'), - ); - } - /** * @return array */ From 79968d6872815779d2c902764bd06826b9454a8f Mon Sep 17 00:00:00 2001 From: isnani Date: Tue, 8 Dec 2015 08:56:08 +0100 Subject: [PATCH 06/13] add ordinal rule for locale de --- src/Coduo/PHPHumanizer/Number/Ordinal/de.xml | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/de.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml new file mode 100644 index 0000000..6ca2d91 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml @@ -0,0 +1,8 @@ + + + + + + . + + \ No newline at end of file From 51571547286f85a84de3f51e28d34d5c801992f5 Mon Sep 17 00:00:00 2001 From: isnani Date: Thu, 10 Dec 2015 04:13:56 +0100 Subject: [PATCH 07/13] add ordinal format for locale de and zh_CN --- src/Coduo/PHPHumanizer/Number/Ordinal/de.xml | 8 ++++++++ src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml | 8 ++++++++ 2 files changed, 16 insertions(+) create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/de.xml create mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml new file mode 100644 index 0000000..6ca2d91 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml @@ -0,0 +1,8 @@ + + + + + + . + + \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml new file mode 100644 index 0000000..07c9973 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file From 83ea2c621158fb19f716fbb62a40c7f7802b8abb Mon Sep 17 00:00:00 2001 From: isnani Date: Thu, 10 Dec 2015 04:14:44 +0100 Subject: [PATCH 08/13] add exception for negative and floating numbers --- src/Coduo/PHPHumanizer/Number/Ordinal.php | 11 +++++++++- tests/Coduo/PHPHumanizer/Tests/NumberTest.php | 21 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php index 716be36..37699d2 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -27,7 +27,7 @@ public function __construct($number, $locale = 'en') private function build() { if (!preg_match('/^([a-z]{2})(_([A-Z]{2}))?$/', $this->locale, $m)) { - throw new \RuntimeException("Invalid locale specified: '$this->locale'."); + throw new \RuntimeException("Invalid locale specified: .'$this->locale'."); } $strategy = $m[1]; if (!empty($m[3])) { @@ -40,7 +40,16 @@ private function build() public function ordinalize() { + if ($this->number < 0){ + throw new \RuntimeException("Cannot treat negative number as ordinal"); + } + + if (is_float($this->number)){ + throw new \RuntimeException("Cannot treat float number as ordinal"); + } + $xml = simplexml_load_file(__DIR__.'/Ordinal/'.$this->build()); + foreach ($xml->irregular->children() as $numbers) { if (preg_match($numbers['pattern'], $this->number)){ return $numbers->prefix. $this->number. $numbers->suffix; diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php index b123f4e..ece8fee 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberTest.php @@ -18,6 +18,23 @@ public function test_ordinalize_numbers($expected, $number, $locale = 'en') $this->assertEquals($expected, Number::ordinalize($number, $locale)); } + /** + * @expectedException \RuntimeException + */ + public function test_statically_throw_exception_when_ordinalizing_negative_number() + { + Number::ordinalize(-111); + } + + /** + * @expectedException \RuntimeException + */ + public function test_statically_throw_exception_when_ordinalizing_floating_number() + { + + NUmber::ordinalize(2.876); + } + /** * @dataProvider binarySuffixDataProvider * @@ -142,11 +159,11 @@ public function ordinalizeDataProvider() array('2nd', 2), array('23rd', 23), array('1002nd', 1002), - array('-111th', -111), //Locale cases array('ke-2', 2, 'id'), - array('ke-41', 41, 'id'), + array('41.', 41, 'de'), + ); } From bd02cf1f908be4fb6c922fe6a1589eb55959c368 Mon Sep 17 00:00:00 2001 From: isnani Date: Wed, 23 Dec 2015 04:46:58 +0100 Subject: [PATCH 09/13] untrack xml --- src/Coduo/PHPHumanizer/Number/Ordinal/id.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/id.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml deleted file mode 100644 index 4332acb..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/id.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - ke- - - - \ No newline at end of file From 0c02f4a0772029679f4d52b090c2aa48ae9ed528 Mon Sep 17 00:00:00 2001 From: isnani Date: Wed, 23 Dec 2015 05:00:14 +0100 Subject: [PATCH 10/13] untrack xml --- src/Coduo/PHPHumanizer/Number/Ordinal/de.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/de.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml deleted file mode 100644 index 6ca2d91..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/de.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - . - - \ No newline at end of file From 791e477d33da06e3316e516bff803fc6c9fa227b Mon Sep 17 00:00:00 2001 From: isnani Date: Wed, 23 Dec 2015 05:00:42 +0100 Subject: [PATCH 11/13] untrack xml --- src/Coduo/PHPHumanizer/Number/Ordinal/en.xml | 25 -------------------- 1 file changed, 25 deletions(-) delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/en.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml deleted file mode 100644 index d64dedc..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/en.xml +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - th - - - - st - - - - nd - - - - rd - - - - - th - - \ No newline at end of file From 82574447ae4da7f2b7f1af64f1296fd279ae3a62 Mon Sep 17 00:00:00 2001 From: isnani Date: Wed, 23 Dec 2015 05:04:25 +0100 Subject: [PATCH 12/13] update to latest version of upstream --- src/Coduo/PHPHumanizer/Number/Ordinal.php | 51 +++-------- src/Coduo/PHPHumanizer/NumberHumanizer.php | 19 +--- .../Tests/NumberHumanizerTest.php | 90 +++++++++---------- 3 files changed, 55 insertions(+), 105 deletions(-) diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php index ae93243..e948eed 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -13,51 +13,24 @@ final class Ordinal private $number; /** - * @var string - */ - private $locale; + * @var StrategyInterface + */ + private $strategy; /** - * @param int $number - * @param string $locale + * @param int|float $number + * @param string $locale */ - public function __construct($number, $locale = 'en') + public function __construct($number, $locale) { $this->number = $number; - $this->locale = $locale; + $this->strategy = Builder::build($locale); } - private function build() - { - if (!preg_match('/^([a-z]{2})(_([A-Z]{2}))?$/', $this->locale, $m)) { - throw new \RuntimeException("Invalid locale specified: .'$this->locale'."); - } - $strategy = $m[1]; - if (!empty($m[3])) { - $strategy .= "_$m[3]"; - } - $strategy .= ".xml"; - - return $strategy; - } - - public function ordinalize() + public function __toString() { - if ($this->number < 0){ - throw new \RuntimeException("Cannot treat negative number as ordinal"); - } - - if (is_float($this->number)){ - throw new \RuntimeException("Cannot treat float number as ordinal"); - } - - $xml = simplexml_load_file(__DIR__.'/Ordinal/'.$this->build()); - - foreach ($xml->irregular->children() as $numbers) { - if (preg_match($numbers['pattern'], $this->number)){ - return $numbers->prefix. $this->number. $numbers->suffix; - } - } - return $xml->regular->prefix. $this->number. $xml->regular->suffix; + return $this + ->strategy + ->ordinalSuffix($this->number); } -} +} \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/NumberHumanizer.php b/src/Coduo/PHPHumanizer/NumberHumanizer.php index f0d8c81..173b727 100644 --- a/src/Coduo/PHPHumanizer/NumberHumanizer.php +++ b/src/Coduo/PHPHumanizer/NumberHumanizer.php @@ -1,12 +1,9 @@ ordinalize(); + return $number.self::ordinal($number, $locale); } - /** * @param int|float $number * @param string $locale @@ -32,7 +27,6 @@ public static function ordinal($number, $locale = 'en') $ordinal = new Ordinal($number, $locale); return (string) $ordinal; } - /** * @param $number * @param string $locale @@ -41,10 +35,8 @@ public static function ordinal($number, $locale = 'en') public static function binarySuffix($number, $locale = 'en') { $binarySuffix = new BinarySuffix($number, $locale); - return $binarySuffix->convert(); } - /** * @param $number * @param $precision @@ -54,10 +46,8 @@ public static function binarySuffix($number, $locale = 'en') public static function preciseBinarySuffix($number, $precision, $locale = 'en') { $binarySuffix = new BinarySuffix($number, $locale, $precision); - return $binarySuffix->convert(); } - /** * @param $number * @param string $locale @@ -66,10 +56,8 @@ public static function preciseBinarySuffix($number, $precision, $locale = 'en') public static function metricSuffix($number, $locale = 'en') { $binarySuffix = new MetricSuffix($number, $locale); - return $binarySuffix->convert(); } - /** * @param $number * @return string @@ -77,10 +65,8 @@ public static function metricSuffix($number, $locale = 'en') public static function toRoman($number) { $romanNumeral = new RomanNumeral(); - return $romanNumeral->toRoman($number); } - /** * @param $number * @return int @@ -88,7 +74,6 @@ public static function toRoman($number) public static function fromRoman($number) { $romanNumeral = new RomanNumeral(); - return $romanNumeral->fromRoman($number); } -} +} \ No newline at end of file diff --git a/tests/Coduo/PHPHumanizer/Tests/NumberHumanizerTest.php b/tests/Coduo/PHPHumanizer/Tests/NumberHumanizerTest.php index 33f07c1..e749330 100644 --- a/tests/Coduo/PHPHumanizer/Tests/NumberHumanizerTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/NumberHumanizerTest.php @@ -1,39 +1,38 @@ assertEquals($expected, NumberHumanizer::ordinal($number)); } - /** - * @expectedException \RuntimeException + * @dataProvider ordinalSuffixDutchProvider + * @param $expected + * @param $number */ - public function test_statically_throw_exception_when_ordinalizing_negative_number() + public function test_return_ordinal_suffix_dutch($expected, $number) { - $this->assertEquals($expected, NumberHumanizer::ordinal($number, 'nl')); + $this->assertEquals($expected, NumberHumanizer::ordinal($number, 'nl')); } - /** - * @expectedException \RuntimeException + * @dataProvider ordinalizeDataProvider + * @depends test_return_ordinal_suffix + * + * @param $expected + * @param $number */ - public function test_statically_throw_exception_when_ordinalizing_floating_number() + public function test_ordinalize_numbers($expected, $number) { $this->assertEquals($expected, NumberHumanizer::ordinalize($number)); } - /** * @dataProvider ordinalizeDataDutchProvider * @depends test_return_ordinal_suffix_dutch @@ -45,7 +44,6 @@ public function test_ordinalize_numbers_dutch($expected, $number) { $this->assertEquals($expected, NumberHumanizer::ordinalize($number, 'nl')); } - /** * @dataProvider binarySuffixDataProvider * @@ -57,7 +55,6 @@ public function test_convert_number_to_string_with_binary_suffix($expected, $num { $this->assertEquals($expected, NumberHumanizer::binarySuffix($number, $locale)); } - /** * @expectedException \InvalidArgumentException */ @@ -65,7 +62,6 @@ public function test_statically_throw_exception_when_converting_to_string_with_b { NumberHumanizer::binarySuffix('as12'); } - /** * @dataProvider preciseBinarySuffixDataProvider * @@ -78,7 +74,6 @@ public function test_convert_number_to_string_with_precise_binary_suffix($expect { $this->assertEquals($expected, NumberHumanizer::preciseBinarySuffix($number, $precision, $locale)); } - /** * @expectedException \InvalidArgumentException */ @@ -86,7 +81,6 @@ public function test_statically_throw_exception_when_converting_to_string_with_p { NumberHumanizer::preciseBinarySuffix(1, -1); } - /** * @expectedException \InvalidArgumentException */ @@ -94,7 +88,6 @@ public function test_statically_throw_exception_when_converting_to_string_with_p { NumberHumanizer::preciseBinarySuffix(1, 4); } - /** * @dataProvider metricSuffixDataProvider * @@ -106,7 +99,6 @@ public function test_convert_number_to_string_with_metric_suffix($expected, $num { $this->assertEquals($expected, NumberHumanizer::metricSuffix($number, $locale)); } - /** * @expectedException \InvalidArgumentException */ @@ -114,7 +106,6 @@ public function test_statically_throw_exception_when_converting_to_string_with_m { NumberHumanizer::metricSuffix('as12'); } - /** * @dataProvider romanDataProvider * @@ -125,7 +116,6 @@ public function test_converts_numbers_to_roman($expected, $number) { $this->assertEquals($expected, NumberHumanizer::toRoman($number)); } - /** * @dataProvider romanDataProvider * @@ -136,7 +126,6 @@ public function test_convert_roman_numbers_to_arabic($number, $expected) { $this->assertEquals($expected, NumberHumanizer::fromRoman($number)); } - /** * @dataProvider romanExceptionProvider * @expectedException \InvalidArgumentException @@ -147,7 +136,6 @@ public function test_statically_throw_exception_when_converting_number_is_out_of { NumberHumanizer::toRoman($number); } - /** * @dataProvider arabicExceptionProvider * @expectedException \InvalidArgumentException @@ -158,11 +146,9 @@ public function test_statically_throw_exception_when_converting_roman_number_is_ { NumberHumanizer::fromRoman($number); } - /** * @return array */ - public function ordinalizeDataProvider() { return array( @@ -170,14 +156,35 @@ public function ordinalizeDataProvider() array('2nd', 2), array('23rd', 23), array('1002nd', 1002), - - //Locale cases - array('ke-2', 2, 'id'), - array('41.', 41, 'de'), - + array('-111th', -111), + ); + } + /** + * @return array + */ + public function ordinalizeDataDutchProvider() + { + return array( + array('1e', 1), + array('2e', 2), + array('23e', 23), + array('1002e', 1002), + array('-111e', -111), + ); + } + /** + * @return array + */ + public function ordinalSuffixProvider() + { + return array( + array('st', 1), + array('nd', 2), + array('rd', 23), + array('nd', 1002), + array('th', -111), ); } - /** * @return array */ @@ -191,7 +198,6 @@ public function ordinalSuffixDutchProvider() array('e', -111), ); } - /** * @return array */ @@ -208,12 +214,10 @@ public function binarySuffixDataProvider() array("2 GB", 1073741824 * 2), array("3 TB", 1099511627776 * 3), array("1.18 PB", 1325899906842624), - array("1,5 kB", 1536, 'pl'), array("1,18 PB", 1325899906842624, 'pl'), ); } - /** * @return array */ @@ -222,40 +226,32 @@ public function preciseBinarySuffixDataProvider() return array( // Negative case array(-1, -1, 3), - // Byte Cases array("0 bytes", 0, 3), array("1 bytes", 1, 0), array("1023 bytes", 1023, 3), - // Kilobyte Cases array('1.000 kB', 1024, 3), array("2 kB", 1588, 0), array("1.6 kB", 1588, 1), array("1.55 kB", 1588, 2), array("1.551 kB", 1588, 3), - // Megabyte Cases array("5.00 MB", (1048576 * 5), 2), array("5.00 MB", (1048576 * 5) + 600, 2), array("5.001 MB", (1048576 * 5) + 600, 3), - // Gigabyte Cases array("2 GB", 1073741824 * 2, 0), array("2.0 GB", 1073741824 * 2, 1), - // Terabyte Cases array("3.00 TB", 1099511627776 * 3, 2), - // Petabyte Case array("1.178 PB", 1325899906842624, 3), - // Locale Cases array("1,500 kB", 1536, 3, 'pl'), array("1,178 PB", 1325899906842624, 3, 'pl'), ); } - /** * @return array */ @@ -270,13 +266,11 @@ public function metricSuffixDataProvider() array("1.2k", 1240), array("1.24M", 1240000), array("3.5M", 3500000), - array("1,2k", 1240, 'pl'), array("1,24M", 1240000, 'pl'), array("3,5M", 3500000, 'pl'), ); } - /** * @return array */ @@ -292,7 +286,6 @@ public function romanDataProvider() array("MMMCMXCIX", 3999), ); } - /** * @return array */ @@ -303,7 +296,6 @@ public function romanExceptionProvider() array(4000), ); } - /** * @return array */ @@ -315,4 +307,4 @@ public function arabicExceptionProvider() array("foobar"), ); } -} +} \ No newline at end of file From f432dcdb635076a31125034c8e4eccdd1c66112f Mon Sep 17 00:00:00 2001 From: isnani Date: Wed, 23 Dec 2015 05:07:51 +0100 Subject: [PATCH 13/13] untrack xml --- src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml b/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml deleted file mode 100644 index 07c9973..0000000 --- a/src/Coduo/PHPHumanizer/Number/Ordinal/zh_CN.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file