Skip to content

Commit 4a6ed1f

Browse files
author
Norbert Orzechowicz
committed
Merge pull request coduo#51 from norzechowicz/humanizer-forbidden-words
Added possibility to pass forbidden words into humanizer
2 parents c0606e6 + a562115 commit 4a6ed1f

File tree

3 files changed

+32
-19
lines changed

3 files changed

+32
-19
lines changed

src/Coduo/PHPHumanizer/String.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,24 @@
77

88
class String
99
{
10-
public static function humanize($text, $capitalize = true, $separator = '_')
10+
/**
11+
* @param $text
12+
* @param bool|true $capitalize
13+
* @param string $separator
14+
* @param array $forbiddenWords
15+
* @return string
16+
*/
17+
public static function humanize($text, $capitalize = true, $separator = '_', array $forbiddenWords = array())
1118
{
12-
return (string) new Humanize($text, $capitalize, $separator);
19+
return (string) new Humanize($text, $capitalize, $separator, $forbiddenWords);
1320
}
1421

22+
/**
23+
* @param $text
24+
* @param $charactersCount
25+
* @param string $append
26+
* @return string
27+
*/
1528
public static function truncate($text, $charactersCount, $append = '')
1629
{
1730
return (string) new Truncate($text, $charactersCount, $append);

src/Coduo/PHPHumanizer/String/Humanize.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44

55
class Humanize
66
{
7-
/**
8-
* @var array
9-
*/
10-
private $forbiddenWords = array('id');
11-
127
/**
138
* @var string
149
*/
@@ -28,12 +23,14 @@ class Humanize
2823
* @param $text
2924
* @param bool $capitalize
3025
* @param string $separator
26+
* @param array $forbiddenWords
3127
*/
32-
public function __construct($text, $capitalize = true, $separator = '_')
28+
public function __construct($text, $capitalize = true, $separator = '_', array $forbiddenWords = array('id'))
3329
{
3430
$this->text = $text;
3531
$this->capitalize = $capitalize;
3632
$this->separator = $separator;
33+
$this->forbiddenWords = $forbiddenWords;
3734
}
3835

3936
/**
@@ -44,6 +41,7 @@ public function __toString()
4441
{
4542
$humanized = trim(strtolower(preg_replace(array('/([A-Z])/', "/[{$this->separator}\\s]+/"), array('_$1', ' '), $this->text)));
4643
$humanized = trim(str_replace($this->forbiddenWords, "", $humanized));
44+
4745
return $this->capitalize ? ucfirst($humanized) : $humanized;
4846
}
4947
}

tests/Coduo/PHPHumanizer/Tests/StringTest.php

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,19 @@ class StringTest extends PHPUnit_Framework_TestCase
1414
* @param $expected
1515
* @param $capitalize
1616
* @param $separator
17+
* @param array $forbiddenWords
1718
*/
18-
public function test_humanize_strings($input, $expected, $capitalize, $separator)
19+
public function test_humanize_strings($input, $expected, $capitalize, $separator, array $forbiddenWords)
1920
{
20-
$this->assertEquals($expected, String::humanize($input, $capitalize, $separator));
21+
$this->assertEquals($expected, String::humanize($input, $capitalize, $separator, $forbiddenWords));
2122
}
2223

2324
/**
2425
* @dataProvider truncateStringProvider
2526
*
26-
* @param $text
27-
* @param $expected
28-
* @param $charactersCount
27+
* @param $text
28+
* @param $expected
29+
* @param $charactersCount
2930
* @param string $append
3031
*/
3132
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(
4041
public function humanizeStringProvider()
4142
{
4243
return array(
43-
array('news_count', 'News count', true, '_'),
44-
array('user', 'user', false, '_'),
45-
array('news_id', 'News', true, '_'),
46-
array('news_count', 'News count', true, '_'),
47-
array('news-count', 'News count', true, '-'),
48-
array('news-count', 'news count', false, '-')
44+
array('news_count', 'News count', true, '_', array('id')),
45+
array('user', 'user', false, '_', array('id')),
46+
array('news_id', 'News', true, '_', array('id')),
47+
array('customer_id', 'Customer id', true, '_', array()),
48+
array('news_count', 'News count', true, '_', array('id')),
49+
array('news-count', 'News count', true, '-', array('id')),
50+
array('news-count', 'news count', false, '-', array('id'))
4951
);
5052
}
5153

0 commit comments

Comments
 (0)