Skip to content

Commit

Permalink
Merge pull request #73 from isnani/improve-ordinal-strategy
Browse files Browse the repository at this point in the history
improve strategy to handle prefix-ordinal
  • Loading branch information
Norbert Orzechowicz committed Jan 21, 2016
2 parents dac4b43 + e2c5fde commit 6ec963f
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 30 deletions.
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);
}
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

0 comments on commit 6ec963f

Please sign in to comment.