Skip to content

Commit

Permalink
Fix unicode support for camelize
Browse files Browse the repository at this point in the history
  • Loading branch information
Nek- committed Aug 1, 2017
1 parent 0fbca00 commit 9bea6ad
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 19 deletions.
28 changes: 23 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,17 @@ Reference
#### ::camelize

```php
StringTools::camelize($str, $from) : string
StringTools::camelize($str, $from, $encoding) : string
```

* `$str` string input
* `$from` (optional, default "\_") string entry format (can be "-" or "\_")
* `$from` (optional, default "\_") input string format (can be "-" or "\_")
* `$encoding` (optional, default "UTF-8") encoding of your input string

#### ::startsWith

Say if the given string starts with needle or not.

```php
StringTools::startsWith($str, $start) : bool
```
Expand All @@ -41,17 +44,21 @@ StringTools::startsWith($str, $start) : bool

#### ::endsWith

Say if the given string ends with needle or not.

```php
StringTools::endsWith($str, $end, $encoding) : bool
StringTools::endsWith($str, $end) : bool
```

* `$str` string input
* `$end` string it should ends with

#### ::removeStart

Removes the start of the string if it matches with the given one.

```php
StringTools::removeStart($str, $toRemove, $encoding) : string
StringTools::removeStart($str, $toRemove) : string
```

* `$str` string input
Expand All @@ -60,12 +67,23 @@ StringTools::removeStart($str, $toRemove, $encoding) : string
#### ::contains

```php
StringTools::contains($str, $needle, $encoding) : bool
StringTools::contains($str, $needle) : bool
```

* `$str` string input
* `$needle` potentially contained string

#### ::mb_ucfirst

Adds missing multi-byte PHP function for `ucfirst` standard function.

```
StringTools::mb_ucfirst($str, $encoding) : string
```

* `$str` string input
* `$encoding` (optional, default "UTF-8") encoding of your input string

### EqualableInterface

Helps you equals on objects on a similar way as [java](http://stackoverflow.com/questions/1643067/whats-the-difference-between-equals-and).
Expand Down
8 changes: 8 additions & 0 deletions spec/Nekland/Tools/StringToolsSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,12 @@ function it_should_contain_str()
$this::contains('Hello world ! 😀', '! 😀')->shouldReturn(true);
$this::contains('Hello 👽 aliens !', 'aliens')->shouldReturn(true);
}

function it_should_uppercase_first_letter_with_ucfirst()
{
$this::mb_ucfirst('hello')->shouldReturn('Hello');
$this::mb_ucfirst('helloWorlD')->shouldReturn('HelloWorlD');
$this::mb_ucfirst('HelloWorld')->shouldReturn('HelloWorld');
$this::mb_ucfirst('🍕isReallyGood')->shouldReturn('🍕isReallyGood');
}
}
53 changes: 39 additions & 14 deletions src/StringTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,27 @@ class StringTools
/**
* @param string $str
* @param string $from
* @param string $encoding
* @return string
*/
public static function camelize($str, $from = '_')
public static function camelize($str, $from = '_', $encoding = 'UTF-8')
{
if (!in_array($from, ['_', '-'])) {
throw new \InvalidArgumentException('We can camelize only from snake case or kebab case.');
}

return implode('', array_map('ucfirst', array_map('strtolower', explode($from, $str))));
return implode('',
array_map(
// Up the first letter for each sub string
function ($item) use ($encoding) {
return StringTools::mb_ucfirst($item, $encoding);
},
// Lowercase the whole string (otherwise it's not camelize)
array_map(function ($item) use ($encoding) {
return mb_strtolower($item, $encoding);
}, explode($from, $str))
)
);
}

/**
Expand All @@ -33,48 +45,61 @@ public static function startsWith($str, $start)
/**
* @param string $str
* @param string $end
* @param string $encoding
* @return bool
*/
public static function endsWith($str, $end, $encoding = 'UTF-8')
public static function endsWith($str, $end)
{
$length = mb_strlen($end, $encoding);
$length = strlen($end);
if ($length === 0) {
return true;
}

return mb_substr($str, -$length, $length, $encoding) === $end;
return substr($str, -$length, $length) === $end;
}

/**
* @param string $str
* @param string $toRemove
* @param string $encoding
* @return string
*/
public static function removeStart($str, $toRemove, $encoding = 'UTF-8')
public static function removeStart($str, $toRemove)
{
if (!StringTools::startsWith($str, $toRemove, $encoding)) {
if (!StringTools::startsWith($str, $toRemove)) {
return $str;
}
$sizeToRemove = mb_strlen($toRemove, $encoding);
$sizeToRemove = strlen($toRemove);

return mb_substr($str, $sizeToRemove, mb_strlen($str, $encoding) - $sizeToRemove, $encoding);
return substr($str, $sizeToRemove, strlen($str) - $sizeToRemove);
}

/**
* @param string $str The string that should contains the needle
* @param string $needle What should be contained
* @param string $encoding
* @return bool
*/
public static function contains($str, $needle, $encoding = 'UTF-8')
public static function contains($str, $needle)
{
$position = mb_strpos($str, $needle, 0, $encoding);
$position = mb_strpos($str, $needle, 0);
if ($position === 0) {
return true;
}

return (bool) $position;
}

/**
* This function is missing in PHP for now.
*
* @param string $str
* @param string $encoding
* @return string
*/
public static function mb_ucfirst($str, $encoding = 'UTF-8')
{
$length = mb_strlen($str, $encoding);
$firstChar = mb_substr($str, 0, 1, $encoding);
$then = mb_substr($str, 1, $length - 1, $encoding);

return mb_strtoupper($firstChar, $encoding) . $then;
}
}

0 comments on commit 9bea6ad

Please sign in to comment.