Skip to content
Permalink
Browse files

Add Inflector::hyphenate().

  • Loading branch information...
ADmad committed Sep 1, 2014
1 parent 081f271 commit a9e008ff889e253ab9484c3614ce92a5aa3e24de
Showing with 35 additions and 0 deletions.
  1. +17 −0 src/Utility/Inflector.php
  2. +18 −0 tests/TestCase/Utility/InflectorTest.php
@@ -598,6 +598,23 @@ public static function underscore($camelCasedWord) {
return $result;
}
/**
* Returns the given underscored_word or CamelCasedWord as an hyphenated-word.
*
* @param string $word The word to hyphenate.
* @return string Hyphenated version of the word.
*/

This comment has been minimized.

Copy link
@thaJeztah

thaJeztah Sep 1, 2014

Maybe use a more descriptive argument-name, like in underscore()? Additionally, 'word' (singular) seems a bit odd; hyphenating a single 'word' generally doesn't change much 😄

public static function hyphenate($word) {
$result = static::_cache(__FUNCTION__, $word);
if ($result !== false) {
return $result;
}

This comment has been minimized.

Copy link
@thaJeztah

thaJeztah Sep 1, 2014

Maybe I'm nitpicking, but why call underscore() and then replace? Maybe both underscore() and hyphenate() should be refactored to be wrappers for a private internal method that does the conversion, but takes an extra $separator argument that can be set to - or _ respectively?

This comment has been minimized.

Copy link
@ADmad

ADmad Sep 1, 2014

Author Member

What would be the benefit of doing that?

P.S. Please comment on the PR not the commit.

This comment has been minimized.

Copy link
@thaJeztah

thaJeztah Sep 1, 2014

Right now, you're basically replacing twice, first camelcase to underscore, then underscore to dash.

Having an internal helper-method that does the right conversion, given the right parameters, is cleaner.

Sorry for commenting here, must have opened the wrong tab 😄

$result = str_replace('_', '-', static::underscore($word));
static::_cache(__FUNCTION__, $word, $result);
return $result;
}
/**
* Returns the given underscored_word_group as a Human Readable Word Group.
* (Underscores are replaced by spaces and capitalized following words.)
@@ -375,6 +375,24 @@ public function testInflectorUnderscore() {
$this->assertSame(Inflector::underscore(false), '');
}
/**
* testInflectorHyphenate method
*
* @return void
*/
public function testInflectorHyphenate() {
$this->assertSame('test-thing', Inflector::hyphenate('TestThing'));
$this->assertSame('test-thing', Inflector::hyphenate('testThing'));
$this->assertSame('test-thing-extra', Inflector::hyphenate('TestThingExtra'));
$this->assertSame('test-thing-extra', Inflector::hyphenate('testThingExtra'));
$this->assertSame('test-this-thing', Inflector::hyphenate('test_this_thing'));
// Test stupid values
$this->assertSame('', Inflector::hyphenate(''));
$this->assertSame('0', Inflector::hyphenate(0));
$this->assertSame('', Inflector::hyphenate(false));
}
/**
* testVariableNaming method
*

0 comments on commit a9e008f

Please sign in to comment.
You can’t perform that action at this time.