Skip to content

Commit

Permalink
Merge pull request #5 from Nekland/feature/src-folder
Browse files Browse the repository at this point in the history
Fixes and new ucfirst feature
  • Loading branch information
Awkan authored Aug 1, 2017
2 parents 4ad260e + 95a8333 commit e3bfcbd
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 86 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
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.
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
80 changes: 0 additions & 80 deletions StringTools.php

This file was deleted.

2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"licence": "MIT",
"description": "Just some tools to work better with PHP",
"autoload": {
"psr-4": { "Nekland\\Tools\\": "" }
"psr-4": { "Nekland\\Tools\\": "src/" }
},
"require-dev": {
"phpspec/phpspec": "^2.5"
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');
}
}
File renamed without changes.
105 changes: 105 additions & 0 deletions src/StringTools.php
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;
}
}

0 comments on commit e3bfcbd

Please sign in to comment.