Completed
Push — master ( 582f7c...946ecd )
by Yuri
01:57
created

Formatter::capitalize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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