Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for multiple character separators at string humanizer #49

Merged
merged 2 commits into from
Nov 1, 2015
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
Binary file added composer.phar
Binary file not shown.
6 changes: 6 additions & 0 deletions spec/Coduo/PHPHumanizer/StringSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace spec\Coduo\PHPHumanizer;

use Coduo\PHPHumanizer\String\Humanize;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions src/Coduo/PHPHumanizer/String.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '')
Expand Down
17 changes: 11 additions & 6 deletions src/Coduo/PHPHumanizer/String/Humanize.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}