-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Nekland/feature/src-folder
Fixes and new ucfirst feature
- Loading branch information
Showing
7 changed files
with
162 additions
and
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Changelog | ||
All notable changes to this project will be documented in this file. | ||
|
||
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) | ||
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). | ||
|
||
## [Unreleased] | ||
|
||
### Added | ||
- New method `mb_ucfirst`. | ||
|
||
### Changed | ||
- Sources are now located inside `src` folder. | ||
- [Minor BC Break] many parameters `encoding` are suppressed because processing is faster without and they are not | ||
mandatory. _This change doesn't break your code but may in the future if we add new parameters._ | ||
|
||
### Fixed | ||
- Unicode usage for `camelize` method. | ||
|
||
## [1.0.0] - 2016-11-3 | ||
|
||
### Added | ||
|
||
- StringTools class. | ||
- EqualableInterface interface. |
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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
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,105 @@ | ||
<?php | ||
|
||
namespace Nekland\Tools; | ||
|
||
class StringTools | ||
{ | ||
/** | ||
* @param string $str | ||
* @param string $from | ||
* @param string $encoding | ||
* @return string | ||
*/ | ||
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( | ||
// 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)) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param string $start | ||
* @return bool | ||
*/ | ||
public static function startsWith($str, $start) | ||
{ | ||
$length = strlen($start); | ||
|
||
return substr($str, 0, $length) === $start; | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param string $end | ||
* @return bool | ||
*/ | ||
public static function endsWith($str, $end) | ||
{ | ||
$length = strlen($end); | ||
if ($length === 0) { | ||
return true; | ||
} | ||
|
||
return substr($str, -$length, $length) === $end; | ||
} | ||
|
||
/** | ||
* @param string $str | ||
* @param string $toRemove | ||
* @return string | ||
*/ | ||
public static function removeStart($str, $toRemove) | ||
{ | ||
if (!StringTools::startsWith($str, $toRemove)) { | ||
return $str; | ||
} | ||
$sizeToRemove = strlen($toRemove); | ||
|
||
return substr($str, $sizeToRemove, strlen($str) - $sizeToRemove); | ||
} | ||
|
||
/** | ||
* @param string $str The string that should contains the needle | ||
* @param string $needle What should be contained | ||
* @return bool | ||
*/ | ||
public static function contains($str, $needle) | ||
{ | ||
$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; | ||
} | ||
} |