-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from doenietzomoeilijk/master
Refactor Number into separate languages
- Loading branch information
Showing
8 changed files
with
171 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\Number\Ordinal; | ||
|
||
/** | ||
* Tries to find a proper strategy for ordinal numbers. | ||
*/ | ||
class Builder | ||
{ | ||
/** | ||
* @param string $locale | ||
* @return StrategyInterface | ||
* @throws \RuntimeException | ||
*/ | ||
public static function build($locale) | ||
{ | ||
// $locale should be xx or xx_YY | ||
if (!preg_match('/^([a-z]{2})(_([A-Z]{2}))?$/', $locale, $m)) { | ||
throw new \RuntimeException("Invalid locale specified: '$locale'."); | ||
} | ||
|
||
$strategy = ucfirst($m[1]); | ||
if (!empty($m[3])) { | ||
$strategy .= "_$m[3]"; | ||
} | ||
|
||
$strategy = "\\Coduo\\PHPHumanizer\\Resources\\Ordinal\\{$strategy}Strategy"; | ||
|
||
if (class_exists($strategy)) { | ||
return new $strategy; | ||
} | ||
|
||
// Debatable: should we fallback to English? | ||
// return self::build('en'); | ||
throw new \RuntimeException("Strategy for locale $locale not found."); | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/Coduo/PHPHumanizer/Number/Ordinal/StrategyInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\Number\Ordinal; | ||
|
||
interface StrategyInterface | ||
{ | ||
/** | ||
* @param int|float $number | ||
* @return string | ||
*/ | ||
public function ordinalSuffix($number); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\Resources\Ordinal; | ||
|
||
use Coduo\PHPHumanizer\Number\Ordinal\StrategyInterface; | ||
|
||
class EnStrategy implements StrategyInterface | ||
{ | ||
/** @inheritdoc */ | ||
public function ordinalSuffix($number) | ||
{ | ||
$absNumber = abs((integer) $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'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace Coduo\PHPHumanizer\Resources\Ordinal; | ||
|
||
use Coduo\PHPHumanizer\Number\Ordinal\StrategyInterface; | ||
|
||
class NlStrategy implements StrategyInterface | ||
{ | ||
/** @inheritdoc */ | ||
public function ordinalSuffix($number) | ||
{ | ||
return "e"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters