Completed
Push — master ( de1469...4e6aee )
by Yuri
03:16
created

Formatter::nc()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 10.1554

Importance

Changes 13
Bugs 1 Features 3
Metric Value
c 13
b 1
f 3
dl 0
loc 21
ccs 3
cts 11
cp 0.2727
rs 9.0534
cc 4
eloc 10
nc 3
nop 2
crap 10.1554
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 ($options['lazy'] && self::skipMixed($string)) return $string;
69
70
        // Capitalize
71
        $string = self::capitalize($string);
72
        $string = self::updateIrish($string);
73
74
        // Fixes for "son (daughter) of" etc
75
        foreach (self::$replacements as $pattern => $replacement) {
76
            $string = mb_ereg_replace($pattern, $replacement, $string);
77
        }
78
79
        $string = self::updateRoman($string);
80
        $string = self::fixConjunction($string);
81
82
        return $string;
83
    }
84
85
    /**
86
     * Capitalize first letters.
87
     *
88
     * @param string $string
89
     *
90
     * @return string
91
     */
92
    private static function capitalize($string)
93
    {
94
        $string = mb_strtolower($string);
95
96
        $string = mb_ereg_replace_callback('\b\w', function ($matches) {
97
            return mb_strtoupper($matches[0]);
98
        }, $string);
99
100
        // Lowercase 's
101
        $string = mb_ereg_replace_callback('\'\w\b', function ($matches) {
102
            return mb_strtolower($matches[0]);
103
        }, $string);
104
105
        return $string;
106
    }
107
108
    /**
109
     * Skip if string is mixed case.
110
     *
111
     * @param string $string
112
     *
113
     * @return bool
114
     */
115
    private static function skipMixed($string)
116
    {
117
        $firstLetterLower = $string[0] == mb_strtolower($string[0]);
118
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
119
120
        return ! ($firstLetterLower || $allLowerOrUpper);
121
    }
122
123
    /**
124
     * Update for Irish names.
125
     *
126
     * @param string $string
127
     *
128
     * @return string
129
     */
130
    private static function updateIrish($string)
131
    {
132
        if ( ! self::$options['irish']) return $string;
133
134
        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
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
138
            }, $string);
139
140
            // Now fix "Mac" exceptions
141
            foreach (self::$exceptions as $pattern => $replacement) {
142
                $string = mb_ereg_replace($pattern, $replacement, $string);
143
            }
144
        }
145
146
        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
    private static function fixConjunction($string)
157
    {
158
        if ( ! self::$options['spanish']) return $string;
159
160
        foreach (self::$conjunctions as $conjunction) {
161
            $string = mb_ereg_replace('\b' . $conjunction . '\b', mb_strtolower($conjunction), $string);
162
        }
163
164
        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
        return mb_ereg_replace_callback(self::$romanRegex, function ($matches) {
177
            return mb_strtoupper($matches[0]);
178
        }, $string);
179
    }
180
}
181