From 66495977d3828ee407c17bb7356a95a424fce5c2 Mon Sep 17 00:00:00 2001 From: enricodias Date: Wed, 6 Nov 2019 17:24:25 -0300 Subject: [PATCH 1/5] update .gitignore --- .gitignore | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index e992c52..285e807 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ -vendor +.vscode tests/_reports -composer.lock +vendor .phpunit.result.cache +composer.lock \ No newline at end of file From 98ef7aded03eb5998e7d9c99ce6e60093cbf1c36 Mon Sep 17 00:00:00 2001 From: enricodias Date: Fri, 8 Nov 2019 18:45:20 -0300 Subject: [PATCH 2/5] update docblocks [skip-ci] --- src/Nameize.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Nameize.php b/src/Nameize.php index 0a48d85..907bf23 100644 --- a/src/Nameize.php +++ b/src/Nameize.php @@ -5,7 +5,7 @@ /** * Nameize * - * A simple class to correctly capitalize full names. + * Correctly capitalize full names. * * @author Enrico Dias * @link https://github.com/enricodias/nameize Github repository. @@ -29,7 +29,7 @@ class Nameize private $_allowedCharacters = array(' ', "'", '-'); /** - * Create a new Nameize instance and set allowed characters. + * Creates 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. @@ -59,7 +59,7 @@ public function minLength($length) } /** - * Correctly capitalize a full name. + * Correctly capitalizes a full name. * * @param string $name Name to be capitalized. * @return string @@ -102,7 +102,7 @@ public function name($name) } /** - * Set one or more allowed characters. + * Sets one or more allowed characters. * * @see Nameize::$_allowedCharacters Definition of allowed characters. * @param string|array $characters A single character or an array of characters. @@ -119,7 +119,7 @@ public function setAllowedCharacters($characters) } /** - * 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 From 98774ecc41a85d9ee11b21dd607e0156f946ca40 Mon Sep 17 00:00:00 2001 From: enricodias Date: Fri, 15 Nov 2019 15:35:11 -0300 Subject: [PATCH 3/5] exclude tests folder in codacy config --- .codacy.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/.codacy.yml b/.codacy.yml index ea1716a..e8edaef 100644 --- a/.codacy.yml +++ b/.codacy.yml @@ -1,6 +1,8 @@ coverage: enabled: true exclude_paths: - - composer.json + - 'composer.json' - '*.xml' - - '*.yml' \ No newline at end of file + - '*.yml' +exclude_paths: + - 'tests/**' \ No newline at end of file From 8e0e62a63efa4f2e9ae8bad7e4e6528f9d68b19b Mon Sep 17 00:00:00 2001 From: enricodias Date: Tue, 26 Nov 2019 11:59:33 -0300 Subject: [PATCH 4/5] add new static create method and rename minLenght --- README.md | 39 +++++++++++++++++++++++------------ src/Nameize.php | 48 ++++++++++++++++++++++++++----------------- tests/nameizeTest.php | 8 ++++---- 3 files changed, 59 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index ee3e1ea..43b4a06 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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. diff --git a/src/Nameize.php b/src/Nameize.php index 907bf23..b7b9835 100644 --- a/src/Nameize.php +++ b/src/Nameize.php @@ -22,40 +22,30 @@ 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(' ', "'", '-'); /** - * Creates 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(); } /** @@ -102,20 +92,40 @@ public function name($name) } /** - * Sets 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; } /** diff --git a/tests/nameizeTest.php b/tests/nameizeTest.php index a49d50c..9206e9b 100644 --- a/tests/nameizeTest.php +++ b/tests/nameizeTest.php @@ -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)); @@ -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)); } @@ -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"], ]; } @@ -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)); } /** From 900e8de099bb300f4d86b95e9d8f81a6bed7a652 Mon Sep 17 00:00:00 2001 From: enricodias Date: Tue, 26 Nov 2019 12:18:59 -0300 Subject: [PATCH 5/5] update CONTRIBUTING.md --- CONTRIBUTING.md | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 130b0bd..df8f05b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -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.