|
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 = self::capitalize($local); |
|
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
|
|
|
* Capitalize first letters. |
|
60
|
|
|
* |
|
61
|
|
|
* @param string $string |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
6 |
|
private static function capitalize($string) |
|
66
|
|
|
{ |
|
67
|
|
|
$string = \mb_ereg_replace_callback('\b\w', function ($matches) { |
|
68
|
6 |
|
return mb_strtoupper($matches[0], self::$encoding); |
|
69
|
6 |
|
}, $string); |
|
70
|
|
|
|
|
71
|
|
|
// Lowercase 's |
|
72
|
|
|
$string = \mb_ereg_replace_callback('\'\w\b', function ($matches) { |
|
73
|
3 |
|
return mb_strtolower($matches[0], self::$encoding); |
|
74
|
6 |
|
}, $string); |
|
75
|
|
|
|
|
76
|
6 |
|
return $string; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Fix Irish names. |
|
81
|
|
|
* |
|
82
|
|
|
* @param string $string |
|
83
|
|
|
* |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
6 |
|
private static function fixIrish($string) |
|
87
|
|
|
{ |
|
88
|
6 |
|
if (mb_ereg_match('\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('\bMc', $string)) { |
|
89
|
|
|
|
|
90
|
3 |
|
$string = \mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) { |
|
91
|
3 |
|
return $matches[1] |
|
92
|
3 |
|
. mb_strtoupper(mb_substr($matches[2], 0, 1, self::$encoding), self::$encoding) |
|
93
|
3 |
|
. mb_substr($matches[2], 1, null, self::$encoding); |
|
94
|
3 |
|
}, $string); |
|
95
|
|
|
|
|
96
|
|
|
$exceptions = [ |
|
97
|
3 |
|
'\bMacEdo' => 'Macedo', |
|
98
|
3 |
|
'\bMacEvicius' => 'Macevicius', |
|
99
|
3 |
|
'\bMacHado' => 'Machado', |
|
100
|
3 |
|
'\bMacHar' => 'Machar', |
|
101
|
3 |
|
'\bMacHin' => 'Machin', |
|
102
|
3 |
|
'\bMacHlin' => 'Machlin', |
|
103
|
3 |
|
'\bMacIas' => 'Macias', |
|
104
|
3 |
|
'\bMacIulis' => 'Maciulis', |
|
105
|
3 |
|
'\bMacKie' => 'Mackie', |
|
106
|
3 |
|
'\bMacKle' => 'Mackle', |
|
107
|
3 |
|
'\bMacKlin' => 'Macklin', |
|
108
|
3 |
|
'\bMacKmin' => 'Mackmin', |
|
109
|
3 |
|
'\bMacQuarie' => 'Macquarie', |
|
110
|
3 |
|
]; |
|
111
|
|
|
|
|
112
|
|
|
// Now fix "Mac" exceptions |
|
113
|
3 |
|
foreach ($exceptions as $pattern => $replacement) { |
|
114
|
3 |
|
$string = mb_ereg_replace($pattern, $replacement, $string); |
|
115
|
3 |
|
} |
|
116
|
3 |
|
} |
|
117
|
|
|
|
|
118
|
6 |
|
return mb_ereg_replace('Macmurdo', 'MacMurdo', $string); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|