1<?php
2
3namespace ComboStrap;
4
5/**
6 * Class StringUtility
7 * @package ComboStrap
8 * A class with string utility
9 */
10class StringUtility
11{
12
13
14    /**
15     * Generate a text with a max length of $length
16     * and add ... if above
17     * @param $myString
18     * @param $length
19     * @return string
20     */
21    static function truncateString($myString, $length): string
22    {
23
24        if (strlen($myString) > $length) {
25            $suffix = ' ...';
26            $myString = substr($myString, 0, ($length - 1) - strlen($suffix)) . $suffix;
27        }
28        return $myString;
29    }
30
31    /**
32     * @param $string
33     * @return string - the string without any carriage return
34     * Used to compare string without worrying about carriage return
35     */
36    public static function normalized($string)
37    {
38        return str_replace("\n", "", $string);
39    }
40
41    /**
42     * @param $needle
43     * @param $haystack
44     * @return bool
45     */
46    public static function contain($needle, $haystack)
47    {
48        $pos = strpos($haystack, $needle);
49        if ($pos === FALSE) {
50            return false;
51        } else {
52            return true;
53        }
54    }
55
56    public static function toString($value)
57    {
58        /**
59         * No transformation if it's a string
60         * var_export below is not idempotent
61         * ie \ would become \\
62         */
63        if(is_string($value)){
64            return $value;
65        }
66
67        $string = var_export($value, true);
68
69        // An array value gets command in var_export
70        $lastCharacterIndex = strlen($string) - 1;
71        if ($string[0] === "'" && $string[$lastCharacterIndex] === "'") {
72            $string = substr($string, 1, strlen($string) - 2);
73        }
74        return $string;
75
76    }
77
78    /**
79     * Add an EOL if not present at the end of the string
80     * @param $doc
81     */
82    public static function addEolCharacterIfNotPresent(&$doc)
83    {
84        if ($doc[strlen($doc) - 1] != DOKU_LF) {
85            $doc .= DOKU_LF;
86        }
87    }
88
89    /**
90     * Delete the string from the end
91     * This is used generally to delete the previous opening tag of an header or a blockquote
92     * @param $doc
93     * @param $string
94     */
95    public static function rtrim(&$doc, $string)
96    {
97
98        /**
99         * We trim because in the process, we may get extra {@link DOKU_LF} at the end
100         */
101        $doc = trim($doc);
102        $string = trim($string);
103        $length = strlen($doc) - strlen($string);
104        if (substr($doc, $length) === $string) {
105            $doc = substr($doc, 0, $length);
106        }
107
108    }
109
110    /**
111     * Delete the string from the beginning
112     * This is used to delete a tag for instance
113     * @param $doc
114     * @param $string
115     */
116    public static function ltrim(&$doc, $string)
117    {
118
119        $doc = trim($doc);
120        $string = trim($string);
121        $length = strlen($string);
122        if (substr($doc, 0, $length) === $string) {
123            $doc = substr($doc, $length);
124        }
125
126    }
127
128    /**
129     * The word count does not take into account
130     * words with non-words characters such as < =
131     * Therefore the node <node> and attribute name=value are not taken in the count
132     * @param $text
133     * @return int the number of words
134     */
135    public static function getWordCount($text)
136    {
137        /**
138         * Delete the frontmatter
139         */
140        $text = preg_replace("/^---(json)?$.*^---$/Ums", "", $text);
141        /**
142         * New line for node
143         */
144        $text = str_replace("<", "\n<", $text);
145        $text = str_replace(">", ">\n", $text);
146        // \s shorthand for whitespace
147        // | the table and links are separated with a |
148        // / to take into account expression such as and/or
149        // /u for unicode support (https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php)
150        $wordSeparator = '/[\s|\/]/u';
151        $preg_split = preg_split($wordSeparator, $text);
152        $wordsWithoutEmpty = array_filter($preg_split, 'self::isWord');
153        return count($wordsWithoutEmpty);
154    }
155
156    public static function normalize($expected)
157    {
158        $expected = preg_replace("/[\s]/", " ", $expected);
159        $expected = str_replace("  ", " ", $expected);
160        $expected = str_replace("  ", " ", $expected);
161        $expected = str_replace("  ", " ", $expected);
162        $expected = str_replace("  ", " ", $expected);
163        return trim($expected);
164
165    }
166
167    /**
168     * @param $text
169     * @return bool
170     */
171    public static function isWord($text)
172    {
173        if (empty($text)) {
174            return false;
175        }
176        /**
177         * We also allow `-` minus
178         *
179         * And because otherwise the words are not counted:
180         *   * `'` (used to highlight words)
181         *   * `[]` used in links
182         *   * `,` used at the end of a sentenct
183         */
184        $preg_match = preg_match("/^[\w\-'\]\[,]*$/u", $text);
185        return $preg_match == 1;
186    }
187
188    public static function match($subject, $pattern)
189    {
190        return preg_match("/$pattern/", $subject) === 1;
191    }
192
193    public static function endWiths($string, $suffix)
194    {
195        $suffixStartPosition = strlen($string) - strlen($suffix);
196        return strrpos($string, $suffix) === $suffixStartPosition;
197    }
198
199    public static function explodeAndTrim($string, $delimiter = ",")
200    {
201        return array_map('trim', explode($delimiter, $string));
202    }
203
204    public static function lastIndexOf($haystack, $needle)
205    {
206        /**
207         * strRpos
208         * and not strpos
209         */
210        return strrpos($haystack, $needle);
211    }
212
213    public static function startWiths($string, $prefix)
214    {
215        return strrpos($string, $prefix) === 0;
216    }
217
218}
219