Completed
Push — master ( 14b3d0...606d66 )
by Yuri
02:15
created

Formatter   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 165
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 16
Bugs 3 Features 3
Metric Value
wmc 16
c 16
b 3
f 3
lcom 1
cbo 0
dl 0
loc 165
ccs 40
cts 40
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A skipMixed() 0 7 3
B nc() 0 25 4
A capitalize() 0 15 1
B updateIrish() 0 18 5
A fixConjunction() 0 10 3
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
        // Fix roman numeral names.
80
        $string = mb_ereg_replace_callback(self::$romanRegex, function ($matches) {
81 6
            return mb_strtoupper($matches[0]);
82 6
        }, $string);
83
84 6
        $string = self::fixConjunction($string);
85
86 6
        return $string;
87
    }
88
89
    /**
90
     * Capitalize first letters.
91
     *
92
     * @param string $string
93
     *
94
     * @return string
95
     */
96 6
    private static function capitalize($string)
97
    {
98 6
        $string = mb_strtolower($string);
99
100
        $string = mb_ereg_replace_callback('\b\w', function ($matches) {
101 6
            return mb_strtoupper($matches[0]);
102 6
        }, $string);
103
104
        // Lowercase 's
105
        $string = mb_ereg_replace_callback('\'\w\b', function ($matches) {
106 3
            return mb_strtolower($matches[0]);
107 6
        }, $string);
108
109 6
        return $string;
110
    }
111
112
    /**
113
     * Skip if string is mixed case.
114
     *
115
     * @param string $string
116
     *
117
     * @return bool
118
     */
119 6
    private static function skipMixed($string)
120
    {
121 6
        $firstLetterLower = $string[0] == mb_strtolower($string[0]);
122 6
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
123
124 6
        return ! ($firstLetterLower || $allLowerOrUpper);
125
    }
126
127
    /**
128
     * Update for Irish names.
129
     *
130
     * @param string $string
131
     *
132
     * @return string
133
     */
134 6
    private static function updateIrish($string)
135
    {
136 6
        if ( ! self::$options['irish']) return $string;
137
138 6
        if (mb_ereg_match('.*?\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('.*?\bMc', $string)) {
139
140 3
            $string = mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) {
141 3
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
142 3
            }, $string);
143
144
            // Now fix "Mac" exceptions
145 3
            foreach (self::$exceptions as $pattern => $replacement) {
146 3
                $string = mb_ereg_replace($pattern, $replacement, $string);
147 3
            }
148 3
        }
149
150 6
        return mb_ereg_replace('Macmurdo', 'MacMurdo', $string);
151
    }
152
153
    /**
154
     * Fix Spanish conjunctions.
155
     *
156
     * @param string $string
157
     *
158
     * @return string
159
     */
160 6
    private static function fixConjunction($string)
161
    {
162 6
        if ( ! self::$options['spanish']) return $string;
163
164 6
        foreach (self::$conjunctions as $conjunction) {
165 6
            $string = mb_ereg_replace('\b' . $conjunction . '\b', mb_strtolower($conjunction), $string);
166 6
        }
167
168 6
        return $string;
169
    }
170
}
171