-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Str::camelCase(), Str::studlyCase(), Str::pascalCase, Str::snak…
…eCase() and Str::kebabCase() methods
- Loading branch information
Showing
4 changed files
with
213 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace PHLAK\Twine\Traits; | ||
|
||
trait Caseable | ||
{ | ||
/** | ||
* Convert the string to camelCase. | ||
* | ||
* @return self | ||
*/ | ||
public function camelCase() : self | ||
{ | ||
$words = array_map(function ($word) { | ||
return ucfirst(strtolower($word)); | ||
}, $this->words()); | ||
|
||
return new static(lcfirst(implode('', $words))); | ||
} | ||
|
||
/** | ||
* Convert the string to StudlyCase. | ||
* | ||
* @return self | ||
*/ | ||
public function studlyCase() : self | ||
{ | ||
$words = array_map(function ($word) { | ||
return ucfirst(strtolower($word)); | ||
}, $this->words()); | ||
|
||
return new static(implode('', $words)); | ||
} | ||
|
||
/** | ||
* Convert the string to PascalCase. | ||
* | ||
* @return self | ||
*/ | ||
public function pascalCase() : self | ||
{ | ||
return $this->studlyCase(); | ||
} | ||
|
||
/** | ||
* Convert the string to snake_case. | ||
* | ||
* @return self | ||
*/ | ||
public function snakeCase() : self | ||
{ | ||
$words = array_map(function ($word) { | ||
return strtolower($word); | ||
}, $this->words()); | ||
|
||
return new static(implode('_', $words)); | ||
} | ||
|
||
/** | ||
* Convert the string to kebab-case. | ||
* | ||
* @return self | ||
*/ | ||
public function kebabCase() : self | ||
{ | ||
$words = array_map(function ($word) { | ||
return strtolower($word); | ||
}, $this->words()); | ||
|
||
return new static(implode('-', $words)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace PHLAK\Twine\Tests; | ||
|
||
use PHLAK\Twine; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CaseableTest extends TestCase | ||
{ | ||
public function test_it_can_convert_to_camel_case() | ||
{ | ||
$string = new Twine\Str('john pinkerton'); | ||
|
||
$camelCase = $string->camelCase(); | ||
|
||
$this->assertEquals('johnPinkerton', $camelCase); | ||
} | ||
|
||
public function test_it_can_convert_to_studly_case() | ||
{ | ||
$string = new Twine\Str('john pinkerton'); | ||
|
||
$studlyCase = $string->studlyCase(); | ||
|
||
$this->assertEquals('JohnPinkerton', $studlyCase); | ||
} | ||
|
||
public function test_it_can_convert_to_pascal_case() | ||
{ | ||
$string = new Twine\Str('john pinkerton'); | ||
|
||
$pascalCase = $string->pascalCase(); | ||
|
||
$this->assertEquals('JohnPinkerton', $pascalCase); | ||
} | ||
|
||
public function test_it_can_convert_to_snake_case() | ||
{ | ||
$string = new Twine\Str('john pinkerton'); | ||
|
||
$snakeCase = $string->snakeCase(); | ||
|
||
$this->assertEquals('john_pinkerton', $snakeCase); | ||
} | ||
|
||
public function test_it_can_convert_to_kebab_case() | ||
{ | ||
$string = new Twine\Str('john pinkerton'); | ||
|
||
$kebabCase = $string->kebabCase(); | ||
|
||
$this->assertEquals('john-pinkerton', $kebabCase); | ||
} | ||
} |