|
1
|
|
|
<?php namespace Tamtamchik\NameCase; |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Class Formatter. |
|
5
|
|
|
*/ |
|
6
|
|
|
class Formatter |
|
7
|
|
|
{ |
|
8
|
|
|
private static $options = [ |
|
9
|
|
|
'lazy' => true, |
|
10
|
|
|
'irish' => true, |
|
11
|
|
|
'spanish' => true, |
|
12
|
|
|
]; |
|
13
|
|
|
|
|
14
|
|
|
private static $encoding = 'UTF-8'; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @param string $string |
|
18
|
|
|
* @param array $options |
|
19
|
|
|
* |
|
20
|
|
|
* @return string |
|
21
|
|
|
*/ |
|
22
|
6 |
|
public static function nc($string, array $options = []) |
|
23
|
|
|
{ |
|
24
|
6 |
|
$options = array_merge(self::$options, $options); |
|
25
|
|
|
|
|
26
|
|
|
// Do not do anything if string is mixed and lazy option is true. |
|
27
|
6 |
|
if ($options['lazy']) { |
|
28
|
6 |
|
if (self::skipMixed($string)) return $string; |
|
29
|
6 |
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
$local = mb_strtolower($string, self::$encoding); |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
// Capitalize |
|
34
|
6 |
|
$local = mb_convert_case($string, MB_CASE_TITLE, self::$encoding); |
|
35
|
|
|
|
|
36
|
6 |
|
if ($options['irish']) { |
|
37
|
6 |
|
$local = self::fixIrish($local); |
|
38
|
6 |
|
} |
|
39
|
|
|
|
|
40
|
6 |
|
return $local; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Skip if string is mixed case. |
|
45
|
|
|
* |
|
46
|
|
|
* @param string $string |
|
47
|
|
|
* |
|
48
|
|
|
* @return bool |
|
49
|
|
|
*/ |
|
50
|
6 |
|
private static function skipMixed($string) |
|
51
|
|
|
{ |
|
52
|
6 |
|
$firstLetterLower = $string[0] == mb_strtolower($string[0], self::$encoding); |
|
53
|
6 |
|
$allLowerOrUpper = (mb_strtolower($string, self::$encoding) == $string || mb_strtoupper($string, self::$encoding) == $string); |
|
54
|
|
|
|
|
55
|
6 |
|
return ! ($firstLetterLower || $allLowerOrUpper); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Fix Irish names. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $string |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
6 |
|
private static function fixIrish($string) |
|
66
|
|
|
{ |
|
67
|
6 |
|
if (mb_ereg_match('\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('\bMc', $string)) { |
|
68
|
|
|
|
|
69
|
3 |
|
$string = \mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) { |
|
70
|
3 |
|
return $matches[1] |
|
71
|
3 |
|
. mb_strtoupper(mb_substr($matches[2], 0, 1, self::$encoding), self::$encoding) |
|
72
|
3 |
|
. mb_substr($matches[2], 1, null, self::$encoding); |
|
73
|
3 |
|
}, $string); |
|
74
|
|
|
|
|
75
|
|
|
$exceptions = [ |
|
76
|
3 |
|
'\bMacEdo' => 'Macedo', |
|
77
|
3 |
|
'\bMacEvicius' => 'Macevicius', |
|
78
|
3 |
|
'\bMacHado' => 'Machado', |
|
79
|
3 |
|
'\bMacHar' => 'Machar', |
|
80
|
3 |
|
'\bMacHin' => 'Machin', |
|
81
|
3 |
|
'\bMacHlin' => 'Machlin', |
|
82
|
3 |
|
'\bMacIas' => 'Macias', |
|
83
|
3 |
|
'\bMacIulis' => 'Maciulis', |
|
84
|
3 |
|
'\bMacKie' => 'Mackie', |
|
85
|
3 |
|
'\bMacKle' => 'Mackle', |
|
86
|
3 |
|
'\bMacKlin' => 'Macklin', |
|
87
|
3 |
|
'\bMacKmin' => 'Mackmin', |
|
88
|
3 |
|
'\bMacQuarie' => 'Macquarie', |
|
89
|
3 |
|
]; |
|
90
|
|
|
|
|
91
|
|
|
// Now fix "Mac" exceptions |
|
92
|
3 |
|
foreach ($exceptions as $pattern => $replacement) { |
|
93
|
3 |
|
$string = mb_ereg_replace($pattern, $replacement, $string); |
|
94
|
3 |
|
} |
|
95
|
3 |
|
} |
|
96
|
|
|
|
|
97
|
6 |
|
return mb_ereg_replace('Macmurdo', 'MacMurdo', $string); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.