StringUtils::removeAccents()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace JimmyOak\Utility;
4
5
class StringUtils extends UtilsBase
6
{
7
    const CASE_INSENSITIVE   = 1;
8
    const ACCENT_INSENSITIVE = 2;
9
10
    private $accentsMap = array(
11
        'Š' => 'S',
12
        'š' => 's',
13
        'Ž' => 'Z',
14
        'ž' => 'z',
15
        'À' => 'A',
16
        'Á' => 'A',
17
        'Â' => 'A',
18
        'Ã' => 'A',
19
        'Ä' => 'A',
20
        'Å' => 'A',
21
        'Æ' => 'A',
22
        'Ç' => 'C',
23
        'È' => 'E',
24
        'É' => 'E',
25
        'Ê' => 'E',
26
        'Ë' => 'E',
27
        'Ì' => 'I',
28
        'Í' => 'I',
29
        'Î' => 'I',
30
        'Ï' => 'I',
31
        'Ñ' => 'N',
32
        'Ò' => 'O',
33
        'Ó' => 'O',
34
        'Ô' => 'O',
35
        'Õ' => 'O',
36
        'Ö' => 'O',
37
        'Ø' => 'O',
38
        'Ù' => 'U',
39
        'Ú' => 'U',
40
        'Û' => 'U',
41
        'Ü' => 'U',
42
        'Ý' => 'Y',
43
        'Þ' => 'B',
44
        'ß' => 'Ss',
45
        'à' => 'a',
46
        'á' => 'a',
47
        'â' => 'a',
48
        'ã' => 'a',
49
        'ä' => 'a',
50
        'å' => 'a',
51
        'æ' => 'a',
52
        'ç' => 'c',
53
        'è' => 'e',
54
        'é' => 'e',
55
        'ê' => 'e',
56
        'ë' => 'e',
57
        'ì' => 'i',
58
        'í' => 'i',
59
        'î' => 'i',
60
        'ï' => 'i',
61
        'ð' => 'o',
62
        'ñ' => 'n',
63
        'ò' => 'o',
64
        'ó' => 'o',
65
        'ô' => 'o',
66
        'õ' => 'o',
67
        'ö' => 'o',
68
        'ø' => 'o',
69
        'ù' => 'u',
70
        'ú' => 'u',
71
        'û' => 'u',
72
        'ý' => 'y',
73
        'þ' => 'b',
74
        'ÿ' => 'y'
75
    );
76
77
    private $urlRegExp;
78
    private $emailRegExp;
79
80
    protected function __construct()
81
    {
82
        $this->urlRegExp = '%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|' .
83
            '(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{0' .
84
            '0a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6})' .
85
            ')(?::\d+)?(?:[^\s]*)?$%iu';
86
87
        $this->emailRegExp = '/^([a-z0-9_.-])+@([a-z0-9_.-])+\.([a-z])+([a-z])+/i';
88
    }
89
90
    /**
91
     * @param string $haystack
92
     * @param string $needle
93
     * @param int|null $modifiers
94
     *
95
     * @return bool
96
     */
97 View Code Duplication
    public function beginsWith($haystack, $needle, $modifiers = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
    {
99
        $this->prepareHaystackAndNeedle($haystack, $needle, $modifiers);
100
        $parsedModifiers = $this->parseModifiers($modifiers);
101
102
        $pattern = '/^' . preg_quote($needle) . '/';
103
104
        return (bool)preg_match($pattern . $parsedModifiers, $haystack);
105
    }
106
107
    /**
108
     * @param string $string
109
     *
110
     * @return string
111
     */
112
    public function removeAccents($string)
113
    {
114
        return strtr($string, $this->accentsMap);
115
    }
116
117
    /**
118
     * @param string $haystack
119
     * @param string $needle
120
     * @param int|null $modifiers
121
     *
122
     * @return bool
123
     */
124 View Code Duplication
    public function endsWith($haystack, $needle, $modifiers = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126
        self::prepareHaystackAndNeedle($haystack, $needle, $modifiers);
127
        $parsedModifiers = $this->parseModifiers($modifiers);
128
129
        $pattern = '/' . preg_quote($needle) . '$/';
130
131
        return (bool)preg_match($pattern . $parsedModifiers, $haystack);
132
    }
133
134
    /**
135
     * @param string $string
136
     *
137
     * @return string
138
     */
139
    public function removeExtraSpaces($string)
140
    {
141
        return trim(preg_replace('/\s+/', ' ', $string));
142
    }
143
144
    /**
145
     * @param string $string
146
     *
147
     * @return bool
148
     */
149
    public function isUrl($string)
150
    {
151
        return preg_match($this->urlRegExp, $string) > 0;
152
    }
153
154
    /**
155
     * @param string $string
156
     *
157
     * @return bool
158
     */
159
    public function isEmail($string)
160
    {
161
        return preg_match($this->emailRegExp, $string) > 0;
162
    }
163
164
    private function parseModifiers($modifiers)
165
    {
166
        $parsedModifiers = '';
167
168
        if ($modifiers & self::CASE_INSENSITIVE) {
169
            $parsedModifiers .= 'i';
170
        }
171
172
        return $parsedModifiers;
173
    }
174
175
    private function isAccentInsensitiveModifier($modifiers)
176
    {
177
        return $modifiers & self::ACCENT_INSENSITIVE;
178
    }
179
180
    private function prepareHaystackAndNeedle(&$haystack, &$needle, $modifiers)
181
    {
182
        if ($this->isAccentInsensitiveModifier($modifiers)) {
183
            $haystack = $this->removeAccents($haystack);
184
            $needle   = $this->removeAccents($needle);
185
        }
186
    }
187
}
188