From 9eee3f5d88280fe7e0d90c0a2cb36b6b768874ce Mon Sep 17 00:00:00 2001 From: Michal Dabrowski Date: Tue, 27 Oct 2015 13:54:37 +0100 Subject: [PATCH] Oxford collection Updating comma Updating readme --- README.md | 14 +++ .../PHPHumanizer/Collection/FormatterSpec.php | 48 +++++++++ .../PHPHumanizer/Collection/OxfordSpec.php | 45 ++++++++ spec/Coduo/PHPHumanizer/CollectionSpec.php | 38 +++++++ src/Coduo/PHPHumanizer/Collection.php | 19 ++++ .../PHPHumanizer/Collection/Formatter.php | 102 ++++++++++++++++++ src/Coduo/PHPHumanizer/Collection/Oxford.php | 21 ++++ src/Coduo/PHPHumanizer/DateTime.php | 2 + .../PHPHumanizer/DateTime/Difference.php | 3 +- .../DateTime/Difference/CompoundResult.php | 1 - src/Coduo/PHPHumanizer/DateTime/Formatter.php | 3 +- .../DateTime/PreciseDifference.php | 4 +- .../DateTime/PreciseFormatter.php | 10 +- src/Coduo/PHPHumanizer/DateTime/Unit.php | 2 +- src/Coduo/PHPHumanizer/DateTime/Unit/Day.php | 1 + src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php | 1 + .../PHPHumanizer/DateTime/Unit/Month.php | 1 + src/Coduo/PHPHumanizer/DateTime/Unit/Week.php | 1 + src/Coduo/PHPHumanizer/DateTime/Unit/Year.php | 1 + src/Coduo/PHPHumanizer/Number.php | 6 +- src/Coduo/PHPHumanizer/Number/Ordinal.php | 6 +- .../PHPHumanizer/Number/RomanNumeral.php | 38 ++++--- .../Resources/translations/oxford.en.yml | 3 + .../Resources/translations/oxford.pl.yml | 3 + .../PHPHumanizer/String/BinarySuffix.php | 5 +- src/Coduo/PHPHumanizer/String/Humanize.php | 6 +- .../PHPHumanizer/String/MetricSuffix.php | 5 +- src/Coduo/PHPHumanizer/String/Truncate.php | 4 +- src/Coduo/PHPHumanizer/Translator/Builder.php | 4 +- 29 files changed, 351 insertions(+), 46 deletions(-) create mode 100644 spec/Coduo/PHPHumanizer/Collection/FormatterSpec.php create mode 100644 spec/Coduo/PHPHumanizer/Collection/OxfordSpec.php create mode 100644 spec/Coduo/PHPHumanizer/CollectionSpec.php create mode 100644 src/Coduo/PHPHumanizer/Collection.php create mode 100644 src/Coduo/PHPHumanizer/Collection/Formatter.php create mode 100644 src/Coduo/PHPHumanizer/Collection/Oxford.php create mode 100644 src/Coduo/PHPHumanizer/Resources/translations/oxford.en.yml create mode 100644 src/Coduo/PHPHumanizer/Resources/translations/oxford.pl.yml diff --git a/README.md b/README.md index dcfd62f..f0261db 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,20 @@ use Coduo\PHPHumanizer\Number; echo Number::metricSuffix(1240000, 'pl'); // "1,24M" ``` +## Collections + +**Oxford** + +```php +use Coduo\PHPHumanizer\Collection; + +echo Collection::oxford(['Michal', 'Norbert', 'Lukasz', 'Pawel'], 2); // "Michal, Norbert, and 2 others" +echo Collection::oxford(['Michal', 'Norbert', 'Lukasz'], 2); // "Michal, Norbert, and 1 other" +echo Collection::oxford(['Michal', 'Norbert']); // "Michal and Norbert" +``` + +Oxford is using translator component, so you can use whatever string format you like. + ## Date time **Difference** diff --git a/spec/Coduo/PHPHumanizer/Collection/FormatterSpec.php b/spec/Coduo/PHPHumanizer/Collection/FormatterSpec.php new file mode 100644 index 0000000..81164ef --- /dev/null +++ b/spec/Coduo/PHPHumanizer/Collection/FormatterSpec.php @@ -0,0 +1,48 @@ +beConstructedWith($translator); + $translator->trans( + 'only_two', + array('%first%' => 'Michal', '%second%' => 'Norbert'), + 'oxford' + )->willReturn('Michal and Norbert'); + + $translator->transChoice( + 'comma_separated_with_limit', + 1, + array('%count%' => 1, "%list%" => 'Michal, Norbert'), + 'oxford' + )->willReturn('Michal, Norbert and 1 more'); + + $translator->trans( + 'comma_separated', + array("%list%" => 'Michal, Norbert', '%last%' => "Lukasz"), + 'oxford' + )->willReturn('Michal, Norbert and Lukasz'); + } + + function it_formats_two_elements() + { + $this->format(array("Michal", "Norbert"), null)->shouldReturn("Michal and Norbert"); + } + + function it_formats_elements_with_limit() + { + $this->format(array("Michal", "Norbert", "Lukasz"), 2)->shouldReturn("Michal, Norbert and 1 more"); + } + + function it_formats_elements_without_limit() + { + $this->format(array("Michal", "Norbert", "Lukasz"), null)->shouldReturn("Michal, Norbert and Lukasz"); + } +} diff --git a/spec/Coduo/PHPHumanizer/Collection/OxfordSpec.php b/spec/Coduo/PHPHumanizer/Collection/OxfordSpec.php new file mode 100644 index 0000000..0a6b56f --- /dev/null +++ b/spec/Coduo/PHPHumanizer/Collection/OxfordSpec.php @@ -0,0 +1,45 @@ +formatter = new Formatter( + $translator->getWrappedObject() + ); + $this->beConstructedWith($this->formatter); + } + function it_returns_empty_string_when_collection_is_empty() + { + $this->format(array())->shouldReturn(''); + } + + function it_returns_collection_item_string_when_collection_has_one_element() + { + $this->format(array(new CollectionItem("Michal")))->shouldReturn('Michal'); + } +} + +class CollectionItem +{ + private $name; + + public function __construct($name) + { + $this->name = $name; + } + + public function __toString() + { + return $this->name; + } +} diff --git a/spec/Coduo/PHPHumanizer/CollectionSpec.php b/spec/Coduo/PHPHumanizer/CollectionSpec.php new file mode 100644 index 0000000..709b137 --- /dev/null +++ b/spec/Coduo/PHPHumanizer/CollectionSpec.php @@ -0,0 +1,38 @@ +oxford($example[0], $example[1])->shouldReturn($example[2]); + } + } + + function it_humanizes_collections_for_polish_locale() + { + $examples = array( + array(array("Michal"), null, 'Michal'), + array(array("Michal", "Norbert"), null, 'Michal i Norbert'), + array(array("Michal", "Norbert", "Lukasz"), 2, 'Michal, Norbert i 1 inny'), + array(array("Michal", "Norbert", "Lukasz", "Pawel"), 2, 'Michal, Norbert i 2 innych'), + ); + + foreach ($examples as $example) { + $this->oxford($example[0], $example[1], 'pl')->shouldReturn($example[2]); + } + } +} diff --git a/src/Coduo/PHPHumanizer/Collection.php b/src/Coduo/PHPHumanizer/Collection.php new file mode 100644 index 0000000..101e013 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Collection.php @@ -0,0 +1,19 @@ +format($collection, $limit); + } +} diff --git a/src/Coduo/PHPHumanizer/Collection/Formatter.php b/src/Coduo/PHPHumanizer/Collection/Formatter.php new file mode 100644 index 0000000..4a6a699 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Collection/Formatter.php @@ -0,0 +1,102 @@ +translator = $translator; + $this->catalogue = $catalogue; + } + + public function format($collection, $limit = null) + { + $count = count($collection); + + if (0 === $count) { + return ''; + } + + if (1 === $count) { + return (string) $collection[0]; + } + + if (2 === $count) { + return $this->formatOnlyTwo($collection); + } + + if (null !== $limit) { + return $this->formatCommaSeparatedWithLimit($collection, $limit, $count); + } + + return $this->formatCommaSeparated($collection, $count); + } + + /** + * @param $collection + * @param $limit + * @param $count + * + * @return string + */ + private function formatCommaSeparatedWithLimit($collection, $limit, $count) + { + $display = array_map(function ($element) { + return (string) $element; + }, array_slice($collection, 0, $limit)); + + $moreCount = $count - count($display); + + return $this->translator->transChoice('comma_separated_with_limit', $moreCount, array( + '%list%' => implode(', ', $display), + '%count%' => $moreCount, + ), $this->catalogue); + } + + /** + * @param $collection + * @param $count + * + * @return string + */ + private function formatCommaSeparated($collection, $count) + { + $display = array_map(function ($element) { + return (string) $element; + }, array_slice($collection, 0, $count - 1)); + + return $this->translator->trans('comma_separated', array( + '%list%' => implode(', ', $display), + '%last%' => (string) end($collection), + ), $this->catalogue); + } + + /** + * @param $collection + * + * @return string + */ + private function formatOnlyTwo($collection) + { + return $this->translator->trans('only_two', array( + '%first%' => (string) $collection[0], + '%second%' => (string) $collection[1], + ), $this->catalogue); + } +} diff --git a/src/Coduo/PHPHumanizer/Collection/Oxford.php b/src/Coduo/PHPHumanizer/Collection/Oxford.php new file mode 100644 index 0000000..47d89f2 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Collection/Oxford.php @@ -0,0 +1,21 @@ +formatter = $formatter; + } + + public function format($collection, $limit = null) + { + return $this->formatter->format($collection, $limit); + } +} diff --git a/src/Coduo/PHPHumanizer/DateTime.php b/src/Coduo/PHPHumanizer/DateTime.php index 4954da6..88b6aa3 100644 --- a/src/Coduo/PHPHumanizer/DateTime.php +++ b/src/Coduo/PHPHumanizer/DateTime.php @@ -13,12 +13,14 @@ class DateTime public static function difference(\DateTime $fromDate, \DateTime $toDate, $locale = 'en') { $formatter = new Formatter(Builder::build($locale)); + return $formatter->formatDifference(new Difference($fromDate, $toDate), $locale); } public static function preciseDifference(\DateTime $fromDate, \DateTime $toDate, $locale = 'en') { $formatter = new PreciseFormatter(Builder::build($locale)); + return $formatter->formatDifference(new PreciseDifference($fromDate, $toDate), $locale); } } diff --git a/src/Coduo/PHPHumanizer/DateTime/Difference.php b/src/Coduo/PHPHumanizer/DateTime/Difference.php index 0084d02..a703081 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Difference.php +++ b/src/Coduo/PHPHumanizer/DateTime/Difference.php @@ -67,7 +67,7 @@ private function calculate() new Hour(), new Minute(), new Second(), - new JustNow() + new JustNow(), ); $absoluteMilliSecondsDiff = abs($this->toDate->getTimestamp() - $this->fromDate->getTimestamp()) * 1000; @@ -86,6 +86,7 @@ private function calculate() public function isPast() { $diff = $this->toDate->getTimestamp() - $this->fromDate->getTimestamp(); + return ($diff > 0) ? false : true; } } diff --git a/src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php b/src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php index 2d7a230..af77a35 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php +++ b/src/Coduo/PHPHumanizer/DateTime/Difference/CompoundResult.php @@ -49,5 +49,4 @@ public function getUnit() { return $this->unit; } - } diff --git a/src/Coduo/PHPHumanizer/DateTime/Formatter.php b/src/Coduo/PHPHumanizer/DateTime/Formatter.php index fc6b692..8b0ec7b 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Formatter.php +++ b/src/Coduo/PHPHumanizer/DateTime/Formatter.php @@ -21,7 +21,8 @@ public function __construct(TranslatorInterface $translator) /** * @param Difference $difference - * @param string $locale + * @param string $locale + * * @return string */ public function formatDifference(Difference $difference, $locale = 'en') diff --git a/src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php b/src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php index 387c341..9425d8e 100644 --- a/src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php +++ b/src/Coduo/PHPHumanizer/DateTime/PreciseDifference.php @@ -4,11 +4,9 @@ use Coduo\PHPHumanizer\DateTime\Unit\Day; use Coduo\PHPHumanizer\DateTime\Unit\Hour; -use Coduo\PHPHumanizer\DateTime\Unit\JustNow; use Coduo\PHPHumanizer\DateTime\Unit\Minute; use Coduo\PHPHumanizer\DateTime\Unit\Month; use Coduo\PHPHumanizer\DateTime\Unit\Second; -use Coduo\PHPHumanizer\DateTime\Unit\Week; use Coduo\PHPHumanizer\DateTime\Unit\Year; use Coduo\PHPHumanizer\DateTime\Difference\CompoundResult; @@ -64,7 +62,6 @@ private function calculate() $diff = $this->fromDate->diff($this->toDate); foreach ($units as $unit) { - if ($diff->{$unit->getDateIntervalSymbol()} > 0) { $this->units[] = $unit; $this->compoundResults[] = new CompoundResult($unit, $diff->{$unit->getDateIntervalSymbol()}); @@ -75,6 +72,7 @@ private function calculate() public function isPast() { $diff = $this->toDate->getTimestamp() - $this->fromDate->getTimestamp(); + return ($diff > 0) ? false : true; } } diff --git a/src/Coduo/PHPHumanizer/DateTime/PreciseFormatter.php b/src/Coduo/PHPHumanizer/DateTime/PreciseFormatter.php index 2117bdc..251a93c 100644 --- a/src/Coduo/PHPHumanizer/DateTime/PreciseFormatter.php +++ b/src/Coduo/PHPHumanizer/DateTime/PreciseFormatter.php @@ -21,7 +21,8 @@ public function __construct(TranslatorInterface $translator) /** * @param PreciseDifference $difference - * @param string $locale + * @param string $locale + * * @return string */ public function formatDifference(PreciseDifference $difference, $locale = 'en') @@ -29,17 +30,16 @@ public function formatDifference(PreciseDifference $difference, $locale = 'en') $diff = array(); foreach ($difference->getCompoundResults() as $result) { - $diff[] = $this->translator->transChoice( - "compound." . $result->getUnit()->getName(), + 'compound.'.$result->getUnit()->getName(), $result->getQuantity(), array('%count%' => $result->getQuantity()), 'difference', $locale ); - } $suffix = $difference->isPast() ? 'compound.ago' : 'compound.from_now'; - return join(", ", $diff) . ' '. $this->translator->trans($suffix, array(), 'difference', $locale); + + return implode(', ', $diff).' '.$this->translator->trans($suffix, array(), 'difference', $locale); } } diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit.php b/src/Coduo/PHPHumanizer/DateTime/Unit.php index 34caf09..75681f5 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit.php @@ -17,7 +17,7 @@ public function getName(); public function getMilliseconds(); /** - * Returns symbol of \DateInterval equivalent + * Returns symbol of \DateInterval equivalent. * * @return string */ diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit/Day.php b/src/Coduo/PHPHumanizer/DateTime/Unit/Day.php index fec6329..fe12aad 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit/Day.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit/Day.php @@ -17,6 +17,7 @@ public function getName() public function getMilliseconds() { $hour = new Hour(); + return $hour->getMilliseconds() * 24; } diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php b/src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php index 2dac8c9..dd33269 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit/Hour.php @@ -17,6 +17,7 @@ public function getName() public function getMilliseconds() { $minute = new Minute(); + return $minute->getMilliseconds() * 60; } diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit/Month.php b/src/Coduo/PHPHumanizer/DateTime/Unit/Month.php index 50e4ca8..8f94e68 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit/Month.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit/Month.php @@ -17,6 +17,7 @@ public function getName() public function getMilliseconds() { $day = new Day(); + return $day->getMilliseconds() * 30; } diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit/Week.php b/src/Coduo/PHPHumanizer/DateTime/Unit/Week.php index 0bd4921..a35f188 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit/Week.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit/Week.php @@ -17,6 +17,7 @@ public function getName() public function getMilliseconds() { $day = new Day(); + return $day->getMilliseconds() * 7; } diff --git a/src/Coduo/PHPHumanizer/DateTime/Unit/Year.php b/src/Coduo/PHPHumanizer/DateTime/Unit/Year.php index 61c3e09..d2185a0 100644 --- a/src/Coduo/PHPHumanizer/DateTime/Unit/Year.php +++ b/src/Coduo/PHPHumanizer/DateTime/Unit/Year.php @@ -17,6 +17,7 @@ public function getName() public function getMilliseconds() { $day = new Day(); + return $day->getMilliseconds() * 356; } diff --git a/src/Coduo/PHPHumanizer/Number.php b/src/Coduo/PHPHumanizer/Number.php index 9ad0c81..560ed86 100644 --- a/src/Coduo/PHPHumanizer/Number.php +++ b/src/Coduo/PHPHumanizer/Number.php @@ -11,7 +11,7 @@ class Number { public static function ordinalize($number) { - return $number . self::ordinal($number); + return $number.self::ordinal($number); } public static function ordinal($number) @@ -22,24 +22,28 @@ public static function ordinal($number) public static function binarySuffix($number, $locale = 'en') { $binarySuffix = new BinarySuffix($number, $locale); + return $binarySuffix->convert(); } public static function metricSuffix($number, $locale = 'en') { $binarySuffix = new MetricSuffix($number, $locale); + return $binarySuffix->convert(); } public static function toRoman($number) { $romanNumeral = new RomanNumeral(); + return $romanNumeral->toRoman($number); } public function fromRoman($number) { $romanNumeral = new RomanNumeral(); + return $romanNumeral->fromRoman($number); } } diff --git a/src/Coduo/PHPHumanizer/Number/Ordinal.php b/src/Coduo/PHPHumanizer/Number/Ordinal.php index 21a2ca6..f1ae6be 100644 --- a/src/Coduo/PHPHumanizer/Number/Ordinal.php +++ b/src/Coduo/PHPHumanizer/Number/Ordinal.php @@ -5,12 +5,12 @@ class Ordinal { /** - * @var int|double + * @var int|float */ private $number; /** - * @param int|double $number + * @param int|float $number */ public function __construct($number) { @@ -21,7 +21,7 @@ public function __toString() { $absNumber = abs((integer) $this->number); - if (in_array(($absNumber % 100), array(11,12,13))) { + if (in_array(($absNumber % 100), array(11, 12, 13))) { return 'th'; } diff --git a/src/Coduo/PHPHumanizer/Number/RomanNumeral.php b/src/Coduo/PHPHumanizer/Number/RomanNumeral.php index b568d7e..8b75359 100644 --- a/src/Coduo/PHPHumanizer/Number/RomanNumeral.php +++ b/src/Coduo/PHPHumanizer/Number/RomanNumeral.php @@ -6,28 +6,29 @@ class RomanNumeral { const MIN_VALUE = 1; const MAX_VALUE = 3999; - const ROMAN_STRING_MATCHER = "/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/"; + const ROMAN_STRING_MATCHER = '/^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/'; private $map = array( - 'M' => 1000, + 'M' => 1000, 'CM' => 900, - 'D' => 500, + 'D' => 500, 'CD' => 400, - 'C' => 100, + 'C' => 100, 'XC' => 90, - 'L' => 50, + 'L' => 50, 'XL' => 40, - 'X' => 10, + 'X' => 10, 'IX' => 9, - 'V' => 5, + 'V' => 5, 'IV' => 4, - 'I' => 1 + 'I' => 1, ); - /** * @param $number + * * @return string + * * @throws \InvalidArgumentException */ public function toRoman($number) @@ -38,12 +39,9 @@ public function toRoman($number) $romanString = ''; - while($number > 0) - { - foreach($this->map as $key => $value) - { - if($number >= $value) - { + while ($number > 0) { + foreach ($this->map as $key => $value) { + if ($number >= $value) { $romanString .= $key; $number -= $value; break; @@ -56,7 +54,9 @@ public function toRoman($number) /** * @param $string + * * @return int + * * @throws \InvalidArgumentException */ public function fromRoman($string) @@ -66,10 +66,9 @@ public function fromRoman($string) } $total = 0; - $i = strlen($string); + $i = strlen($string); - while ($i > 0) - { + while ($i > 0) { $digit = $this->map[$string{--$i}]; if ($i > 0) { @@ -77,7 +76,7 @@ public function fromRoman($string) if ($previousDigit < $digit) { $digit -= $previousDigit; - $i--; + --$i; } } @@ -86,5 +85,4 @@ public function fromRoman($string) return $total; } - } diff --git a/src/Coduo/PHPHumanizer/Resources/translations/oxford.en.yml b/src/Coduo/PHPHumanizer/Resources/translations/oxford.en.yml new file mode 100644 index 0000000..22eb5cf --- /dev/null +++ b/src/Coduo/PHPHumanizer/Resources/translations/oxford.en.yml @@ -0,0 +1,3 @@ +only_two: "%first% and %second%" +comma_separated: "%list%, and %last%" +comma_separated_with_limit: "{1} %list%, and 1 other|[2,Inf] %list%, and %count% others" \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/Resources/translations/oxford.pl.yml b/src/Coduo/PHPHumanizer/Resources/translations/oxford.pl.yml new file mode 100644 index 0000000..6a999c0 --- /dev/null +++ b/src/Coduo/PHPHumanizer/Resources/translations/oxford.pl.yml @@ -0,0 +1,3 @@ +only_two: "%first% i %second%" +comma_separated: "%list% i %last%" +comma_separated_with_limit: "{1} %list% i 1 inny|[2,Inf] %list% i %count% innych" \ No newline at end of file diff --git a/src/Coduo/PHPHumanizer/String/BinarySuffix.php b/src/Coduo/PHPHumanizer/String/BinarySuffix.php index a718888..446f642 100644 --- a/src/Coduo/PHPHumanizer/String/BinarySuffix.php +++ b/src/Coduo/PHPHumanizer/String/BinarySuffix.php @@ -25,18 +25,19 @@ class BinarySuffix 1073741824 => '#.## GB', 1048576 => '#.## MB', 1024 => '#.# kB', - 0 => '# bytes' + 0 => '# bytes', ); /** * @param $number * @param string $locale + * * @throws \InvalidArgumentException */ public function __construct($number, $locale = 'en') { if (!is_numeric($number)) { - throw new \InvalidArgumentException("Binary suffix converter accept only numeric values."); + throw new \InvalidArgumentException('Binary suffix converter accept only numeric values.'); } $this->number = (int) $number; diff --git a/src/Coduo/PHPHumanizer/String/Humanize.php b/src/Coduo/PHPHumanizer/String/Humanize.php index 5c2f7ea..3ec9c58 100644 --- a/src/Coduo/PHPHumanizer/String/Humanize.php +++ b/src/Coduo/PHPHumanizer/String/Humanize.php @@ -15,7 +15,7 @@ class Humanize private $text; /** - * @var boolean + * @var bool */ private $capitalize; @@ -31,12 +31,14 @@ public function __construct($text, $capitalize = true) /** * @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)); + $humanized = trim(str_replace($this->forbiddenWords, '', $humanized)); + return $this->capitalize ? ucfirst($humanized) : $humanized; } } diff --git a/src/Coduo/PHPHumanizer/String/MetricSuffix.php b/src/Coduo/PHPHumanizer/String/MetricSuffix.php index 5749712..513ddac 100644 --- a/src/Coduo/PHPHumanizer/String/MetricSuffix.php +++ b/src/Coduo/PHPHumanizer/String/MetricSuffix.php @@ -25,18 +25,19 @@ class MetricSuffix 1000000000 => '#.##G', 1000000 => '#.##M', 1000 => '#.#k', - 0 => '#.#' + 0 => '#.#', ); /** * @param $number * @param string $locale + * * @throws \InvalidArgumentException */ public function __construct($number, $locale = 'en') { if (!is_numeric($number)) { - throw new \InvalidArgumentException("Metric suffix converter accept only numeric values."); + throw new \InvalidArgumentException('Metric suffix converter accept only numeric values.'); } $this->number = (int) $number; diff --git a/src/Coduo/PHPHumanizer/String/Truncate.php b/src/Coduo/PHPHumanizer/String/Truncate.php index e063468..9bbd369 100644 --- a/src/Coduo/PHPHumanizer/String/Truncate.php +++ b/src/Coduo/PHPHumanizer/String/Truncate.php @@ -21,7 +21,7 @@ class Truncate /** * @param string $text - * @param int $charactersCount + * @param int $charactersCount * @param string $append */ public function __construct($text, $charactersCount, $append = '') @@ -42,6 +42,6 @@ public function __toString() $length = $breakpoint; } - return rtrim(mb_substr($this->text, 0, $length)) . $this->append; + return rtrim(mb_substr($this->text, 0, $length)).$this->append; } } diff --git a/src/Coduo/PHPHumanizer/Translator/Builder.php b/src/Coduo/PHPHumanizer/Translator/Builder.php index 79e4809..7131ed1 100644 --- a/src/Coduo/PHPHumanizer/Translator/Builder.php +++ b/src/Coduo/PHPHumanizer/Translator/Builder.php @@ -12,10 +12,10 @@ public static function build($locale) $translator = new Translator($locale); $translator->addLoader('yml', new YamlFileLoader()); - $iterator = new \FilesystemIterator(__DIR__ . "/../Resources/translations"); + $iterator = new \FilesystemIterator(__DIR__.'/../Resources/translations'); $filter = new \RegexIterator($iterator, '/[aA-zZ]+\.([a-z]{2}|[a-z]{2}\_[A-Z]{2})\.yml$/'); - foreach($filter as $file) { + foreach ($filter as $file) { /* @var $file \SplFileInfo */ $resourceName = $file->getBasename('.yml'); list($fileDomain, $fileLocale) = explode('.', $resourceName);