StringHandler::sanitizeString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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