Skip to content

Commit

Permalink
moved functionality to behavior, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
schmunk42 committed Jul 2, 2014
1 parent 504d9fd commit 7c04ff0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
10 changes: 6 additions & 4 deletions framework/helpers/BaseInflector.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public static function camel2words($name, $ucwords = true)
'-',
'_',
'.'
], ' ', preg_replace('/([A-Z])/', ' \0', $name))));
], ' ', preg_replace('/(?<![A-Z])[A-Z]/', ' \0', $name))));

return $ucwords ? ucwords($label) : $label;
}
Expand All @@ -326,14 +326,16 @@ public static function camel2words($name, $ucwords = true)
* For example, 'PostTag' will be converted to 'post-tag'.
* @param string $name the string to be converted
* @param string $separator the character used to concatenate the words in the ID
* @param string $strict where to insert a separator between two consecutive uppercase chars, defaults to false
* @return string the resulting ID
*/
public static function camel2id($name, $separator = '-')
public static function camel2id($name, $separator = '-', $strict = false)
{
$regex = ($strict)?'/[A-Z]/':'/(?<![A-Z])[A-Z]/';
if ($separator === '_') {
return trim(strtolower(preg_replace('/([A-Z])/', '_\0', $name)), '_');
return trim(strtolower(preg_replace($regex, '_\0', $name)), '_');
} else {
return trim(strtolower(str_replace('_', $separator, preg_replace('/([A-Z])/', $separator . '\0', $name))), $separator);
return trim(strtolower(str_replace('_', $separator, preg_replace($regex, $separator . '\0', $name))), $separator);
}
}

Expand Down
9 changes: 9 additions & 0 deletions tests/unit/framework/helpers/InflectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ public function testCamel2id()

$this->assertEquals('post-tag', Inflector::camel2id('postTag'));
$this->assertEquals('post_tag', Inflector::camel2id('postTag', '_'));

$this->assertEquals('foo-ybar', Inflector::camel2id('FooYBar', '-', false));
$this->assertEquals('foo_ybar', Inflector::camel2id('fooYBar', '_', false));

$this->assertEquals('foo-y-bar', Inflector::camel2id('FooYBar', '-', true));
$this->assertEquals('foo_y_bar', Inflector::camel2id('fooYBar', '_', true));
}

public function testId2camel()
Expand All @@ -104,6 +110,9 @@ public function testId2camel()

$this->assertEquals('PostTag', Inflector::id2camel('post-tag'));
$this->assertEquals('PostTag', Inflector::id2camel('post_tag', '_'));

$this->assertEquals('FooYBar', Inflector::id2camel('foo-y-bar'));
$this->assertEquals('FooYBar', Inflector::id2camel('foo_y_bar', '_'));
}

public function testHumanize()
Expand Down

0 comments on commit 7c04ff0

Please sign in to comment.