Completed
Push — master ( 4c1cbe...6a7787 )
by Yuri
01:57
created

Formatter::nc()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.2
cc 4
eloc 9
nc 5
nop 2
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
    /**
15
     * @param string $string
16
     * @param array  $options
17
     *
18
     * @return string
19
     */
20 6
    public static function nc($string, array $options = [])
21
    {
22 6
        $options = array_merge(self::$options, $options);
23
24
        // Do not do anything if string is mixed and lazy option is true.
25 6
        if ($options['lazy']) {
26 6
            if (self::skipMixed($string)) return $string;
27 6
        }
28
29 6
        $local = mb_strtolower($string);
30
31
        // Capitalize
32 6
        $local = self::capitalize($local);
33
34 6
        if ($options['irish']) {
35 6
            $local = self::fixIrish($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]);
51 6
        $allLowerOrUpper  = (mb_strtolower($string) == $string || mb_strtoupper($string) == $string);
52
53 6
        return ! ($firstLetterLower || $allLowerOrUpper);
54
    }
55
56
    /**
57
     * Capitalize first letters.
58
     *
59
     * @param string $string
60
     *
61
     * @return string
62
     */
63 6
    private static function capitalize($string)
64
    {
65
        $string = mb_ereg_replace_callback('\b\w', function ($matches) {
66 6
            return mb_strtoupper($matches[0]);
67 6
        }, $string);
68
69
        // Lowercase 's
70
        $string = mb_ereg_replace_callback('\'\w\b', function ($matches) {
71 3
            return mb_strtolower($matches[0]);
72 6
        }, $string);
73
74 6
        return $string;
75
    }
76
77
    /**
78
     * Fix Irish names.
79
     *
80
     * @param string $string
81
     *
82
     * @return string
83
     */
84 6
    private static function fixIrish($string)
85
    {
86 6
        if (mb_ereg_match('\bMac[A-Za-z]{2,}[^aciozj]\b', $string) || mb_ereg_match('\bMc', $string)) {
87 3
            $string = mb_ereg_replace_callback('\b(Ma?c)([A-Za-z]+)', function ($matches) {
88 3
                return $matches[1] . mb_strtoupper(mb_substr($matches[2], 0, 1)) . mb_substr($matches[2], 1);
89 3
            }, $string);
90
91
            // Now fix "Mac" exceptions
92 3
            $string = mb_ereg_replace('\bMacEdo', 'Macedo', $string);
93 3
            $string = mb_ereg_replace('\bMacEvicius', 'Macevicius', $string);
94 3
            $string = mb_ereg_replace('\bMacHado', 'Machado', $string);
95 3
            $string = mb_ereg_replace('\bMacHar', 'Machar', $string);
96 3
            $string = mb_ereg_replace('\bMacHin', 'Machin', $string);
97 3
            $string = mb_ereg_replace('\bMacHlin', 'Machlin', $string);
98 3
            $string = mb_ereg_replace('\bMacIas', 'Macias', $string);
99 3
            $string = mb_ereg_replace('\bMacIulis', 'Maciulis', $string);
100 3
            $string = mb_ereg_replace('\bMacKie', 'Mackie', $string);
101 3
            $string = mb_ereg_replace('\bMacKle', 'Mackle', $string);
102 3
            $string = mb_ereg_replace('\bMacKlin', 'Macklin', $string);
103 3
            $string = mb_ereg_replace('\bMacKmin', 'Mackmin', $string);
104 3
            $string = mb_ereg_replace('\bMacQuarie', 'Macquarie', $string);
105 3
        }
106
107 6
        return mb_ereg_replace('Macmurdo', 'MacMurdo', $string);
108
    }
109
}
110