Completed
Push — master ( d5d5b9...a4192b )
by Guillermo
02:05
created

StringHandler::removeSpecialChars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Atrapalo\PHPTools\String;
4
5
/**
6
 * Class StringHandler
7
 * @package Atrapalo\PHPTools\String
8
 */
9
class StringHandler
10
{
11
    /**
12
     * @param string $text
13
     * @return string
14
     */
15
    public static function removeAccentsByString(string $text): string
16
    {
17
        $charactersWithAccents = self::mbStringToArray(
18
            "ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿºªç",
19
            "UTF-8"
20
        );
21
22
        $charactersWithoutAccents = self::mbStringToArray(
23
            "SOZsozYYuAAAAAAACEEEEIIIIDNOOOOOOUUUUYsaaaaaaaceeeeiiiionoooooouuuuyyoac",
24
            "UTF-8"
25
        );
26
27
        return str_replace($charactersWithAccents, $charactersWithoutAccents, $text);
28
    }
29
30
    /**
31
     * @param string $text
32
     * @param string $encoding
33
     * @return array
34
     */
35
    private static function mbStringToArray(string $text, string $encoding): array
36
    {
37
        $result = [];
38
        $length = mb_strlen($text);
39
40
        while ($length) {
41
            $result[] = mb_substr($text, 0, 1, $encoding);
42
            $text = mb_substr($text, 1, $length, $encoding);
43
            $length = mb_strlen($text);
44
        }
45
46
        return $result;
47
    }
48
49
    /**
50
     * @param string $url
51
     * @return string
52
     */
53
    public static function sanitizeUrl(string $url): string
54
    {
55
        $encoding = mb_detect_encoding($url);
56
        $url = mb_convert_case($url, MB_CASE_LOWER, $encoding);
57
58
        $needle = [
59
            'a%9A','ó', 'ý', 'Ã', 'Õ', 'ã', 'õ', '`', "'", '&amp;',
60
            'ñ', 'ç', '-', ' ', 'à', 'è', 'ì','ò', 'ù', 'á',
61
            'é', 'í', 'ó', 'ú', '/', '´', '"', 'Á', 'É', 'Í',
62
            'Ó', 'Ú', 'ä', 'ë', 'ï', 'ö','ü', 'â', 'ê', 'î',
63
            'ô', 'û', '€', '$', '&', '!', '¡', '?', '¿',
64
            '=', '(', ')', '%', '+',',', '.', ';', '@', ':',
65
            '%', '*','º','ª','å','ø', '#', 'ß', 'æ', 'î', ',,'
66
        ];
67
68
        $haystack =  ['u', 'o', 'y', 'a', 'o', 'a', 'o', '-', '-', 'i',
69
            'n', 'c', '-', '-', 'a', 'e', 'i', 'o', 'u','a',
70
            'e', 'i', 'o', 'u', '-', '-', '', 'a', 'e', 'i',
71
            'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e','i',
72
            'o', 'u', 'euro', 'dollar', 'i', '', '', '', '',
73
            '', '-', '-', '', '', '-', '-', '-','a', '-',
74
            '-', '-','','','a','o', '-', 'ss', 'ae', 'i', '-'
75
        ];
76
77
        $url = urlencode(str_ireplace($needle, $haystack, strtolower($url)));
78
        $url = str_replace(['-+', '---', '--'], '-', $url);
79
80
        return $url;
81
    }
82
83
    /**
84
     * @param string $text
85
     * @return string
86
     */
87
    public static function sanitizeString(string $text): string
88
    {
89
        return self::removeSpecialChars(self::removeAccentsByString($text));
90
    }
91
92
    /**
93
     * @param string $text
94
     * @return string
95
     */
96
    public static function removeSpecialChars(string $text): string
97
    {
98
        $text = preg_replace('/[^A-Za-z0-9]/', ' ', $text);
99
100
        return self::removeExtraSpaces($text);
101
    }
102
103
    /**
104
     * @param string $text
105
     * @return string
106
     */
107
    public static function removeExtraSpaces(string $text): string
108
    {
109
        return trim(preg_replace('/\s+/', ' ', $text));
110
    }
111
}
112