Skip to content

Commit

Permalink
Allow directly specifying the separator for camelize
Browse files Browse the repository at this point in the history
  • Loading branch information
AD7six committed Feb 8, 2015
1 parent 77bd88d commit 9a0e755
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/Utility/Inflector.php
Expand Up @@ -579,15 +579,21 @@ public static function singularize($word)
* Returns the given lower_case_and_underscored_word as a CamelCased word.
*
* @param string $string Word to camelize
* @param string $replacement the delimiter in the input string
* @return string Camelized word. LikeThis.
* @link http://book.cakephp.org/3.0/en/core-libraries/inflector.html#creating-camelcase-and-under-scored-forms
*/
public static function camelize($string)
public static function camelize($string, $replacement = '_')
{
if (!($result = static::_cache(__FUNCTION__, $string))) {
$result = str_replace(' ', '', static::humanize($string));
$cacheKey = __FUNCTION__ . $replacement;

$result = static::_cache($cacheKey, $string);

if ($result === false) {
$result = str_replace(' ', '', static::humanize($string, $replacement));
static::_cache(__FUNCTION__, $string, $result);
}

return $result;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Utility/InflectorTest.php
Expand Up @@ -410,6 +410,26 @@ public function testDasherized()
$this->assertSame('', Inflector::dasherize(false));
}

/**
* Demonstrate the expected output for bad inputs
*
* @return void
*/
public function testCamelize()
{
$this->assertSame('TestThing', Inflector::camelize('test_thing'));
$this->assertSame('Test-thing', Inflector::camelize('test-thing'));
$this->assertSame('TestThing', Inflector::camelize('test thing'));

$this->assertSame('Test_thing', Inflector::camelize('test_thing', '-'));
$this->assertSame('TestThing', Inflector::camelize('test-thing', '-'));
$this->assertSame('TestThing', Inflector::camelize('test thing', '-'));

$this->assertSame('Test_thing', Inflector::camelize('test_thing', ' '));
$this->assertSame('Test-thing', Inflector::camelize('test-thing', ' '));
$this->assertSame('TestThing', Inflector::camelize('test thing', ' '));
}

/**
* testVariableNaming method
*
Expand Down

0 comments on commit 9a0e755

Please sign in to comment.