Completed
Push — master ( c95d8a...138ca9 )
by Yuri
02:22
created

Formatter::nc()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 25
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6

Importance

Changes 11
Bugs 1 Features 3
Metric Value
c 11
b 1
f 3
dl 0
loc 25
ccs 14
cts 14
cp 1
rs 8.439
cc 6
eloc 12
nc 17
nop 2
crap 6
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
    /**
53
     * Main function for NameCase.
54
     *
55
     * @param string $string
56
     * @param array  $options
57
     *
58
     * @return string
59
     */
60 6
    public static function nc($string, array $options = [])
61
    {
62 6
        $options = array_merge(self::$options, $options);
63
64
        // Do not do anything if string is mixed and lazy option is true.
65 6
        if ($options['lazy']) {
66 6
            if (self::skipMixed($string)) return $string;
67 6
        }
68
69
        // Capitalize
70 6
        $string = self::capitalize($string);
71
72 6
        if ($options['irish'])
73 6
            $string = self::updateIrish($string);
74
75
        // Fixes for "son (daughter) of" etc
76 6
        foreach (self::$replacements as $pattern => $replacement) {
77 6
            $string = mb_ereg_replace($pattern, $replacement, $string);
78 6
        }
79
80 6
        if ($options['spanish'])
81 6
            $string = self::fixConjunction($string);
82
83 6
        return $string;
84
    }
85
86
    /**
87
     * Capitalize first letters.
88
     *
89
     * @param string $string
90
     *
91
     * @return string
92
     */
93 6
    private static function capitalize($string)
94
    {
95 6
        $string = mb_strtolower($string);
96
97
        $string = mb_ereg_replace_callback('\b\w', function ($matches) {
98 6
            return mb_strtoupper($matches[0]);
99 6
        }, $string);
100
101
        // Lowercase 's
102
        $string = mb_ereg_replace_callback('\'\w\b', function ($matches) {
103 3
            return mb_strtolower($matches[0]);
104 6
        }, $string);
105
106 6
        return $string;
107
    }
108
109
    /**
110
     * Skip if string is mixed case.
111
     *
112
     * @param string $string
113
     *
114
     * @return bool
115
     */
116 6
    private static function skipMixed($string)
117
    {
118 6
        $firstLetterLower = $string[0] == mb_strtolower($string[0]);
119 6
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
120
121 6
        return ! ($firstLetterLower || $allLowerOrUpper);
122
    }
123
124
    /**
125
     * Update for Irish names.
126
     *
127
     * @param string $string
128
     *
129
     * @return string
130
     */
131 6
    private static function updateIrish($string)
132
    {
133 6
        if (mb_ereg_match('.*?\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('.*?\bMc', $string)) {
134
135 3
            $string = mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) {
136 3
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
137 3
            }, $string);
138
139
            // Now fix "Mac" exceptions
140 3
            foreach (self::$exceptions as $pattern => $replacement) {
141 3
                $string = mb_ereg_replace($pattern, $replacement, $string);
142 3
            }
143 3
        }
144
145 6
        return mb_ereg_replace('Macmurdo', 'MacMurdo', $string);
146
    }
147
148
    /**
149
     * Fix Spanish conjunctions.
150
     *
151
     * @param string $string
152
     *
153
     * @return string
154
     */
155 6
    private static function fixConjunction($string)
156
    {
157 6
        foreach (self::$conjunctions as $conjunction) {
158 6
            $string = mb_ereg_replace('\b' . $conjunction . '\b', mb_strtolower($conjunction), $string);
159 6
        }
160
161 6
        return $string;
162
    }
163
}
164