Skip to content

Commit

Permalink
Fix #7
Browse files Browse the repository at this point in the history
  • Loading branch information
tamtamchik committed Feb 7, 2020
1 parent 625a129 commit 1128964
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 30 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All Notable changes to `tamtamchik/namecase` will be documented in this file.

Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## 2.1.0 – 2020-02-07

### Added

* `excludePostNominals` method to add excluded values (#7).

## 2.0.1 – 2020-02-07

### Added
Expand Down
24 changes: 18 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ It correctly deals with names which contain apostrophes and hyphens too.

Via Composer

``` bash
```bash
$ composer require tamtamchik/namecase
```

## Usage

``` php
```php
use \Tamtamchik\NameCase\Formatter;

// As a static call
Expand Down Expand Up @@ -66,10 +66,10 @@ $formatter = new Formatter(['spanish' => true]);
$formatter->setOptions([
'lazy' = false,
'postnominal' => false
])
]);

// Or even
Formatter::nameCase("VAN DYKE", ['lazy' = false])
Formatter::nameCase("VAN DYKE", ['lazy' = false]);
```

## Options
Expand All @@ -86,7 +86,7 @@ Formatter::nameCase("VAN DYKE", ['lazy' = false])
This package used to have global function `str_name_case`. Now it does not have it anymore.
But, if you still need to have it, use this snippet.

``` php
```php
<?php

use Tamtamchik\NameCase\Formatter;
Expand All @@ -109,13 +109,25 @@ if ( ! function_exists('str_name_case')) {

```

## Exclude Post-Nominals

```php
<?php

use Tamtamchik\NameCase\Formatter;

Formatter::excludePostNominals('MOst');

Formatter::nameCase('ČERNÝ MOST');
```

## Changelog

Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.

## Testing & Demo

``` bash
```bash
$ composer tests
$ composer demo
```
Expand Down
66 changes: 42 additions & 24 deletions src/Formatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Formatter
'\bLa\b' => 'la', // la French or La Spanish.
];

const HEBREW = [
private const HEBREW = [
'\bBen(?=\s+\w)' => 'ben', // ben Hebrew or forename Ben.
'\bBat(?=\s+\w)' => 'bat', // bat Hebrew or forename Bat.
];
Expand All @@ -71,30 +71,33 @@ class Formatter

// Post nominal values.
private const POST_NOMINALS = [
'ACILEx', 'ACSM', 'ADC', 'AEPC', 'AFC', 'AFM', 'AICSM', 'AKC', 'AM', 'ARBRIBA', 'ARCS', 'ARRC', 'ARSM', 'AUH', 'AUS',
'BA', 'BArch', 'BCh', 'BChir', 'BCL', 'BDS', 'BEd', 'BEM', 'BEng', 'BM', 'BS', 'BSc', 'BSW', 'BVM&S', 'BVScBVetMed',
'CB', 'CBE', 'CEng', 'CertHE', 'CGC', 'CGM', 'CH', 'CIE', 'CMarEngCMarSci', 'CMarTech', 'CMG', 'CMILT', 'CML', 'CPhT', 'CPLCTP', 'CPM', 'CQSW', 'CSciTeach', 'CSI', 'CTL', 'CVO',
'DBE', 'DBEnv', 'DC', 'DCB', 'DCM', 'DCMG', 'DConstMgt', 'DCVO', 'DD', 'DEM', 'DFC', 'DFM', 'DIC', 'Dip', 'DipHE', 'DipLP', 'DipSW', 'DL', 'DLitt', 'DLP', 'DPhil', 'DProf', 'DPT', 'DREst', 'DSC', 'DSM', 'DSO', 'DSocSci',
'ED', 'EdD', 'EJLog', 'EMLog', 'EN', 'EngD', 'EngTech', 'ERD', 'ESLog',
'FADO', 'FAWM', 'FBDOFCOptom', 'FCEM', 'FCILEx', 'FCILT', 'FCSP.', 'FdAFdSc', 'FdEng', 'FFHOM', 'FFPM', 'FRCAFFPMRCA', 'FRCGP', 'FRCOG', 'FRCP', 'FRCPsych', 'FRCS', 'FRCVS', 'FSCR.',
'GBE', 'GC', 'GCB', 'GCIE', 'GCILEx', 'GCMG', 'GCSI', 'GCVO', 'GM',
'HNC', 'HNCert', 'HND', 'HNDip',
'ICTTech', 'IDSM', 'IEng', 'IMarEng', 'IOMCPM', 'ISO',
'J', 'JP', 'JrLog',
'KBE', 'KC', 'KCB', 'KCIE', 'KCMG', 'KCSI', 'KCVO', 'KG', 'KP', 'KT',
'LFHOM', 'LG', 'LJ', 'LLB', 'LLD', 'LLM', 'Log', 'LPE', 'LT', 'LVO',
'MA', 'MAcc', 'MAnth', 'MArch', 'MarEngTech', 'MB', 'MBA', 'MBChB', 'MBE', 'MBEIOM', 'MBiochem', 'MC', 'MCEM', 'MCGI', 'MCh.', 'MChem', 'MChiro', 'MClinRes', 'MComp', 'MCOptom', 'MCSM', 'MCSP', 'MD', 'MEarthSc', 'MEng', 'MEnt', 'MEP', 'MFHOM', 'MFin', 'MFPM', 'MGeol', 'MILT', 'MJur', 'MLA', 'MLitt', 'MM', 'MMath', 'MMathStat', 'MMORSE', 'MMus', 'MOst', 'MP', 'MPAMEd', 'MPharm', 'MPhil', 'MPhys', 'MRCGP', 'MRCOG', 'MRCP', 'MRCPath', 'MRCPCHFRCPCH', 'MRCPsych', 'MRCS', 'MRCVS', 'MRes', 'MS', 'MSc', 'MScChiro', 'MSci', 'MSCR', 'MSM', 'MSocSc', 'MSP', 'MSt', 'MSW', 'MSYP', 'MVO',
'NPQH',
'OBE', 'OBI', 'OM', 'OND',
'PgC', 'PGCAP', 'PGCE', 'PgCert', 'PGCHE', 'PgCLTHE', 'PgD', 'PGDE', 'PgDip', 'PhD', 'PLog', 'PLS',
'QAM', 'QC', 'QFSM', 'QGM', 'QHC', 'QHDS', 'QHNS', 'QHP', 'QHS', 'QPM', 'QS', 'QTSCSci',
'RD', 'RFHN', 'RGN', 'RHV', 'RIAI', 'RIAS', 'RM', 'RMN', 'RN', 'RN1RNA', 'RN2', 'RN3', 'RN4', 'RN5', 'RN6', 'RN7', 'RN8', 'RN9', 'RNC', 'RNLD', 'RNMH', 'ROH', 'RRC', 'RSAW', 'RSci', 'RSciTech', 'RSCN', 'RSN', 'RVM', 'RVN',
'SCHM', 'SCJ', 'SCLD', 'SEN', 'SGM', 'SL', 'SPANSPMH', 'SPCC', 'SPCN', 'SPDN', 'SPHP', 'SPLD', 'SrLog', 'SRN', 'SROT',
'TD',
'UD',
'ACILEx', 'ACSM', 'ADC', 'AEPC', 'AFC', 'AFM', 'AICSM', 'AKC', 'AM', 'ARBRIBA', 'ARCS', 'ARRC', 'ARSM', 'AUH', 'AUS',
'BA', 'BArch', 'BCh', 'BChir', 'BCL', 'BDS', 'BEd', 'BEM', 'BEng', 'BM', 'BS', 'BSc', 'BSW', 'BVM&S', 'BVScBVetMed',
'CB', 'CBE', 'CEng', 'CertHE', 'CGC', 'CGM', 'CH', 'CIE', 'CMarEngCMarSci', 'CMarTech', 'CMG', 'CMILT', 'CML', 'CPhT', 'CPLCTP', 'CPM', 'CQSW', 'CSciTeach', 'CSI', 'CTL', 'CVO',
'DBE', 'DBEnv', 'DC', 'DCB', 'DCM', 'DCMG', 'DConstMgt', 'DCVO', 'DD', 'DEM', 'DFC', 'DFM', 'DIC', 'Dip', 'DipHE', 'DipLP', 'DipSW', 'DL', 'DLitt', 'DLP', 'DPhil', 'DProf', 'DPT', 'DREst', 'DSC', 'DSM', 'DSO', 'DSocSci',
'ED', 'EdD', 'EJLog', 'EMLog', 'EN', 'EngD', 'EngTech', 'ERD', 'ESLog',
'FADO', 'FAWM', 'FBDOFCOptom', 'FCEM', 'FCILEx', 'FCILT', 'FCSP.', 'FdAFdSc', 'FdEng', 'FFHOM', 'FFPM', 'FRCAFFPMRCA', 'FRCGP', 'FRCOG', 'FRCP', 'FRCPsych', 'FRCS', 'FRCVS', 'FSCR.',
'GBE', 'GC', 'GCB', 'GCIE', 'GCILEx', 'GCMG', 'GCSI', 'GCVO', 'GM',
'HNC', 'HNCert', 'HND', 'HNDip',
'ICTTech', 'IDSM', 'IEng', 'IMarEng', 'IOMCPM', 'ISO',
'J', 'JP', 'JrLog',
'KBE', 'KC', 'KCB', 'KCIE', 'KCMG', 'KCSI', 'KCVO', 'KG', 'KP', 'KT',
'LFHOM', 'LG', 'LJ', 'LLB', 'LLD', 'LLM', 'Log', 'LPE', 'LT', 'LVO',
'MA', 'MAcc', 'MAnth', 'MArch', 'MarEngTech', 'MB', 'MBA', 'MBChB', 'MBE', 'MBEIOM', 'MBiochem', 'MC', 'MCEM', 'MCGI', 'MCh.', 'MChem', 'MChiro', 'MClinRes', 'MComp', 'MCOptom', 'MCSM', 'MCSP', 'MD', 'MEarthSc', 'MEng', 'MEnt', 'MEP', 'MFHOM', 'MFin', 'MFPM', 'MGeol', 'MILT', 'MJur', 'MLA', 'MLitt', 'MM', 'MMath', 'MMathStat', 'MMORSE', 'MMus', 'MOst', 'MP', 'MPAMEd', 'MPharm', 'MPhil', 'MPhys', 'MRCGP', 'MRCOG', 'MRCP', 'MRCPath', 'MRCPCHFRCPCH', 'MRCPsych', 'MRCS', 'MRCVS', 'MRes', 'MS', 'MSc', 'MScChiro', 'MSci', 'MSCR', 'MSM', 'MSocSc', 'MSP', 'MSt', 'MSW', 'MSYP', 'MVO',
'NPQH',
'OBE', 'OBI', 'OM', 'OND',
'PgC', 'PGCAP', 'PGCE', 'PgCert', 'PGCHE', 'PgCLTHE', 'PgD', 'PGDE', 'PgDip', 'PhD', 'PLog', 'PLS',
'QAM', 'QC', 'QFSM', 'QGM', 'QHC', 'QHDS', 'QHNS', 'QHP', 'QHS', 'QPM', 'QS', 'QTSCSci',
'RD', 'RFHN', 'RGN', 'RHV', 'RIAI', 'RIAS', 'RM', 'RMN', 'RN', 'RN1RNA', 'RN2', 'RN3', 'RN4', 'RN5', 'RN6', 'RN7', 'RN8', 'RN9', 'RNC', 'RNLD', 'RNMH', 'ROH', 'RRC', 'RSAW', 'RSci', 'RSciTech', 'RSCN', 'RSN', 'RVM', 'RVN',
'SCHM', 'SCJ', 'SCLD', 'SEN', 'SGM', 'SL', 'SPANSPMH', 'SPCC', 'SPCN', 'SPDN', 'SPHP', 'SPLD', 'SrLog', 'SRN', 'SROT',
'TD',
'UD',
'V100', 'V200', 'V300', 'VC', 'VD', 'VetMB', 'VN', 'VRD'
];

// Excluded post-nominals
private static $postNominalsExcluded = [];

// Default options.
private static $options = [
'lazy' => true,
Expand Down Expand Up @@ -125,6 +128,20 @@ public static function setOptions($options)
self::$options = array_merge(self::$options, $options);
}

/**
* Global post-nominals exclusions setter.
*
* @param array|string $values
* @return boolean
*/
public static function excludePostNominals($values)
{
if (is_string($values)) $values = [$values];
if (!is_array($values)) return false;

self::$postNominalsExcluded = array_merge(self::$postNominalsExcluded, $values);
}

/**
* Main function for NameCase.
*
Expand Down Expand Up @@ -308,8 +325,9 @@ private static function fixConjunction(string $name): string
*/
private static function fixPostNominal(string $name): string
{
foreach (self::POST_NOMINALS as $postnominal) {
$name = mb_ereg_replace('\b' . $postnominal . '\b', $postnominal, $name, 'ix');
$postNominals = array_diff(self::POST_NOMINALS, self::$postNominalsExcluded);
foreach ($postNominals as $postNominal) {
$name = mb_ereg_replace('\b' . $postNominal . '\b', $postNominal, $name, 'ix');
}
return $name;
}
Expand Down
24 changes: 24 additions & 0 deletions tests/PostNominalsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php namespace Tamtamchik\NameCase\Test;

use PHPUnit\Framework\TestCase;
use Tamtamchik\NameCase\Formatter;

class PostNominalsTest extends TestCase
{
/** Test `MBE` post-nominal */
public function testMbe() {
$this->assertEquals('Adisa Azapagic MBE Freng Frsc Ficheme', Formatter::nameCase('ADISA AZAPAGIC MBE FRENG FRSC FICHEME'));
}

/** Test post-nominals exclusion. */
public function testExcludeString() {
Formatter::excludePostNominals('MOst');
$this->assertEquals('Černý Most', Formatter::nameCase('ČERNÝ MOST'));
}

/** Test post-nominals exclusion. */
public function testExcludeArray() {
Formatter::excludePostNominals(['MOst']);
$this->assertEquals('Černý Most', Formatter::nameCase('ČERNÝ MOST'));
}
}

0 comments on commit 1128964

Please sign in to comment.