Completed
Push — master ( 4e6aee...14b3d0 )
by Yuri
03:42
created

Formatter::updateRoman()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php namespace Tamtamchik\NameCase;
2
3
/**
4
 * Class Formatter.
5
 */
6
class Formatter
7
{
8
    // Default options.
9
    private static $options = [
10
        'lazy'    => true,
11
        'irish'   => true,
12
        'spanish' => true,
13
    ];
14
15
    // Irish exceptions.
16
    private static $exceptions = [
17
        '\bMacEdo'     => 'Macedo',
18
        '\bMacEvicius' => 'Macevicius',
19
        '\bMacHado'    => 'Machado',
20
        '\bMacHar'     => 'Machar',
21
        '\bMacHin'     => 'Machin',
22
        '\bMacHlin'    => 'Machlin',
23
        '\bMacIas'     => 'Macias',
24
        '\bMacIulis'   => 'Maciulis',
25
        '\bMacKie'     => 'Mackie',
26
        '\bMacKle'     => 'Mackle',
27
        '\bMacKlin'    => 'Macklin',
28
        '\bMacKmin'    => 'Mackmin',
29
        '\bMacQuarie'  => 'Macquarie',
30
    ];
31
32
    // General replacements.
33
    private static $replacements = [
34
        '\bAl(?=\s+\w)'         => 'al',        // al Arabic or forename Al.
35
        '\b(Bin|Binti|Binte)\b' => 'bin',       // bin, binti, binte Arabic
36
        '\bAp\b'                => 'ap',        // ap Welsh.
37
        '\bBen(?=\s+\w)'        => 'ben',       // ben Hebrew or forename Ben.
38
        '\bDell([ae])\b'        => 'dell\1',    // della and delle Italian.
39
        '\bD([aeiou])\b'        => 'd\1',       // da, de, di Italian; du French; do Brasil
40
        '\bD([ao]s)\b'          => 'd\1',       // das, dos Brasileiros
41
        '\bDe([lr])\b'          => 'de\1',      // del Italian; der Dutch/Flemish.
42
        '\bEl\b'                => 'el',        // el Greek or El Spanish.
43
        '\bLa\b'                => 'la',        // la French or La Spanish.
44
        '\bL([eo])\b'           => 'l\1',       // lo Italian; le French.
45
        '\bVan(?=\s+\w)'        => 'van',       // van German or forename Van.
46
        '\bVon\b'               => 'von',       // von Dutch/Flemish
47
    ];
48
49
    // Spanish conjunctions.
50
    private static $conjunctions = ["Y", "E", "I"];
51
52
    // Roman letters regexp.
53
    private static $romanRegex = '\b((?:[Xx]{1,3}|[Xx][Ll]|[Ll][Xx]{0,3})?(?:[Ii]{1,3}|[Ii][VvXx]|[Vv][Ii]{0,3})?)\b';
54
55
    /**
56
     * Main function for NameCase.
57
     *
58
     * @param string $string
59
     * @param array  $options
60
     *
61
     * @return string
62
     */
63 6
    public static function nc($string, array $options = [])
64
    {
65 6
        self::$options = array_merge(self::$options, $options);
66
67
        // Do not do anything if string is mixed and lazy option is true.
68 6
        if (self::$options['lazy'] && self::skipMixed($string)) return $string;
69
70
        // Capitalize
71 6
        $string = self::capitalize($string);
72 6
        $string = self::updateIrish($string);
73
74
        // Fixes for "son (daughter) of" etc
75 6
        foreach (self::$replacements as $pattern => $replacement) {
76 6
            $string = mb_ereg_replace($pattern, $replacement, $string);
77 6
        }
78
79 6
        $string = self::updateRoman($string);
80 6
        $string = self::fixConjunction($string);
81
82 6
        return $string;
83
    }
84
85
    /**
86
     * Capitalize first letters.
87
     *
88
     * @param string $string
89
     *
90
     * @return string
91
     */
92 6
    private static function capitalize($string)
93
    {
94 6
        $string = mb_strtolower($string);
95
96
        $string = mb_ereg_replace_callback('\b\w', function ($matches) {
97 6
            return mb_strtoupper($matches[0]);
98 6
        }, $string);
99
100
        // Lowercase 's
101
        $string = mb_ereg_replace_callback('\'\w\b', function ($matches) {
102 3
            return mb_strtolower($matches[0]);
103 6
        }, $string);
104
105 6
        return $string;
106
    }
107
108
    /**
109
     * Skip if string is mixed case.
110
     *
111
     * @param string $string
112
     *
113
     * @return bool
114
     */
115 6
    private static function skipMixed($string)
116
    {
117 6
        $firstLetterLower = $string[0] == mb_strtolower($string[0]);
118 6
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
119
120 6
        return ! ($firstLetterLower || $allLowerOrUpper);
121
    }
122
123
    /**
124
     * Update for Irish names.
125
     *
126
     * @param string $string
127
     *
128
     * @return string
129
     */
130 6
    private static function updateIrish($string)
131
    {
132 6
        if ( ! self::$options['irish']) return $string;
133
134 6
        if (mb_ereg_match('.*?\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('.*?\bMc', $string)) {
135
136
            $string = mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) {
137 3
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
138 3
            }, $string);
139
140
            // Now fix "Mac" exceptions
141 3
            foreach (self::$exceptions as $pattern => $replacement) {
142 3
                $string = mb_ereg_replace($pattern, $replacement, $string);
143 3
            }
144 3
        }
145
146 6
        return mb_ereg_replace('Macmurdo', 'MacMurdo', $string);
147
    }
148
149
    /**
150
     * Fix Spanish conjunctions.
151
     *
152
     * @param string $string
153
     *
154
     * @return string
155
     */
156 6
    private static function fixConjunction($string)
157
    {
158 6
        if ( ! self::$options['spanish']) return $string;
159
160 6
        foreach (self::$conjunctions as $conjunction) {
161 6
            $string = mb_ereg_replace('\b' . $conjunction . '\b', mb_strtolower($conjunction), $string);
162 6
        }
163
164 6
        return $string;
165
    }
166
167
    /**
168
     * Fix roman numeral names.
169
     *
170
     * @param $string
171
     *
172
     * @return string
173
     */
174
    private static function updateRoman($string)
175
    {
176 6
        return mb_ereg_replace_callback(self::$romanRegex, function ($matches) {
177 6
            return mb_strtoupper($matches[0]);
178 6
        }, $string);
179
    }
180
}
181