From a562115f6cf627dc408981568f9d425368da1d75 Mon Sep 17 00:00:00 2001 From: Norbert Orzechowicz Date: Sun, 1 Nov 2015 13:56:22 +0100 Subject: [PATCH] Added possibility to pass forbidden words into humanizer --- src/Coduo/PHPHumanizer/String.php | 17 +++++++++++-- src/Coduo/PHPHumanizer/String/Humanize.php | 10 ++++---- tests/Coduo/PHPHumanizer/Tests/StringTest.php | 24 ++++++++++--------- 3 files changed, 32 insertions(+), 19 deletions(-) diff --git a/src/Coduo/PHPHumanizer/String.php b/src/Coduo/PHPHumanizer/String.php index 38dd31b..3eed429 100644 --- a/src/Coduo/PHPHumanizer/String.php +++ b/src/Coduo/PHPHumanizer/String.php @@ -7,11 +7,24 @@ class String { - public static function humanize($text, $capitalize = true, $separator = '_') + /** + * @param $text + * @param bool|true $capitalize + * @param string $separator + * @param array $forbiddenWords + * @return string + */ + public static function humanize($text, $capitalize = true, $separator = '_', array $forbiddenWords = array()) { - return (string) new Humanize($text, $capitalize, $separator); + return (string) new Humanize($text, $capitalize, $separator, $forbiddenWords); } + /** + * @param $text + * @param $charactersCount + * @param string $append + * @return string + */ public static function truncate($text, $charactersCount, $append = '') { return (string) new Truncate($text, $charactersCount, $append); diff --git a/src/Coduo/PHPHumanizer/String/Humanize.php b/src/Coduo/PHPHumanizer/String/Humanize.php index d4eaa17..c58fa03 100644 --- a/src/Coduo/PHPHumanizer/String/Humanize.php +++ b/src/Coduo/PHPHumanizer/String/Humanize.php @@ -4,11 +4,6 @@ class Humanize { - /** - * @var array - */ - private $forbiddenWords = array('id'); - /** * @var string */ @@ -28,12 +23,14 @@ class Humanize * @param $text * @param bool $capitalize * @param string $separator + * @param array $forbiddenWords */ - public function __construct($text, $capitalize = true, $separator = '_') + public function __construct($text, $capitalize = true, $separator = '_', array $forbiddenWords = array('id')) { $this->text = $text; $this->capitalize = $capitalize; $this->separator = $separator; + $this->forbiddenWords = $forbiddenWords; } /** @@ -44,6 +41,7 @@ public function __toString() { $humanized = trim(strtolower(preg_replace(array('/([A-Z])/', "/[{$this->separator}\\s]+/"), array('_$1', ' '), $this->text))); $humanized = trim(str_replace($this->forbiddenWords, "", $humanized)); + return $this->capitalize ? ucfirst($humanized) : $humanized; } } diff --git a/tests/Coduo/PHPHumanizer/Tests/StringTest.php b/tests/Coduo/PHPHumanizer/Tests/StringTest.php index 6abb9d1..eef471a 100644 --- a/tests/Coduo/PHPHumanizer/Tests/StringTest.php +++ b/tests/Coduo/PHPHumanizer/Tests/StringTest.php @@ -14,18 +14,19 @@ class StringTest extends PHPUnit_Framework_TestCase * @param $expected * @param $capitalize * @param $separator + * @param array $forbiddenWords */ - public function test_humanize_strings($input, $expected, $capitalize, $separator) + public function test_humanize_strings($input, $expected, $capitalize, $separator, array $forbiddenWords) { - $this->assertEquals($expected, String::humanize($input, $capitalize, $separator)); + $this->assertEquals($expected, String::humanize($input, $capitalize, $separator, $forbiddenWords)); } /** * @dataProvider truncateStringProvider * - * @param $text - * @param $expected - * @param $charactersCount + * @param $text + * @param $expected + * @param $charactersCount * @param string $append */ function test_truncate_string_to_word_closest_to_a_certain_number_of_characters($text, $expected, $charactersCount, $append = '') @@ -40,12 +41,13 @@ function test_truncate_string_to_word_closest_to_a_certain_number_of_characters( public function humanizeStringProvider() { return array( - array('news_count', 'News count', true, '_'), - array('user', 'user', false, '_'), - array('news_id', 'News', true, '_'), - array('news_count', 'News count', true, '_'), - array('news-count', 'News count', true, '-'), - array('news-count', 'news count', false, '-') + array('news_count', 'News count', true, '_', array('id')), + array('user', 'user', false, '_', array('id')), + array('news_id', 'News', true, '_', array('id')), + array('customer_id', 'Customer id', true, '_', array()), + array('news_count', 'News count', true, '_', array('id')), + array('news-count', 'News count', true, '-', array('id')), + array('news-count', 'news count', false, '-', array('id')) ); }