Completed
Push — master ( 511b57...ecc10d )
by Yuri
03:19 queued 23s
created

Formatter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 2 Features 2
Metric Value
wmc 11
c 10
b 2
f 2
lcom 1
cbo 0
dl 0
loc 92
ccs 40
cts 40
cp 1
rs 10

3 Methods

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