From 65564e1202be4e67822e40c366dd36836bdec70a Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Tue, 22 Apr 2014 23:12:31 +0200 Subject: [PATCH] Refactoring --- src/PHPHumanizer/Number.php | 18 ++---------- src/PHPHumanizer/Number/Ordinal.php | 35 +++++++++++++++++++++++ src/PHPHumanizer/String.php | 8 ++---- src/PHPHumanizer/String/Humanize.php | 42 ++++++++++++++++++++++++++++ 4 files changed, 83 insertions(+), 20 deletions(-) create mode 100644 src/PHPHumanizer/Number/Ordinal.php create mode 100644 src/PHPHumanizer/String/Humanize.php diff --git a/src/PHPHumanizer/Number.php b/src/PHPHumanizer/Number.php index 2bbd469..bbd8f7f 100644 --- a/src/PHPHumanizer/Number.php +++ b/src/PHPHumanizer/Number.php @@ -2,9 +2,10 @@ namespace PHPHumanizer; +use PHPHumanizer\Number\Ordinal; + class Number { - public static function ordinalize($number) { return $number . self::oridinal($number); @@ -12,19 +13,6 @@ public static function ordinalize($number) public static function oridinal($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'; - } + return (string) new Ordinal($number); } - - } diff --git a/src/PHPHumanizer/Number/Ordinal.php b/src/PHPHumanizer/Number/Ordinal.php new file mode 100644 index 0000000..e586961 --- /dev/null +++ b/src/PHPHumanizer/Number/Ordinal.php @@ -0,0 +1,35 @@ +number = $number; + } + + 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'; + } + } +} diff --git a/src/PHPHumanizer/String.php b/src/PHPHumanizer/String.php index cfed781..94b2460 100644 --- a/src/PHPHumanizer/String.php +++ b/src/PHPHumanizer/String.php @@ -2,14 +2,12 @@ namespace PHPHumanizer; +use PHPHumanizer\String\Humanize; + class String { - protected static $forbiddenWords = array('id'); - public static function humanize($text, $capitalize = true) { - $humanized = trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $text))); - $humanized = trim(str_replace(self::$forbiddenWords, "", $humanized)); - return $capitalize ? ucfirst($humanized) : $humanized; + return (string) new Humanize($text, $capitalize); } } diff --git a/src/PHPHumanizer/String/Humanize.php b/src/PHPHumanizer/String/Humanize.php new file mode 100644 index 0000000..443b8e4 --- /dev/null +++ b/src/PHPHumanizer/String/Humanize.php @@ -0,0 +1,42 @@ +text = $text; + $this->capitalize = $capitalize; + } + + /** + * @internal param bool $capitalize + * @return string + */ + public function __toString() + { + $humanized = trim(strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $this->text))); + $humanized = trim(str_replace($this->forbiddenWords, "", $humanized)); + return $this->capitalize ? ucfirst($humanized) : $humanized; + } +}