1 | <?php namespace Tamtamchik\NameCase; |
||
6 | class Formatter |
||
7 | { |
||
8 | // Irish exceptions. |
||
9 | private const EXCEPTIONS = [ |
||
10 | '\bMacEdo' => 'Macedo', |
||
11 | '\bMacEvicius' => 'Macevicius', |
||
12 | '\bMacHado' => 'Machado', |
||
13 | '\bMacHar' => 'Machar', |
||
14 | '\bMacHin' => 'Machin', |
||
15 | '\bMacHlin' => 'Machlin', |
||
16 | '\bMacIas' => 'Macias', |
||
17 | '\bMacIulis' => 'Maciulis', |
||
18 | '\bMacKie' => 'Mackie', |
||
19 | '\bMacKle' => 'Mackle', |
||
20 | '\bMacKlin' => 'Macklin', |
||
21 | '\bMacKmin' => 'Mackmin', |
||
22 | '\bMacQuarie' => 'Macquarie', |
||
23 | '\bMacOmber' => 'Macomber', |
||
24 | '\bMacIn' => 'Macin', |
||
25 | '\bMacKintosh' => 'Mackintosh', |
||
26 | '\bMacKen' => 'Macken', |
||
27 | '\bMacHen' => 'Machen', |
||
28 | '\bMacisaac' => 'MacIsaac', |
||
29 | '\bMacHiel' => 'Machiel', |
||
30 | '\bMacIol' => 'Maciol', |
||
31 | '\bMacKell' => 'Mackell', |
||
32 | '\bMacKlem' => 'Macklem', |
||
33 | '\bMacKrell' => 'Mackrell', |
||
34 | '\bMacLin' => 'Maclin', |
||
35 | '\bMacKey' => 'Mackey', |
||
36 | '\bMacKley' => 'Mackley', |
||
37 | '\bMacHell' => 'Machell', |
||
38 | '\bMacHon' => 'Machon', |
||
39 | ]; |
||
40 | |||
41 | // General replacements. |
||
42 | private const REPLACEMENTS = [ |
||
43 | '\bAl(?=\s+\w)' => 'al', // al Arabic or forename Al. |
||
44 | '\bAp\b' => 'ap', // ap Welsh. |
||
45 | '\b(Bin|Binti|Binte)\b' => 'bin', // bin, binti, binte Arabic. |
||
46 | '\bDell([ae])\b' => 'dell\1', // della and delle Italian. |
||
47 | '\bD([aeiou])\b' => 'd\1', // da, de, di Italian; du French; do Brasil. |
||
48 | '\bD([ao]s)\b' => 'd\1', // das, dos Brasileiros. |
||
49 | '\bDe([lrn])\b' => 'de\1', // del Italian; der/den Dutch/Flemish. |
||
50 | '\bL([eo])\b' => 'l\1', // lo Italian; le French. |
||
51 | '\bTe([rn])\b' => 'te\1', // ten, ter Dutch/Flemish. |
||
52 | '\bVan(?=\s+\w)' => 'van', // van German or forename Van. |
||
53 | '\bVon\b' => 'von', // von Dutch/Flemish. |
||
54 | ]; |
||
55 | |||
56 | private const SPANISH = [ |
||
57 | '\bEl\b' => 'el', // el Greek or El Spanish. |
||
58 | '\bLa\b' => 'la', // la French or La Spanish. |
||
59 | ]; |
||
60 | |||
61 | private const HEBREW = [ |
||
62 | '\bBen(?=\s+\w)' => 'ben', // ben Hebrew or forename Ben. |
||
63 | '\bBat(?=\s+\w)' => 'bat', // bat Hebrew or forename Bat. |
||
64 | ]; |
||
65 | |||
66 | // Spanish conjunctions. |
||
67 | private const CONJUNCTIONS = ['Y', 'E', 'I']; |
||
68 | |||
69 | // Roman letters regexp. |
||
70 | private const ROMAN_REGEX = '\b((?:[Xx]{1,3}|[Xx][Ll]|[Ll][Xx]{0,3})?(?:[Ii]{1,3}|[Ii][VvXx]|[Vv][Ii]{0,3})?)\b'; |
||
71 | |||
72 | // Post nominal values. |
||
73 | private const POST_NOMINALS = [ |
||
74 | 'ACILEx', 'ACSM', 'ADC', 'AEPC', 'AFC', 'AFM', 'AICSM', 'AKC', 'AM', 'ARBRIBA', 'ARCS', 'ARRC', 'ARSM', 'AUH', 'AUS', |
||
75 | 'BA', 'BArch', 'BCh', 'BChir', 'BCL', 'BDS', 'BEd', 'BEM', 'BEng', 'BM', 'BS', 'BSc', 'BSW', 'BVM&S', 'BVScBVetMed', |
||
76 | 'CB', 'CBE', 'CEng', 'CertHE', 'CGC', 'CGM', 'CH', 'CIE', 'CMarEngCMarSci', 'CMarTech', 'CMG', 'CMILT', 'CML', 'CPhT', 'CPLCTP', 'CPM', 'CQSW', 'CSciTeach', 'CSI', 'CTL', 'CVO', |
||
77 | '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', |
||
78 | 'ED', 'EdD', 'EJLog', 'EMLog', 'EN', 'EngD', 'EngTech', 'ERD', 'ESLog', |
||
79 | 'FADO', 'FAWM', 'FBDOFCOptom', 'FCEM', 'FCILEx', 'FCILT', 'FCSP.', 'FdAFdSc', 'FdEng', 'FFHOM', 'FFPM', 'FRCAFFPMRCA', 'FRCGP', 'FRCOG', 'FRCP', 'FRCPsych', 'FRCS', 'FRCVS', 'FSCR.', |
||
80 | 'GBE', 'GC', 'GCB', 'GCIE', 'GCILEx', 'GCMG', 'GCSI', 'GCVO', 'GM', |
||
81 | 'HNC', 'HNCert', 'HND', 'HNDip', |
||
82 | 'ICTTech', 'IDSM', 'IEng', 'IMarEng', 'IOMCPM', 'ISO', |
||
83 | 'J', 'JP', 'JrLog', |
||
84 | 'KBE', 'KC', 'KCB', 'KCIE', 'KCMG', 'KCSI', 'KCVO', 'KG', 'KP', 'KT', |
||
85 | 'LFHOM', 'LG', 'LJ', 'LLB', 'LLD', 'LLM', 'Log', 'LPE', 'LT', 'LVO', |
||
86 | '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', |
||
87 | 'NPQH', |
||
88 | 'OBE', 'OBI', 'OM', 'OND', |
||
89 | 'PgC', 'PGCAP', 'PGCE', 'PgCert', 'PGCHE', 'PgCLTHE', 'PgD', 'PGDE', 'PgDip', 'PhD', 'PLog', 'PLS', |
||
90 | 'QAM', 'QC', 'QFSM', 'QGM', 'QHC', 'QHDS', 'QHNS', 'QHP', 'QHS', 'QPM', 'QS', 'QTSCSci', |
||
91 | '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', |
||
92 | 'SCHM', 'SCJ', 'SCLD', 'SEN', 'SGM', 'SL', 'SPANSPMH', 'SPCC', 'SPCN', 'SPDN', 'SPHP', 'SPLD', 'SrLog', 'SRN', 'SROT', |
||
93 | 'TD', |
||
94 | 'UD', |
||
95 | 'V100', 'V200', 'V300', 'VC', 'VD', 'VetMB', 'VN', 'VRD' |
||
96 | ]; |
||
97 | |||
98 | // Excluded post-nominals |
||
99 | private static $postNominalsExcluded = []; |
||
100 | |||
101 | // Lowercase words |
||
102 | private const LOWER_CASE_WORDS = ['The', 'Of', 'And']; |
||
103 | |||
104 | // Default options. |
||
105 | private static $options = [ |
||
106 | 'lazy' => true, |
||
107 | 'irish' => true, |
||
108 | 'spanish' => false, |
||
109 | 'roman' => true, |
||
110 | 'hebrew' => true, |
||
111 | 'postnominal' => true, |
||
112 | ]; |
||
113 | 4 | ||
114 | /** |
||
115 | 4 | * Formatter constructor. |
|
116 | 4 | * |
|
117 | * @param array $options |
||
118 | */ |
||
119 | public function __construct($options = []) |
||
123 | 18 | ||
124 | /** |
||
125 | 18 | * Global options setter. |
|
126 | 18 | * |
|
127 | * @param array $options |
||
128 | */ |
||
129 | public static function setOptions($options): void |
||
133 | |||
134 | /** |
||
135 | * Global post-nominals exclusions setter. |
||
136 | 20 | * |
|
137 | * @param array|string $values |
||
138 | 20 | * @return boolean|void |
|
139 | */ |
||
140 | 18 | public static function excludePostNominals($values) |
|
147 | |||
148 | 16 | /** |
|
149 | 16 | * Main function for NameCase. |
|
150 | * |
||
151 | * @param string $name |
||
152 | 16 | * @param array $options |
|
153 | * |
||
154 | * @return string |
||
155 | 16 | */ |
|
156 | public static function nameCase($name = '', array $options = []): string |
||
176 | |||
177 | /** |
||
178 | * Process options with given name |
||
179 | 16 | * |
|
180 | * @param string $name |
||
181 | 16 | * |
|
182 | * @return string |
||
183 | */ |
||
184 | 16 | private static function processOptions(string $name): string |
|
200 | |||
201 | /** |
||
202 | 16 | * Capitalize first letters. |
|
203 | * |
||
204 | * @param string $name |
||
205 | 16 | * |
|
206 | 16 | * @return string |
|
207 | 16 | */ |
|
208 | private static function capitalize(string $name): string |
||
225 | |||
226 | 18 | /** |
|
227 | 18 | * Define required replacements. |
|
228 | * |
||
229 | 18 | * @return array |
|
230 | */ |
||
231 | private static function getReplacements(): array |
||
245 | 16 | ||
246 | /** |
||
247 | 6 | * Skip if string is mixed case. |
|
248 | * |
||
249 | * @param string $name |
||
250 | 16 | * |
|
251 | * @return bool |
||
252 | */ |
||
253 | private static function skipMixed(string $name): bool |
||
260 | 16 | ||
261 | /** |
||
262 | * Update for Irish names. |
||
263 | 16 | * |
|
264 | 16 | * @param string $name |
|
265 | * |
||
266 | * @return string |
||
267 | */ |
||
268 | private static function updateIrish(string $name): string |
||
281 | 6 | ||
282 | 6 | /** |
|
283 | * Fix roman numeral names. |
||
284 | * |
||
285 | 6 | * @param string $name |
|
286 | * |
||
287 | * @return string |
||
288 | */ |
||
289 | private static function updateRoman(string $name): string |
||
295 | 2 | ||
296 | /** |
||
297 | 2 | * Updates irish Mac & Mc. |
|
298 | 2 | * |
|
299 | * @param string $name |
||
300 | 2 | * |
|
301 | * @return string |
||
302 | */ |
||
303 | private static function updateMac(string $name): string |
||
316 | |||
317 | /** |
||
318 | * Fix Spanish conjunctions. |
||
319 | * |
||
320 | * @param string $name |
||
321 | * |
||
322 | * @return string |
||
323 | */ |
||
324 | private static function fixConjunction(string $name): string |
||
331 | |||
332 | /** |
||
333 | * Correct lower-case words of titles. |
||
334 | * |
||
335 | * @param string $name |
||
336 | * |
||
337 | * @return string |
||
338 | */ |
||
339 | private static function correctLowerCaseWords(string $name): string |
||
346 | |||
347 | /** |
||
348 | * Fix post-nominal letter cases. |
||
349 | * |
||
350 | * @param string $name |
||
351 | * @return string |
||
352 | */ |
||
353 | private static function fixPostNominal(string $name): string |
||
361 | } |
||
362 |