Skip to content

Commit

Permalink
Modifying methods for better usage
Browse files Browse the repository at this point in the history
Now it's possible to use the class without any special character
The public static method was removed
  • Loading branch information
enricodias committed Oct 22, 2019
1 parent aa8917e commit 0031cfb
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 62 deletions.
46 changes: 34 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,61 @@
# nameize

A simple class to correctly capitalize full names, specially Brazilian names.
[![Build Status](https://travis-ci.com/enricodias/nameize.svg?branch=master)](https://travis-ci.com/enricodias/nameize)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/ce9cfa2739534021a15aebfb7037ef1d)](https://www.codacy.com/manual/enricodias/nameize?utm_source=github.com&utm_medium=referral&utm_content=enricodias/nameize&utm_campaign=Badge_Coverage)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/ce9cfa2739534021a15aebfb7037ef1d)](https://www.codacy.com/manual/enricodias/nameize?utm_source=github.com&utm_medium=referral&utm_content=enricodias/nameize&utm_campaign=Badge_Grade)

A simple class to correctly capitalize full names.

## Installation

Require this package with Composer in the root directory of your project.
Require this package with Composer in the root directory of your project

```bash
composer require enricodias/nameize
```

and include the composer's autoloader in your code

```php
include 'vendor/autoload.php';
```


## Usage

### Creating an object instance
### Simple usage

```php
$Object = new \enricodias\nameize();
$Object->nameize("joão da silva"); // returns João da Silva
echo (new \enricodias\nameize())->name("Carlo D'ippoliti"); // Carlo D'Ippoliti
```

### Using a static method
or

```php
\enricodias\nameize::format("mArIa dAs DORES"); // returns Maria das Dores
$nameize = new \enricodias\nameize();
echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-Smith
```

### Specifying special characters

The second parameter is optional and receives a single (or list 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.
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.

```php
use enricodias\nameize;

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

or

```php
\enricodias\nameize::format("john o'grady-smith"); // returns John O'Grady-Smith
\enricodias\nameize::format("john o'grady-smith", "'"); // returns John O'Grady-smith
$nameize = new \enricodias\nameize("'");
echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-smith
```

## Other features
## Additional features

If you need more features, I recommend using a name parser such as https://github.com/theiconic/name-parser
If you need more features I recommend using a name parser such as <https://github.com/theiconic/name-parser>
56 changes: 23 additions & 33 deletions src/nameize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

namespace enricodias;

class nameize {
class nameize
{

private $_allowedCharacters = array();
private $_allowedCharacters = array(' ', "'", '-');

public function __construct($allowedCharacters = null) {

$this->_allowedCharacters = self::filterArray($allowedCharacters);

if (!is_null($allowedCharacters)) $this->setAllowedCharacters($allowedCharacters);

return $this;

}

public function nameize($name) {

return self::process($name, $this->_allowedCharacters);

}
public function setAllowedCharacters($characters) {

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

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

$this->_allowedCharacters = $characters;

public static function format($name, $allowedCharacters = null) {

return self::process($name, self::filterArray($allowedCharacters));

}
private static function process($name, $allowedCharacters) {

public function name($name) {

$string = mb_strtolower($name, 'UTF-8');

if (is_array($allowedCharacters) && !empty($allowedCharacters)) {
if (is_array($this->_allowedCharacters) && !empty($this->_allowedCharacters)) {

foreach ($allowedCharacters as $char) {
foreach ($this->_allowedCharacters as $char) {

if (stripos($string, $char) !== false) {

Expand All @@ -55,8 +57,10 @@ private static function process($name, $allowedCharacters) {
}

}

return self::ucFirst($string);

$this->_processedName = self::ucFirst($string);

return $this->_processedName;

}

Expand All @@ -67,19 +71,5 @@ private static function ucFirst($string) {
return $string;

}

private static function filterArray($array) {

if (empty($array)) $array = array("'", '-');
elseif (!is_array($array)) $array = array($array);

$array[] = ' ';
$array = array_unique($array);

return $array;

}

}

?>
53 changes: 36 additions & 17 deletions tests/nameizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,53 @@

final class nameizeTest extends TestCase
{
/**
* @dataProvider nameProvider
*/
public function testNames($name, $allowedCharacters, $expected)

public function testCalls()
{
$instance = new nameize($allowedCharacters);
$original = "john o'grady-smith";
$expected = "John O'Grady-Smith";

$this->assertSame($expected, $instance->nameize($name));
$this->assertSame($expected, (new nameize())->name($original));

$nameize = new nameize();
$this->assertSame($expected, $nameize->name($original));
}

public function testStaticMethod()
/**
* @dataProvider nameProvider
*/
public function testNames($name, $allowedCharacters, $expected)
{
$instance = new nameize();
$nameize = new nameize($allowedCharacters);

$this->assertSame($instance->nameize("john o'grady-smith"), nameize::format("john o'grady-smith"));
$this->assertSame($expected, $nameize->name($name));
}

/**
*
* @codeCoverageIgnore
*/
public function nameProvider()
{
return [
["john o'grady-smith", "", "John O'Grady-Smith"],
["JOHN O'GRADY-SMITH", array(), "John O'Grady-Smith"],
["john o'grady-smith", "'", "John O'Grady-smith"],
["john o'grady-smith", "-", "John O'grady-Smith"],
["john o'grady-smith", array("'", "-"), "John O'Grady-Smith"],
["joão da silva", array(""), "João da Silva"],
["maria das dores", "", "Maria das Dores"],

// issue #1
//["Tri vu phu", null, "Tri Vu Phu"],
["Carlo D'ippoliti", null, "Carlo D'Ippoliti"],
["Kerényi ádám", null, "Kerényi Ádám"],
["Matteo Dell'aqcua", null, "Matteo Dell'Aqcua"],
//["Shuanping dai", null, "Shuanping Dai"],

// default tests
["john o'grady-smith", null, "John O'Grady-Smith"],
["JOHN O'GRADY-SMITH2", null, "John O'Grady-Smith2"],
["john o'grady-smith3", "'", "John O'Grady-smith3"],
["john o'grady-smith4", "-", "John O'grady-Smith4"],
["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"],

];
}
}

0 comments on commit 0031cfb

Please sign in to comment.