From e61e409bfe479bf753b4a3d2e503c259feffb99c Mon Sep 17 00:00:00 2001 From: FrancescoUK Date: Sat, 24 Jun 2017 12:25:40 +0100 Subject: [PATCH] Fix name Title Case issue (#1301) using a composer lib --- application/controllers/Persons.php | 35 +--- composer.json | 3 +- composer.lock | 61 +++++- vendor/composer/autoload_files.php | 10 + vendor/composer/autoload_psr4.php | 1 + vendor/composer/autoload_real.php | 14 ++ vendor/composer/installed.json | 59 ++++++ vendor/tamtamchik/namecase/.editorconfig | 15 ++ vendor/tamtamchik/namecase/.travis.php.ini | 1 + vendor/tamtamchik/namecase/CHANGELOG.md | 21 ++ vendor/tamtamchik/namecase/CONDUCT.md | 22 +++ vendor/tamtamchik/namecase/CONTRIBUTING.md | 32 +++ vendor/tamtamchik/namecase/LICENSE.md | 21 ++ vendor/tamtamchik/namecase/README.md | 113 +++++++++++ vendor/tamtamchik/namecase/composer.json | 50 +++++ vendor/tamtamchik/namecase/examples/demo.php | 22 +++ vendor/tamtamchik/namecase/src/Formatter.php | 198 +++++++++++++++++++ vendor/tamtamchik/namecase/src/functions.php | 19 ++ 18 files changed, 668 insertions(+), 29 deletions(-) create mode 100644 vendor/composer/autoload_files.php create mode 100644 vendor/tamtamchik/namecase/.editorconfig create mode 100644 vendor/tamtamchik/namecase/.travis.php.ini create mode 100644 vendor/tamtamchik/namecase/CHANGELOG.md create mode 100644 vendor/tamtamchik/namecase/CONDUCT.md create mode 100644 vendor/tamtamchik/namecase/CONTRIBUTING.md create mode 100644 vendor/tamtamchik/namecase/LICENSE.md create mode 100644 vendor/tamtamchik/namecase/README.md create mode 100644 vendor/tamtamchik/namecase/composer.json create mode 100644 vendor/tamtamchik/namecase/examples/demo.php create mode 100644 vendor/tamtamchik/namecase/src/Formatter.php create mode 100644 vendor/tamtamchik/namecase/src/functions.php diff --git a/application/controllers/Persons.php b/application/controllers/Persons.php index 07cc9d626..dca63dbf5 100644 --- a/application/controllers/Persons.php +++ b/application/controllers/Persons.php @@ -37,36 +37,19 @@ abstract class Persons extends Secure_Controller } /* - Capitalize segments of a name, and put the rest into lower case. You can pass the characters you want to use as delimiters. + Capitalize segments of a name, and put the rest into lower case. + You can pass the characters you want to use as delimiters as exceptions. + The function supports UTF-8 string. - i.e. + Example: + i.e. - returns John O'Grady-Smith + returns John O'Grady-Smith */ - protected function nameize($str, $a_char = array("'", "-", " ")) - { - // $str contains the complete raw name string - // $a_char is an array containing the characters we use as separators for capitalization. If you don't pass anything, there are three in there as default. - $string = strtolower($str); - foreach($a_char as $temp) - { - $pos = strpos($string, $temp); - if($pos) - { - // we are in the loop because we found one of the special characters in the array, so lets split it up into chunks and capitalize each one. - $mend = ''; - $a_split = explode($temp, $string); - foreach($a_split as $temp2) - { - // capitalize each portion of the string which was separated at a special character - $mend .= ucfirst($temp2).$temp; - } - $string = substr($mend, 0, -1); - } - } - - return ucfirst($string); + protected function nameize($string) + { + return str_name_case($string); } } ?> diff --git a/composer.json b/composer.json index ef5389f93..fb6247639 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,8 @@ "php": "^5.5 || ^7.0", "codeigniter/framework": "^3.1.2", "ins0/google-measurement-php-client": "dev-master#2b3aef2375837339f5b2bfe88e76994481f86264", - "dompdf/dompdf": "^0.7.0" + "dompdf/dompdf": "^0.7.0", + "tamtamchik/namecase": "^1.0" }, "require-dev": { "mikey179/vfsStream": "1.1.*", diff --git a/composer.lock b/composer.lock index 654acbbac..2c31f1c5a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "2cc2838037ae160820629d267c23f4c1", - "content-hash": "7d58e6565d976c6e32780195e6e87ec8", + "hash": "137741ac54e015539c74a0871e3c85fb", + "content-hash": "5f6ba4cd8a7aadbc1220fdce09849270", "packages": [ { "name": "codeigniter/framework", @@ -221,6 +221,63 @@ "description": "A library to read, parse and export to PDF SVG files.", "homepage": "https://github.com/PhenX/php-svg-lib", "time": "2015-05-06 18:49:49" + }, + { + "name": "tamtamchik/namecase", + "version": "1.0.2", + "source": { + "type": "git", + "url": "https://github.com/tamtamchik/namecase.git", + "reference": "b4facc6022420bd1575d0c1d188c2ef870ef176b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tamtamchik/namecase/zipball/b4facc6022420bd1575d0c1d188c2ef870ef176b", + "reference": "b4facc6022420bd1575d0c1d188c2ef870ef176b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "4.*", + "scrutinizer/ocular": "1.*" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "autoload": { + "psr-4": { + "Tamtamchik\\NameCase\\": "src" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yuri Tkachenko", + "email": "yuri.tam.tkachenko@gmail.com", + "homepage": "http://tamtamchika.net" + } + ], + "description": "This package allows you to convert names into the correct case where possible.", + "homepage": "https://github.com/tamtamchik/namecase", + "keywords": [ + "cased", + "namecase", + "properly", + "strings", + "tamtamchik" + ], + "time": "2016-02-06 10:46:26" } ], "packages-dev": [ diff --git a/vendor/composer/autoload_files.php b/vendor/composer/autoload_files.php new file mode 100644 index 000000000..a255969c1 --- /dev/null +++ b/vendor/composer/autoload_files.php @@ -0,0 +1,10 @@ + $vendorDir . '/tamtamchik/namecase/src/functions.php', +); diff --git a/vendor/composer/autoload_psr4.php b/vendor/composer/autoload_psr4.php index 7d52b46ca..3404296f7 100644 --- a/vendor/composer/autoload_psr4.php +++ b/vendor/composer/autoload_psr4.php @@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'Tamtamchik\\NameCase\\' => array($vendorDir . '/tamtamchik/namecase/src'), 'Dotenv\\' => array($vendorDir . '/vlucas/phpdotenv/src'), 'Dompdf\\' => array($vendorDir . '/dompdf/dompdf/src'), ); diff --git a/vendor/composer/autoload_real.php b/vendor/composer/autoload_real.php index 6f220a064..d37d1d75c 100644 --- a/vendor/composer/autoload_real.php +++ b/vendor/composer/autoload_real.php @@ -40,6 +40,20 @@ class ComposerAutoloaderInit4fe73a8b8bda5131fdc94f85e4d193c1 $loader->register(true); + $includeFiles = require __DIR__ . '/autoload_files.php'; + foreach ($includeFiles as $fileIdentifier => $file) { + composerRequire4fe73a8b8bda5131fdc94f85e4d193c1($fileIdentifier, $file); + } + return $loader; } } + +function composerRequire4fe73a8b8bda5131fdc94f85e4d193c1($fileIdentifier, $file) +{ + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { + require $file; + + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; + } +} diff --git a/vendor/composer/installed.json b/vendor/composer/installed.json index 8938591d8..b641adab8 100644 --- a/vendor/composer/installed.json +++ b/vendor/composer/installed.json @@ -307,5 +307,64 @@ "env", "environment" ] + }, + { + "name": "tamtamchik/namecase", + "version": "1.0.2", + "version_normalized": "1.0.2.0", + "source": { + "type": "git", + "url": "https://github.com/tamtamchik/namecase.git", + "reference": "b4facc6022420bd1575d0c1d188c2ef870ef176b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/tamtamchik/namecase/zipball/b4facc6022420bd1575d0c1d188c2ef870ef176b", + "reference": "b4facc6022420bd1575d0c1d188c2ef870ef176b", + "shasum": "" + }, + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "4.*", + "scrutinizer/ocular": "1.*" + }, + "time": "2016-02-06 10:46:26", + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + }, + "installation-source": "dist", + "autoload": { + "psr-4": { + "Tamtamchik\\NameCase\\": "src" + }, + "files": [ + "src/functions.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Yuri Tkachenko", + "email": "yuri.tam.tkachenko@gmail.com", + "homepage": "http://tamtamchika.net" + } + ], + "description": "This package allows you to convert names into the correct case where possible.", + "homepage": "https://github.com/tamtamchik/namecase", + "keywords": [ + "cased", + "namecase", + "properly", + "strings", + "tamtamchik" + ] } ] diff --git a/vendor/tamtamchik/namecase/.editorconfig b/vendor/tamtamchik/namecase/.editorconfig new file mode 100644 index 000000000..cd8eb86ef --- /dev/null +++ b/vendor/tamtamchik/namecase/.editorconfig @@ -0,0 +1,15 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at http://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false diff --git a/vendor/tamtamchik/namecase/.travis.php.ini b/vendor/tamtamchik/namecase/.travis.php.ini new file mode 100644 index 000000000..012d04de6 --- /dev/null +++ b/vendor/tamtamchik/namecase/.travis.php.ini @@ -0,0 +1 @@ +mbstring.internal_encoding = UTF-8; diff --git a/vendor/tamtamchik/namecase/CHANGELOG.md b/vendor/tamtamchik/namecase/CHANGELOG.md new file mode 100644 index 000000000..a772b8b1e --- /dev/null +++ b/vendor/tamtamchik/namecase/CHANGELOG.md @@ -0,0 +1,21 @@ +# Changelog + +All Notable changes to `tamtamchik/namecase` will be documented in this file. + +Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. + +## 1.0.2 - 2016-02-06 + +### Added +- Dutch: `den` (thx, @nexxai) + +## 1.0.1 - 2016-01-30 + +### Added +- static call +- instantiation +- tests + +## 1.0.0 - 2016-01-29 + +Initial release. diff --git a/vendor/tamtamchik/namecase/CONDUCT.md b/vendor/tamtamchik/namecase/CONDUCT.md new file mode 100644 index 000000000..6ff94ca3a --- /dev/null +++ b/vendor/tamtamchik/namecase/CONDUCT.md @@ -0,0 +1,22 @@ +# Contributor Code of Conduct + +As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities. + +We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality. + +Examples of unacceptable behavior by participants include: + +* The use of sexualized language or imagery +* Personal attacks +* Trolling or insulting/derogatory comments +* Public or private harassment +* Publishing other's private information, such as physical or electronic addresses, without explicit permission +* Other unethical or unprofessional conduct. + +Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team. + +This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations. + +Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers. + +This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/) diff --git a/vendor/tamtamchik/namecase/CONTRIBUTING.md b/vendor/tamtamchik/namecase/CONTRIBUTING.md new file mode 100644 index 000000000..670bbaefd --- /dev/null +++ b/vendor/tamtamchik/namecase/CONTRIBUTING.md @@ -0,0 +1,32 @@ +# Contributing + +Contributions are **welcome** and will be fully **credited**. + +We accept contributions via Pull Requests on [Github](https://github.com/tamtamchik/namecase). + + +## Pull Requests + +- **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](http://pear.php.net/package/PHP_CodeSniffer). + +- **Add tests!** - Your patch won't be accepted if it doesn't have tests. + +- **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. + +- **Consider our release cycle** - We try to follow [SemVer v2.0.0](http://semver.org/). Randomly breaking public APIs is not an option. + +- **Create feature branches** - Don't ask us to pull from your master branch. + +- **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. + +- **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](http://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. + + +## Running Tests + +``` bash +$ composer test +``` + + +**Happy coding**! diff --git a/vendor/tamtamchik/namecase/LICENSE.md b/vendor/tamtamchik/namecase/LICENSE.md new file mode 100644 index 000000000..d132664d5 --- /dev/null +++ b/vendor/tamtamchik/namecase/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2015 Yuri Tkachenko + +> Permission is hereby granted, free of charge, to any person obtaining a copy +> of this software and associated documentation files (the "Software"), to deal +> in the Software without restriction, including without limitation the rights +> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +> copies of the Software, and to permit persons to whom the Software is +> furnished to do so, subject to the following conditions: +> +> The above copyright notice and this permission notice shall be included in +> all copies or substantial portions of the Software. +> +> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +> THE SOFTWARE. diff --git a/vendor/tamtamchik/namecase/README.md b/vendor/tamtamchik/namecase/README.md new file mode 100644 index 000000000..63b7626bf --- /dev/null +++ b/vendor/tamtamchik/namecase/README.md @@ -0,0 +1,113 @@ +# NameCase [![SensioLabsInsight](https://insight.sensiolabs.com/projects/660fea1e-d105-4064-9caa-f47e8a282f2a/small.png)](https://insight.sensiolabs.com/projects/660fea1e-d105-4064-9caa-f47e8a282f2a) + +[![Latest Version on Packagist][ico-version]][link-packagist] +[![Software License][ico-license]](LICENSE.md) +[![Build Status][ico-travis]][link-travis] +[![Coverage Status][ico-scrutinizer]][link-scrutinizer] +[![Quality Score][ico-code-quality]][link-code-quality] +[![Total Downloads][ico-downloads]][link-downloads] + +Forenames and surnames are often stored either wholly in UPPERCASE or wholly in lowercase. This package allows you to convert names into the correct case where possible. Although forenames and surnames are normally stored separately if they do appear in a single string, whitespace separated, NameCase deals correctly with them. + +Currently correctly name cases names which include any of the following: + +``` +Mc, Mac, al, el, ap, da, de, delle, della, den, di, du, del, der, la, le, lo, van and von. +``` + +It correctly deals with names which contain apostrophes and hyphens too. + +> **Warning!** This library contains global `str_name_case` function, that potentially can break your function with this name. Now you are warned! + +## Install + +Via Composer + +``` bash +$ composer require tamtamchik/namecase +``` +> **Warning!** For PHP < 5.6, please be sure to setup UTF-8 as defaut encoding in your `php.ini` +`mbstring.internal_encoding = UTF-8;` + +## Usage + +``` php +// As global function +str_name_case("KEITH"); // => Keith +str_name_case("LEIGH-WILLIAMS"); // => Leigh-Williams +str_name_case("MCCARTHY"); // => McCarthy +str_name_case("O'CALLAGHAN"); // => O'Callaghan +str_name_case("ST. JOHN"); // => St. John +str_name_case("VON STREIT"); // => von Streit +str_name_case("AP LLWYD DAFYDD"); // => ap Llwyd Dafydd +str_name_case("HENRY VIII"); // => Henry VIII + +use \Tamtamchik\NameCase\Formatter; + +// As static call +Formatter::nameCase("VAN DYKE"); // => van Dyke + +// As instance +$formatter = new Formatter(); +$formatter->nameCase("LOUIS XIV"); // => Louis XIV +``` + +## Change log + +Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently. + +## Testing & Demo + +``` bash +$ composer tests +$ composer demo +``` + +## Contributing + +Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details. + +## Security + +If you discover any security related issues, please email instead of using the issue tracker. + +## Acknowledgements + +This library is a port of the [Perl library](https://metacpan.org/release/BARBIE/Lingua-EN-NameCase-1.19), and owes most of its functionality to the Perl version by Mark Summerfield. +I also used some solutions from [Ruby version](https://github.com/tenderlove/namecase) by Aaron Patterson. +Any bugs in the PHP port are my fault. + +## Credits + +Original PERL `Lingua::EN::NameCase` Version: + +- Copyright © Mark Summerfield 1998-2014. All Rights Reserved. +- Copyright © Barbie 2014-2015. All Rights Reserved. + +Ruby Version: + +- Copyright © Aaron Patterson 2006. All Rights Reserved. + +PHP Version: + +- [Yuri Tkachenko][link-author] +- [All Contributors][link-contributors] + +## License + +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. + +[ico-version]: https://img.shields.io/packagist/v/tamtamchik/namecase.svg?style=flat-square +[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square +[ico-travis]: https://img.shields.io/travis/tamtamchik/namecase/master.svg?style=flat-square +[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/tamtamchik/namecase.svg?style=flat-square +[ico-code-quality]: https://img.shields.io/scrutinizer/g/tamtamchik/namecase.svg?style=flat-square +[ico-downloads]: https://img.shields.io/packagist/dt/tamtamchik/namecase.svg?style=flat-square + +[link-packagist]: https://packagist.org/packages/tamtamchik/namecase +[link-travis]: https://travis-ci.org/tamtamchik/namecase +[link-scrutinizer]: https://scrutinizer-ci.com/g/tamtamchik/namecase/code-structure +[link-code-quality]: https://scrutinizer-ci.com/g/tamtamchik/namecase +[link-downloads]: https://packagist.org/packages/tamtamchik/namecase +[link-author]: https://github.com/tamtamchik +[link-contributors]: ../../contributors diff --git a/vendor/tamtamchik/namecase/composer.json b/vendor/tamtamchik/namecase/composer.json new file mode 100644 index 000000000..b6314a858 --- /dev/null +++ b/vendor/tamtamchik/namecase/composer.json @@ -0,0 +1,50 @@ +{ + "name": "tamtamchik/namecase", + "type": "library", + "description": "This package allows you to convert names into the correct case where possible.", + "keywords": [ + "tamtamchik", + "namecase", + "strings", + "properly", + "cased" + ], + "homepage": "https://github.com/tamtamchik/namecase", + "license": "MIT", + "authors": [ + { + "name": "Yuri Tkachenko", + "email": "yuri.tam.tkachenko@gmail.com", + "homepage": "http://tamtamchika.net" + } + ], + "require": { + "php": ">=5.4.0" + }, + "require-dev": { + "phpunit/phpunit": "4.*", + "scrutinizer/ocular": "1.*" + }, + "autoload": { + "psr-4": { + "Tamtamchik\\NameCase\\": "src" + }, + "files": [ + "src/functions.php" + ] + }, + "autoload-dev": { + "psr-4": { + "Tamtamchik\\NameCase\\Test\\": "tests" + } + }, + "scripts": { + "tests": "vendor/bin/phpunit", + "demo": "php examples/demo.php" + }, + "extra": { + "branch-alias": { + "dev-master": "1.0-dev" + } + } +} diff --git a/vendor/tamtamchik/namecase/examples/demo.php b/vendor/tamtamchik/namecase/examples/demo.php new file mode 100644 index 000000000..0a362ae1e --- /dev/null +++ b/vendor/tamtamchik/namecase/examples/demo.php @@ -0,0 +1,22 @@ + ' . str_name_case('KEITH') . PHP_EOL; +echo 'LEIGH-WILLIAMS => ' . str_name_case('LEIGH-WILLIAMS') . PHP_EOL; +echo 'MCCARTHY => ' . str_name_case('MCCARTHY') . PHP_EOL; +echo 'O\'CALLAGHAN => ' . str_name_case('O\'CALLAGHAN') . PHP_EOL; +echo 'ST. JOHN => ' . str_name_case('ST. JOHN') . PHP_EOL; +echo 'VON STREIT => ' . str_name_case('VON STREIT') . PHP_EOL; +echo 'AP LLWYD DAFYDD => ' . str_name_case('AP LLWYD DAFYDD') . PHP_EOL; +echo 'HENRY VIII => ' . str_name_case('HENRY VIII') . PHP_EOL; + +// As static call +echo 'VAN DYKE => ' . Formatter::nameCase('VAN DYKE') . PHP_EOL; + +// As instance +$formatter = new Formatter(); +echo 'LOUIS XIV => ' . $formatter->nameCase('LOUIS XIV') . PHP_EOL; diff --git a/vendor/tamtamchik/namecase/src/Formatter.php b/vendor/tamtamchik/namecase/src/Formatter.php new file mode 100644 index 000000000..147750a35 --- /dev/null +++ b/vendor/tamtamchik/namecase/src/Formatter.php @@ -0,0 +1,198 @@ + true, + 'irish' => true, + 'spanish' => true, + ]; + + // Irish exceptions. + private static $exceptions = [ + '\bMacEdo' => 'Macedo', + '\bMacEvicius' => 'Macevicius', + '\bMacHado' => 'Machado', + '\bMacHar' => 'Machar', + '\bMacHin' => 'Machin', + '\bMacHlin' => 'Machlin', + '\bMacIas' => 'Macias', + '\bMacIulis' => 'Maciulis', + '\bMacKie' => 'Mackie', + '\bMacKle' => 'Mackle', + '\bMacKlin' => 'Macklin', + '\bMacKmin' => 'Mackmin', + '\bMacQuarie' => 'Macquarie', + ]; + + // General replacements. + private static $replacements = [ + '\bAl(?=\s+\w)' => 'al', // al Arabic or forename Al. + '\b(Bin|Binti|Binte)\b' => 'bin', // bin, binti, binte Arabic + '\bAp\b' => 'ap', // ap Welsh. + '\bBen(?=\s+\w)' => 'ben', // ben Hebrew or forename Ben. + '\bDell([ae])\b' => 'dell\1', // della and delle Italian. + '\bD([aeiou])\b' => 'd\1', // da, de, di Italian; du French; do Brasil + '\bD([ao]s)\b' => 'd\1', // das, dos Brasileiros + '\bDe([lrn])\b' => 'de\1', // del Italian; der/den Dutch/Flemish. + '\bEl\b' => 'el', // el Greek or El Spanish. + '\bLa\b' => 'la', // la French or La Spanish. + '\bL([eo])\b' => 'l\1', // lo Italian; le French. + '\bVan(?=\s+\w)' => 'van', // van German or forename Van. + '\bVon\b' => 'von', // von Dutch/Flemish + ]; + + // Spanish conjunctions. + private static $conjunctions = ["Y", "E", "I"]; + + // Roman letters regexp. + private static $romanRegex = '\b((?:[Xx]{1,3}|[Xx][Ll]|[Ll][Xx]{0,3})?(?:[Ii]{1,3}|[Ii][VvXx]|[Vv][Ii]{0,3})?)\b'; + + public function __construct(array $options = []) + { + self::$options = array_merge(self::$options, $options); + } + + /** + * Main function for NameCase. + * + * @param string $string + * @param array $options + * + * @return string + */ + public static function nameCase($string = '', array $options = []) + { + self::$options = array_merge(self::$options, $options); + + // Do not do anything if string is mixed and lazy option is true. + if (self::$options['lazy'] && self::skipMixed($string)) return $string; + + // Capitalize + $string = self::capitalize($string); + $string = self::updateIrish($string); + + // Fixes for "son (daughter) of" etc + foreach (self::$replacements as $pattern => $replacement) { + $string = mb_ereg_replace($pattern, $replacement, $string); + } + + $string = self::updateRoman($string); + $string = self::fixConjunction($string); + + return $string; + } + + /** + * Capitalize first letters. + * + * @param string $string + * + * @return string + */ + private static function capitalize($string) + { + $string = mb_strtolower($string); + + $string = mb_ereg_replace_callback('\b\w', function ($matches) { + return mb_strtoupper($matches[0]); + }, $string); + + // Lowercase 's + $string = mb_ereg_replace_callback('\'\w\b', function ($matches) { + return mb_strtolower($matches[0]); + }, $string); + + return $string; + } + + /** + * Skip if string is mixed case. + * + * @param string $string + * + * @return bool + */ + private static function skipMixed($string) + { + $firstLetterLower = $string[0] == mb_strtolower($string[0]); + $allLowerOrUpper = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string); + + return ! ($firstLetterLower || $allLowerOrUpper); + } + + /** + * Update for Irish names. + * + * @param string $string + * + * @return string + */ + private static function updateIrish($string) + { + if ( ! self::$options['irish']) return $string; + + if (mb_ereg_match('.*?\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('.*?\bMc', $string)) { + $string = self::updateMac($string); + } + + return mb_ereg_replace('Macmurdo', 'MacMurdo', $string); + } + + /** + * Fix Spanish conjunctions. + * + * @param string $string + * + * @return string + */ + private static function fixConjunction($string) + { + if ( ! self::$options['spanish']) return $string; + + foreach (self::$conjunctions as $conjunction) { + $string = mb_ereg_replace('\b' . $conjunction . '\b', mb_strtolower($conjunction), $string); + } + + return $string; + } + + /** + * Fix roman numeral names. + * + * @param string $string + * + * @return string + */ + private static function updateRoman($string) + { + return mb_ereg_replace_callback(self::$romanRegex, function ($matches) { + return mb_strtoupper($matches[0]); + }, $string); + } + + /** + * Updates irish Mac & Mc. + * + * @param string $string + * + * @return string + */ + private static function updateMac($string) + { + $string = mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) { + return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1); + }, $string); + + // Now fix "Mac" exceptions + foreach (self::$exceptions as $pattern => $replacement) { + $string = mb_ereg_replace($pattern, $replacement, $string); + } + + return $string; + } +} diff --git a/vendor/tamtamchik/namecase/src/functions.php b/vendor/tamtamchik/namecase/src/functions.php new file mode 100644 index 000000000..4fc1a8287 --- /dev/null +++ b/vendor/tamtamchik/namecase/src/functions.php @@ -0,0 +1,19 @@ +