Passed
Pull Request — master (#6)
by Mauro
08:26
created

Strings   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 157
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 157
rs 10
c 1
b 0
f 0
wmc 21

11 Methods

Rating   Name   Duplication   Size   Complexity  
A htmlEntityDecode() 0 3 1
A cutNbsps() 0 3 1
A isMultibyte() 0 3 1
A token() 0 10 2
A split() 0 3 1
A lastChar() 0 3 1
A charMap() 0 7 1
A unprotectHTMLTags() 0 6 1
A contains() 0 7 2
A firstChar() 0 3 1
B protectHTMLTags() 0 45 9
1
<?php
2
3
namespace Matecat\Finder\Helper;
4
5
class Strings
6
{
7
    /**
8
     * @param string $string
9
     *
10
     * @return string
11
     */
12
    public static function cutNbsps($string)
13
    {
14
        return str_replace(['&nbsp;', ' '], ' ', $string);
15
    }
16
17
    /**
18
     * @param string $string
19
     *
20
     * @return string
21
     */
22
    public static function htmlEntityDecode($string)
23
    {
24
        return html_entity_decode($string, ENT_QUOTES|ENT_XHTML, 'UTF-8');
25
    }
26
27
    /**
28
     * @param string $string
29
     *
30
     * @return array|bool|string[]|null
31
     */
32
    public static function split($string)
33
    {
34
        return mb_str_split($string);
35
    }
36
37
    /**
38
     * @param int $length
39
     *
40
     * @return string
41
     */
42
    public static function token($length = 8)
43
    {
44
        $key = '';
45
        $keys = array_merge(range(0, 9), range('a', 'z'));
46
47
        for ($i = 0; $i < $length; $i++) {
48
            $key .= $keys[array_rand($keys)];
49
        }
50
51
        return $key;
52
    }
53
54
    /**
55
     * @param string $string
56
     *
57
     * @return bool
58
     */
59
    public static function isMultibyte($string)
60
    {
61
        return ((strlen($string) - mb_strlen($string)) > 0);
62
    }
63
64
    /**
65
     * @param $needle
66
     * @param $haystack
67
     *
68
     * @return bool
69
     */
70
    public static function contains($needle, $haystack)
71
    {
72
        if(empty($haystack)){
73
            return false;
74
        }
75
76
        return strpos($haystack, $needle) !== false;
77
    }
78
79
    public static function firstChar($string)
80
    {
81
        return $string[0];
82
    }
83
84
    public static function lastChar($string)
85
    {
86
        return $string[strlen($string) - 1];
87
    }
88
89
    /**
90
     * @param string $string
91
     * @return string
92
     */
93
    public static function protectHTMLTags($string)
94
    {
95
        preg_match_all('/&lt;(.*?)&gt;|<(.*?)>/sm', $string, $matches);
96
97
        if(!empty($matches[0])){
98
            foreach ($matches[0] as $index => $element){
99
100
                $tag = explode(" ", $element);
101
                $tag = str_replace(["<", ">", "&lt;", "&gt;", "/"], "", $tag[0]);
102
103
                $nextElement = $matches[0][$index+1] ?? null;
104
105
                if(!self::contains("/", $element)){
106
107
                    if($nextElement === null){
108
                        continue;
109
                    }
110
111
                    $nextTag = explode(" ", $nextElement);
112
                    $nextTag = str_replace(["<", ">", "&lt;", "&gt;", "/"], "", $nextTag[0]);
113
114
                    if($nextTag !== $tag){
115
                        continue;
116
                    }
117
                } else {
118
                    // self closing tag
119
                    $closingTag = explode(" ", $element);
120
                    $closingTag = str_replace(["<", ">", "&lt;", "&gt;"], "", $closingTag[0]);
121
122
                    if(empty($closingTag)){
123
                        continue;
124
                    }
125
126
                    if(!(self::firstChar($closingTag) === "/" or self::lastChar($closingTag) === "/")){
127
                        continue;
128
                    }
129
                }
130
131
                $charMap = self::charMap();
132
                $protectedTag = str_replace(["<", ">", "&lt;", "&gt;"], [$charMap["<"], $charMap[">"], $charMap["&lt;"], $charMap["&gt;"]], $element);
133
                $string = str_replace($element, $protectedTag, $string);
134
            }
135
        }
136
137
        return $string;
138
    }
139
140
    /**
141
     * @param string $string
142
     * @return string
143
     */
144
    public static function unprotectHTMLTags($string)
145
    {
146
        $charMap = self::charMap();
147
        $string = str_replace([$charMap["<"], $charMap[">"], $charMap["&lt;"], $charMap["&gt;"]], ["<", ">", "&lt;", "&gt;"], $string);
148
149
        return $string;
150
    }
151
152
    /**
153
     * @return array
154
     */
155
    private static function charMap()
156
    {
157
        return [
158
            "<"    => "ʃʃʃʃ",
159
            ">"    => "¶¶¶¶",
160
            "&lt;" => "ɑɑɑɑ",
161
            "&gt;" => "ʒʒʒʒ",
162
        ];
163
    }
164
}