Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve strategy to handle prefix-ordinal #73

Merged
merged 1 commit into from
Jan 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/Coduo/PHPHumanizer/Number/Ordinal.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,15 @@ public function __construct($number, $locale)
$this->strategy = Builder::build($locale);
}

public function isPrefix()
{
return $this->strategy->isPrefix();
}

public function __toString()
{
return $this
->strategy
->ordinalSuffix($this->number);
->ordinalIndicator($this->number);
}
}
}
7 changes: 6 additions & 1 deletion src/Coduo/PHPHumanizer/Number/Ordinal/StrategyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@

interface StrategyInterface
{
/**
* @return boolean
*/
public function isPrefix();

/**
* @param int|float $number
*
* @return string
*/
public function ordinalSuffix($number);
public function ordinalIndicator($number);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what was wrong with suffix?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some languages has ordinal as prefix, e.g Indonesian, Malay, Chinese, ...
Ex:
"1st" in English = "ke-1" in Indonesian

}
9 changes: 6 additions & 3 deletions src/Coduo/PHPHumanizer/NumberHumanizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,13 @@ final class NumberHumanizer
*/
public static function ordinalize($number, $locale = 'en')
{
return $number.self::ordinal($number, $locale);
$ordinal = new Ordinal($number, $locale);
if ($ordinal->isPrefix()) {
return (string) $ordinal.$number;
}
else return (string) $number.$ordinal;
}

/**
* @param int|float $number
* @param string $locale
Expand All @@ -29,7 +33,6 @@ public static function ordinalize($number, $locale = 'en')
public static function ordinal($number, $locale = 'en')
{
$ordinal = new Ordinal($number, $locale);

return (string) $ordinal;
}

Expand Down
7 changes: 6 additions & 1 deletion src/Coduo/PHPHumanizer/Resources/Ordinal/EnStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

final class EnStrategy implements StrategyInterface
{
/** {@inheritdoc}*/
public function isPrefix(){
return False;
}

/** {@inheritdoc} */
public function ordinalSuffix($number)
public function ordinalIndicator($number)
{
$absNumber = abs((integer) $number);

Expand Down
19 changes: 19 additions & 0 deletions src/Coduo/PHPHumanizer/Resources/Ordinal/IdStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Coduo\PHPHumanizer\Resources\Ordinal;

use Coduo\PHPHumanizer\Number\Ordinal\StrategyInterface;

final class IdStrategy implements StrategyInterface
{
/** {@inheritdoc}*/
public function isPrefix(){
return True;
}

/** {@inheritdoc} */
public function ordinalIndicator($number)
{
return 'ke-';
}
}
7 changes: 6 additions & 1 deletion src/Coduo/PHPHumanizer/Resources/Ordinal/NlStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@

final class NlStrategy implements StrategyInterface
{
/** {@inheritdoc}*/
public function isPrefix(){
return False;
}

/** {@inheritdoc} */
public function ordinalSuffix($number)
public function ordinalIndicator($number)
{
return 'e';
}
Expand Down
94 changes: 72 additions & 22 deletions tests/Coduo/PHPHumanizer/Tests/NumberHumanizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class NumberHumanizerTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider ordinalSuffixProvider
* @dataProvider ordinalIndicatorProvider
*
* @param $expected
* @param $number
Expand All @@ -18,7 +18,7 @@ public function test_return_ordinal_suffix($expected, $number)
}

/**
* @dataProvider ordinalSuffixDutchProvider
* @dataProvider ordinalIndicatorDutchProvider
* @param $expected
* @param $number
*/
Expand All @@ -27,6 +27,16 @@ public function test_return_ordinal_suffix_dutch($expected, $number)
$this->assertEquals($expected, NumberHumanizer::ordinal($number, 'nl'));
}

/**
* @dataProvider ordinalIndicatorIndonesianProvider
* @param $expected
* @param $number
*/
public function test_return_ordinal_suffix_indonesian($expected, $number)
{
$this->assertEquals($expected, NumberHumanizer::ordinal($number, 'id'));
}

/**
* @dataProvider ordinalizeDataProvider
* @depends test_return_ordinal_suffix
Expand All @@ -51,6 +61,18 @@ public function test_ordinalize_numbers_dutch($expected, $number)
$this->assertEquals($expected, NumberHumanizer::ordinalize($number, 'nl'));
}

/**
* @dataProvider ordinalizeDataIndonesianProvider
* @depends test_return_ordinal_suffix_indonesian
*
* @param $expected
* @param $number
*/
public function test_ordinalize_numbers_indonesian($expected, $number)
{
$this->assertEquals($expected, NumberHumanizer::ordinalize($number, 'id'));
}

/**
* @dataProvider binarySuffixDataProvider
*
Expand Down Expand Up @@ -164,6 +186,48 @@ public function test_statically_throw_exception_when_converting_roman_number_is_
NumberHumanizer::fromRoman($number);
}

/**
* @return array
*/
public function ordinalIndicatorProvider()
{
return array(
array('st', 1),
array('nd', 2),
array('rd', 23),
array('nd', 1002),
array('th', -111),
);
}

/**
* @return array
*/
public function ordinalIndicatorDutchProvider()
{
return array(
array('e', 1),
array('e', 2),
array('e', 23),
array('e', 1002),
array('e', -111),
);
}

/**
* @return array
*/
public function ordinalIndicatorIndonesianProvider()
{
return array(
array('ke-', 1),
array('ke-', 2),
array('ke-', 23),
array('ke-', 1002),
array('ke-', -111),
);
}

/**
* @return array
*/
Expand Down Expand Up @@ -195,28 +259,14 @@ public function ordinalizeDataDutchProvider()
/**
* @return array
*/
public function ordinalSuffixProvider()
public function ordinalizeDataIndonesianProvider()
{
return array(
array('st', 1),
array('nd', 2),
array('rd', 23),
array('nd', 1002),
array('th', -111),
);
}

/**
* @return array
*/
public function ordinalSuffixDutchProvider()
{
return array(
array('e', 1),
array('e', 2),
array('e', 23),
array('e', 1002),
array('e', -111),
array('ke-1', 1),
array('ke-2', 2),
array('ke-23', 23),
array('ke-1002', 1002),
array('ke--111', -111),
);
}

Expand Down