Skip to content

Commit 2e594b3

Browse files
committed
Added Str::camelCase(), Str::studlyCase(), Str::pascalCase, Str::snakeCase() and Str::kebabCase() methods
1 parent f844178 commit 2e594b3

File tree

4 files changed

+213
-0
lines changed

4 files changed

+213
-0
lines changed

README.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1047,6 +1047,91 @@ $string->decrypt('secret'); // Returns 'john pinkerton'
10471047

10481048
---
10491049

1050+
### camelCase
1051+
> Convert the string to camelCase.
1052+
1053+
```php
1054+
Twine\Str::camelCase( void ) : Twine\Str
1055+
```
1056+
1057+
#### Example
1058+
1059+
```php
1060+
$string = new Twine\Str('john pinkerton');
1061+
1062+
$string->camelCase(); // Returns 'johnPinkerton'
1063+
```
1064+
1065+
---
1066+
1067+
### studlyCase
1068+
> Convert the string to studlyCase.
1069+
1070+
```php
1071+
Twine\Str::studlyCase( void ) : Twine\Str
1072+
```
1073+
1074+
#### Example
1075+
1076+
```php
1077+
$string = new Twine\Str('john pinkerton');
1078+
1079+
$string->studlyCase(); // Returns 'JohnPinkerton'
1080+
```
1081+
1082+
---
1083+
1084+
### pascalCase
1085+
> Convert the string to pascalCase.
1086+
1087+
```php
1088+
Twine\Str::pascalCase( void ) : Twine\Str
1089+
```
1090+
1091+
#### Example
1092+
1093+
```php
1094+
$string = new Twine\Str('john pinkerton');
1095+
1096+
$string->pascalCase(); // Returns 'JohnPinkerton'
1097+
```
1098+
1099+
---
1100+
1101+
### snakeCase
1102+
> Convert the string to snakeCase.
1103+
1104+
```php
1105+
Twine\Str::snakeCase( void ) : Twine\Str
1106+
```
1107+
1108+
#### Example
1109+
1110+
```php
1111+
$string = new Twine\Str('john pinkerton');
1112+
1113+
$string->snakeCase(); // Returns 'john_pinkerton'
1114+
```
1115+
1116+
---
1117+
1118+
### kebabCase
1119+
> Convert the string to kebabCase.
1120+
1121+
```php
1122+
Twine\Str::kebabCase( void ) : Twine\Str
1123+
```
1124+
1125+
#### Example
1126+
1127+
```php
1128+
$string = new Twine\Str('john pinkerton');
1129+
1130+
$string->kebabCase(); // Returns 'john-pinkerton'
1131+
```
1132+
1133+
---
1134+
10501135
## Chaining Methods
10511136

10521137
A Twine string can be manipulated fluently by chaining methods. Here are a few

src/Str.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PHLAK\Twine\Traits\Aliases;
66
use PHLAK\Twine\Traits\ArrayAccess;
7+
use PHLAK\Twine\Traits\Caseable;
78
use PHLAK\Twine\Traits\Comparable;
89
use PHLAK\Twine\Traits\Convenience;
910
use PHLAK\Twine\Traits\Encodable;
@@ -16,6 +17,7 @@ class Str implements \ArrayAccess, \JsonSerializable, \Serializable
1617
{
1718
use Aliases,
1819
ArrayAccess,
20+
Caseable,
1921
Comparable,
2022
Convenience,
2123
Encodable,

src/Traits/Caseable.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace PHLAK\Twine\Traits;
4+
5+
trait Caseable
6+
{
7+
/**
8+
* Convert the string to camelCase.
9+
*
10+
* @return self
11+
*/
12+
public function camelCase() : self
13+
{
14+
$words = array_map(function ($word) {
15+
return ucfirst(strtolower($word));
16+
}, $this->words());
17+
18+
return new static(lcfirst(implode('', $words)));
19+
}
20+
21+
/**
22+
* Convert the string to StudlyCase.
23+
*
24+
* @return self
25+
*/
26+
public function studlyCase() : self
27+
{
28+
$words = array_map(function ($word) {
29+
return ucfirst(strtolower($word));
30+
}, $this->words());
31+
32+
return new static(implode('', $words));
33+
}
34+
35+
/**
36+
* Convert the string to PascalCase.
37+
*
38+
* @return self
39+
*/
40+
public function pascalCase() : self
41+
{
42+
return $this->studlyCase();
43+
}
44+
45+
/**
46+
* Convert the string to snake_case.
47+
*
48+
* @return self
49+
*/
50+
public function snakeCase() : self
51+
{
52+
$words = array_map(function ($word) {
53+
return strtolower($word);
54+
}, $this->words());
55+
56+
return new static(implode('_', $words));
57+
}
58+
59+
/**
60+
* Convert the string to kebab-case.
61+
*
62+
* @return self
63+
*/
64+
public function kebabCase() : self
65+
{
66+
$words = array_map(function ($word) {
67+
return strtolower($word);
68+
}, $this->words());
69+
70+
return new static(implode('-', $words));
71+
}
72+
}

tests/CaseableTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace PHLAK\Twine\Tests;
4+
5+
use PHLAK\Twine;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class CaseableTest extends TestCase
9+
{
10+
public function test_it_can_convert_to_camel_case()
11+
{
12+
$string = new Twine\Str('john pinkerton');
13+
14+
$camelCase = $string->camelCase();
15+
16+
$this->assertEquals('johnPinkerton', $camelCase);
17+
}
18+
19+
public function test_it_can_convert_to_studly_case()
20+
{
21+
$string = new Twine\Str('john pinkerton');
22+
23+
$studlyCase = $string->studlyCase();
24+
25+
$this->assertEquals('JohnPinkerton', $studlyCase);
26+
}
27+
28+
public function test_it_can_convert_to_pascal_case()
29+
{
30+
$string = new Twine\Str('john pinkerton');
31+
32+
$pascalCase = $string->pascalCase();
33+
34+
$this->assertEquals('JohnPinkerton', $pascalCase);
35+
}
36+
37+
public function test_it_can_convert_to_snake_case()
38+
{
39+
$string = new Twine\Str('john pinkerton');
40+
41+
$snakeCase = $string->snakeCase();
42+
43+
$this->assertEquals('john_pinkerton', $snakeCase);
44+
}
45+
46+
public function test_it_can_convert_to_kebab_case()
47+
{
48+
$string = new Twine\Str('john pinkerton');
49+
50+
$kebabCase = $string->kebabCase();
51+
52+
$this->assertEquals('john-pinkerton', $kebabCase);
53+
}
54+
}

0 commit comments

Comments
 (0)