Completed
Push — master ( ecc10d...888771 )
by Yuri
03:56
created

Formatter::updateIrish()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.2
cc 4
eloc 8
nc 2
nop 1
crap 4
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 $exceptionsIrish = [
15
        '\bMacEdo'     => 'Macedo',
16
        '\bMacEvicius' => 'Macevicius',
17
        '\bMacHado'    => 'Machado',
18
        '\bMacHar'     => 'Machar',
19
        '\bMacHin'     => 'Machin',
20
        '\bMacHlin'    => 'Machlin',
21
        '\bMacIas'     => 'Macias',
22
        '\bMacIulis'   => 'Maciulis',
23
        '\bMacKie'     => 'Mackie',
24
        '\bMacKle'     => 'Mackle',
25
        '\bMacKlin'    => 'Macklin',
26
        '\bMacKmin'    => 'Mackmin',
27
        '\bMacQuarie'  => 'Macquarie',
28
    ];
29
30
    /**
31
     * @param string $string
32
     * @param array  $options
33
     *
34
     * @return string
35
     */
36 6
    public static function nc($string, array $options = [])
37
    {
38 6
        $options = array_merge(self::$options, $options);
39
40
        // Do not do anything if string is mixed and lazy option is true.
41 6
        if ($options['lazy']) {
42 6
            if (self::skipMixed($string)) return $string;
43 6
        }
44
45
        // Capitalize
46 6
        $local = mb_convert_case(mb_strtolower($string), MB_CASE_TITLE);
47
48 6
        if ($options['irish']) {
49 6
            $local = self::updateIrish($local);
50 6
        }
51
52 6
        return $local;
53
    }
54
55
    /**
56
     * Skip if string is mixed case.
57
     *
58
     * @param string $string
59
     *
60
     * @return bool
61
     */
62 6
    private static function skipMixed($string)
63
    {
64 6
        $firstLetterLower = $string[0] == mb_strtolower($string[0]);
65 6
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
66
67 6
        return ! ($firstLetterLower || $allLowerOrUpper);
68
    }
69
70
    /**
71
     * Fix Irish names.
72
     *
73
     * @param string $string
74
     *
75
     * @return string
76
     */
77 6
    private static function updateIrish($string)
78
    {
79 6
        if (mb_ereg_match('\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('\bMc', $string)) {
80
81 3
            $string = \mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) {
82 3
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
83 3
            }, $string);
84
85
            // Now fix "Mac" exceptions
86 3
            foreach (self::$exceptionsIrish as $pattern => $replacement) {
87 3
                $string = mb_ereg_replace($pattern, $replacement, $string);
88 3
            }
89 3
        }
90
91 6
        return mb_ereg_replace('Macmurdo', 'MacMurdo', $string);
92
    }
93
}
94