Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/Coduo/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 4 additions & 6 deletions src/Coduo/PHPHumanizer/String/Humanize.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@

class Humanize
{
/**
* @var array
*/
private $forbiddenWords = array('id');

/**
* @var string
*/
Expand All @@ -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;
}

/**
Expand All @@ -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;
}
}
24 changes: 13 additions & 11 deletions tests/Coduo/PHPHumanizer/Tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')
Expand All @@ -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'))
);
}

Expand Down