diff --git a/composer.phar b/composer.phar new file mode 100755 index 0000000..502d1e7 Binary files /dev/null and b/composer.phar differ diff --git a/spec/Coduo/PHPHumanizer/StringSpec.php b/spec/Coduo/PHPHumanizer/StringSpec.php index 0143dc2..a360bff 100644 --- a/spec/Coduo/PHPHumanizer/StringSpec.php +++ b/spec/Coduo/PHPHumanizer/StringSpec.php @@ -2,6 +2,7 @@ namespace spec\Coduo\PHPHumanizer; +use Coduo\PHPHumanizer\String\Humanize; use PhpSpec\ObjectBehavior; use Prophecy\Argument; @@ -12,6 +13,11 @@ function it_humanize_string() $this->humanize('news_count')->shouldReturn('News count'); } + function it_humanize_string_with_special_character() + { + $this->humanize('news-count', true, '-')->shouldReturn('News count'); + } + function it_humanize_strings_without_capitalize() { $this->humanize('user', false)->shouldReturn('user'); diff --git a/src/Coduo/PHPHumanizer/String.php b/src/Coduo/PHPHumanizer/String.php index a9a268b..38dd31b 100644 --- a/src/Coduo/PHPHumanizer/String.php +++ b/src/Coduo/PHPHumanizer/String.php @@ -7,9 +7,9 @@ class String { - public static function humanize($text, $capitalize = true) + public static function humanize($text, $capitalize = true, $separator = '_') { - return (string) new Humanize($text, $capitalize); + return (string) new Humanize($text, $capitalize, $separator); } public static function truncate($text, $charactersCount, $append = '') diff --git a/src/Coduo/PHPHumanizer/String/Humanize.php b/src/Coduo/PHPHumanizer/String/Humanize.php index 5da7e8f..d4eaa17 100644 --- a/src/Coduo/PHPHumanizer/String/Humanize.php +++ b/src/Coduo/PHPHumanizer/String/Humanize.php @@ -15,30 +15,35 @@ class Humanize private $text; /** - * @var bool + * @var boolean */ private $capitalize; + /** + * @var string + */ + private $separator; + /** * @param $text * @param bool $capitalize + * @param string $separator */ - public function __construct($text, $capitalize = true) + public function __construct($text, $capitalize = true, $separator = '_') { $this->text = $text; $this->capitalize = $capitalize; + $this->separator = $separator; } /** * @internal param bool $capitalize - * * @return string */ public function __toString() { - $humanized = trim(mb_strtolower(preg_replace(array('/([A-Z])/', '/[_\s]+/'), array('_$1', ' '), $this->text))); - $humanized = trim(str_replace($this->forbiddenWords, '', $humanized)); - + $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; } }