Skip to content

Commit

Permalink
Added Str::camelCase(), Str::studlyCase(), Str::pascalCase, Str::snak…
Browse files Browse the repository at this point in the history
…eCase() and Str::kebabCase() methods
  • Loading branch information
PHLAK committed Jul 29, 2018
1 parent f844178 commit 2e594b3
Show file tree
Hide file tree
Showing 4 changed files with 213 additions and 0 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,91 @@ $string->decrypt('secret'); // Returns 'john pinkerton'

---

### camelCase
> Convert the string to camelCase.
```php
Twine\Str::camelCase( void ) : Twine\Str
```

#### Example

```php
$string = new Twine\Str('john pinkerton');

$string->camelCase(); // Returns 'johnPinkerton'
```

---

### studlyCase
> Convert the string to studlyCase.
```php
Twine\Str::studlyCase( void ) : Twine\Str
```

#### Example

```php
$string = new Twine\Str('john pinkerton');

$string->studlyCase(); // Returns 'JohnPinkerton'
```

---

### pascalCase
> Convert the string to pascalCase.
```php
Twine\Str::pascalCase( void ) : Twine\Str
```

#### Example

```php
$string = new Twine\Str('john pinkerton');

$string->pascalCase(); // Returns 'JohnPinkerton'
```

---

### snakeCase
> Convert the string to snakeCase.
```php
Twine\Str::snakeCase( void ) : Twine\Str
```

#### Example

```php
$string = new Twine\Str('john pinkerton');

$string->snakeCase(); // Returns 'john_pinkerton'
```

---

### kebabCase
> Convert the string to kebabCase.
```php
Twine\Str::kebabCase( void ) : Twine\Str
```

#### Example

```php
$string = new Twine\Str('john pinkerton');

$string->kebabCase(); // Returns 'john-pinkerton'
```

---

## Chaining Methods

A Twine string can be manipulated fluently by chaining methods. Here are a few
Expand Down
2 changes: 2 additions & 0 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHLAK\Twine\Traits\Aliases;
use PHLAK\Twine\Traits\ArrayAccess;
use PHLAK\Twine\Traits\Caseable;
use PHLAK\Twine\Traits\Comparable;
use PHLAK\Twine\Traits\Convenience;
use PHLAK\Twine\Traits\Encodable;
Expand All @@ -16,6 +17,7 @@ class Str implements \ArrayAccess, \JsonSerializable, \Serializable
{
use Aliases,
ArrayAccess,
Caseable,
Comparable,
Convenience,
Encodable,
Expand Down
72 changes: 72 additions & 0 deletions src/Traits/Caseable.php
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));
}
}
54 changes: 54 additions & 0 deletions tests/CaseableTest.php
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);
}
}

0 comments on commit 2e594b3

Please sign in to comment.