Skip to content

Commit

Permalink
Merge pull request #2 from enricodias/dev
Browse files Browse the repository at this point in the history
Fixes #1 add minLenght method
  • Loading branch information
enricodias authored Oct 22, 2019
2 parents b9b00fa + 16d9294 commit 06519de
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 24 deletions.
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# The MIT License (MIT)

Copyright (c) 2012 Enrico Dias <[email protected]>
Copyright (c) 2012 Enrico Dias

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
Expand Down
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# nameize
# Nameize

[![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&amp;utm_medium=referral&amp;utm_content=enricodias/nameize&amp;utm_campaign=Badge_Grade)
[![Latest version](http://img.shields.io/packagist/v/enricodias/nameize.svg)](https://packagist.org/packages/enricodias/nameize)
[![Downloads total](http://img.shields.io/packagist/dt/enricodias/nameize.svg)](https://packagist.org/packages/enricodias/nameize)
[![License](http://img.shields.io/packagist/l/enricodias/nameize.svg)](https://github.com/enricodias/nameize/blob/master/LICENSE.md)

A simple class to correctly capitalize full names.

Expand All @@ -26,13 +29,14 @@ include 'vendor/autoload.php';
### Simple usage

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

or

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

echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->name("john o'grady-smith"); // John O'Grady-Smith
```
Expand All @@ -42,20 +46,34 @@ echo $nameize->name("john o'grady-smith"); // John O'Grady-Smith
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;
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
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
$nameize = new \enricodias\nameize("'");
$nameize = new \enricodias\Nameize("'");

echo $nameize->name("Matteo Dell'aqcua"); // Matteo Dell'Aqcua
echo $nameize->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).

```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
```

Your application may detect the user's country and use the appropriate minLength value.

## Additional features

If you need more features I recommend using a name parser such as <https://github.com/theiconic/name-parser>
27 changes: 19 additions & 8 deletions src/nameize.php → src/Nameize.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

namespace enricodias;

class nameize
class Nameize
{

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

public function __construct($allowedCharacters = null) {
Expand All @@ -14,15 +15,14 @@ public function __construct($allowedCharacters = null) {
return $this;

}

public function setAllowedCharacters($characters) {

if (!is_array($characters)) $characters = array($characters);
public function minLength($length) {

$characters[] = ' '; // space is always used
$characters = array_unique($characters);
if (!is_int($length) || $length < 1 || $length > 5) return;

$this->_allowedCharacters = $characters;
$this->_minLength = $length;

return $this;

}

Expand All @@ -43,7 +43,7 @@ public function name($name) {

foreach ($split as $temp) {

if (strlen($temp) > 3) $mend .= self::ucFirst($temp).$char;
if (strlen($temp) >= $this->_minLength) $mend .= self::ucFirst($temp).$char;
else $mend .= $temp.$char;

}
Expand All @@ -63,6 +63,17 @@ public function name($name) {
return $this->_processedName;

}

public function setAllowedCharacters($characters) {

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

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

$this->_allowedCharacters = $characters;

}

private static function ucFirst($string) {

Expand Down
38 changes: 30 additions & 8 deletions tests/nameizeTest.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

use enricodias\nameize;
use enricodias\Nameize;
use PHPUnit\Framework\TestCase;

final class nameizeTest extends TestCase
final class NameizeTest extends TestCase
{

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

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

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

Expand All @@ -22,25 +22,22 @@ public function testCalls()
*/
public function testNames($name, $allowedCharacters, $expected)
{
$nameize = new nameize($allowedCharacters);
$nameize = new Nameize($allowedCharacters);

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

/**
*
* @codeCoverageIgnore
*/
public function nameProvider()
{
return [

// 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"],
Expand All @@ -54,4 +51,29 @@ public function nameProvider()

];
}

/**
* @dataProvider shortNamesProvider
*/
public function testMinLength($name, $expected)
{
$nameize = new Nameize();

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

/**
* @codeCoverageIgnore
*/
public function shortNamesProvider()
{
return [

// issue #1
["Tri vu phu", "Tri Vu Phu"],
["Shuanping dai", "Shuanping Dai"],

];
}

}

0 comments on commit 06519de

Please sign in to comment.