Skip to content

Commit

Permalink
Merge pull request #11 from enricodias/dev
Browse files Browse the repository at this point in the history
Add new static create method and rename minLenght
  • Loading branch information
enricodias authored Nov 27, 2019
2 parents 41eeafb + 4002f3a commit 405df2c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 46 deletions.
6 changes: 4 additions & 2 deletions .codacy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
coverage:
enabled: true
exclude_paths:
- composer.json
- 'composer.json'
- '*.xml'
- '*.yml'
- '*.yml'
exclude_paths:
- 'tests/**'
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
vendor
.vscode
tests/_reports
composer.lock
vendor
.phpunit.result.cache
composer.lock
4 changes: 1 addition & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@

When contributing to this project, please first discuss the change you wish to make in an [Issue](https://github.com/enricodias/Nameize/issues/new).

Ensure that your change was not already proposed by searching old [Issues](https://github.com/enricodias/Nameize/issues).

## Coding conventions

- Use the [PSR-12](https://www.php-fig.org/psr/psr-12/) coding style.
- Follow the PHPMD and PHPCS rules when possible.
- Include DocBlocks in new methods, classes and properties.
- Create the appropriate tests for any new features.
- Create the appropriate tests for any new feature.
- Update the README.md explaining new or modified features.
- Follow the SOLID principles.

Expand Down
39 changes: 26 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ include 'vendor/autoload.php';
### Simple usage

```php
echo (new \enricodias\Nameize())->name("Carlo D'ippoliti"); // Carlo D'Ippoliti
echo \enricodias\Nameize::create()->name("Carlo D'ippoliti"); // Carlo D'Ippoliti
```

or
Expand All @@ -42,33 +42,46 @@ echo $nameize->name("john o'grady-smith"); // John O'Grady-Smith

### Specifying special characters

The constructor has an optional argument that receives an array of special characters. Those characters sinalizes that the next letter should be in upper case. If no character is specified, the default ```array("'", '-')``` is used. If you pass a string, it will be consider a single character.
The method ```setAllowedCharacters()``` receives an array of special characters. Those characters signalizes that the next letter should be in upper case. If no character is specified, the default ```array("'", '-')``` is used. If you pass a string, it will be consider a single character.

```php
use enricodias\Nameize;

echo (new Nameize("'"))->name("john o'grady-smith"); // John O'Grady-smith
echo (new Nameize(["-"]))->name("john o'grady-smith"); // John O'grady-Smith
```

or

```php
$nameize = new \enricodias\Nameize("'");
$nameize = new \enricodias\Nameize();

$nameize->setAllowedCharacters("'");

echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-smith
```

or with method chaining:

```php
echo \enricodias\Nameize::create()
->setAllowedCharacters("-")
->name("john o'grady-smith"); // John O'grady-Smith
```

### Minimum length

Some languages require capitalization on the first letter of every word regardless of their size. The ```minLength``` method sets the minimum length of which words will be capitalized (min: 1, max: 5, default: 4).
Some languages require capitalization on the first letter of every word regardless of their size. The ```setMinLength()``` method sets the minimum length of which words will be capitalized (min: 1, max: 5, default: 4).

```php
$nameize = new \enricodias\Nameize();

echo $nameize->minLength(1)->name("Tri vu phu"); // Tri Vu Phu
echo $nameize->minLength(1)->name("Shuanping dai"); // Shuanping Dai
$nameize->setMinLength(1);

echo $nameize->name("Tri vu phu"); // Tri Vu Phu
echo $nameize->name("Shuanping dai"); // Shuanping Dai
```

or with method chaining:

```php
echo \enricodias\Nameize::create()
->setMinLength(1)
->name("Tri vu phu"); // Tri Vu Phu
```

Your application may detect the user's country and use the appropriate minLength value.
Expand Down
54 changes: 32 additions & 22 deletions src/Nameize.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
* Nameize
*
* A simple class to correctly capitalize full names.
* Correctly capitalize full names.
*
* @author Enrico Dias <enrico@enricodias.com>
* @link https://github.com/enricodias/nameize Github repository.
Expand All @@ -22,44 +22,34 @@ class Nameize
/**
* An array of allowed characters.
*
* Those characters sinalizes that the next letter of a word should be in upper case.
* Those characters signalizes that the next letter of a word should be in upper case.
*
* @var array
*/
private $_allowedCharacters = array(' ', "'", '-');

/**
* Create a new Nameize instance and set allowed characters.
*
* @see Nameize::$_allowedCharacters Definition of allowed characters.
* @param string|array $allowedCharacters A single or an array of characters to be set as allowed characters.
* Creates a new Nameize instance.
*
* @return Nameize
*/
public function __construct($allowedCharacters = null)
public function __construct()
{
if ($allowedCharacters !== null) $this->setAllowedCharacters($allowedCharacters);

return $this;
}

/**
* Sets the minimum word length.
* Creates a new Nameize instance and returns it for chaining.
*
* @see Nameize::$_minLength Definition of minimum word length.
* @param int $length Minimum word length, must be between 1 and 5.
* @return Nameize
*/
public function minLength($length)
public static function create()
{
if (!is_int($length) || $length < 1 || $length > 5) return;

$this->_minLength = $length;

return $this;
return new Nameize();
}

/**
* Correctly capitalize a full name.
* Correctly capitalizes a full name.
*
* @param string $name Name to be capitalized.
* @return string
Expand Down Expand Up @@ -102,24 +92,44 @@ public function name($name)
}

/**
* Set one or more allowed characters.
* Sets one or more allowed characters and returns $this for chaining.
*
* @see Nameize::$_allowedCharacters Definition of allowed characters.
* @param string|array $characters A single character or an array of characters.
* @return void
* @return Nameize
*/
public function setAllowedCharacters($characters)
{
if ($characters === null) return $this;

if (!is_array($characters)) $characters = array($characters);

$characters[] = ' '; // space is always used
$characters = array_unique($characters);

$this->_allowedCharacters = $characters;

return $this;
}

/**
* Sets the minimum word length and returns $this for chaining.
*
* @see Nameize::$_minLength Definition of minimum word length.
* @param int $length Minimum word length, must be between 1 and 5.
* @return Nameize
*/
public function setMinLength($length)
{
if (!is_int($length) || $length < 1 || $length > 5) return;

$this->_minLength = $length;

return $this;
}

/**
* Make a string's first character uppercase using mbstring.
* Makes a string's first character uppercase using mbstring.
*
* @param string $string The input string.
* @return string
Expand Down
8 changes: 4 additions & 4 deletions tests/NameizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function testCalls()
$original = "john o'grady-smith";
$expected = "John O'Grady-Smith";

$this->assertSame($expected, (new Nameize())->name($original));
$this->assertSame($expected, Nameize::create()->name($original));

$nameize = new Nameize();
$this->assertSame($expected, $nameize->name($original));
Expand All @@ -21,7 +21,8 @@ public function testCalls()
*/
public function testNames($name, $allowedCharacters, $expected)
{
$nameize = new Nameize($allowedCharacters);
$nameize = new Nameize();
$nameize->setAllowedCharacters($allowedCharacters);

$this->assertSame($expected, $nameize->name($name));
}
Expand All @@ -46,7 +47,6 @@ public function nameProvider()
["john o'grady-smith5", array("'", "-"), "John O'Grady-Smith5"],
["joão da silva", array(), "João da Silva"],
["maria das dores", null, "Maria das Dores"],
//["mendes d'eça", null, "Mendes d'Eça"],

];
}
Expand All @@ -58,7 +58,7 @@ public function testMinLength($name, $expected)
{
$nameize = new Nameize();

$this->assertSame($expected, $nameize->minLength(1)->name($name));
$this->assertSame($expected, $nameize->setMinLength(1)->name($name));
}

/**
Expand Down

0 comments on commit 405df2c

Please sign in to comment.