xref: /plugin/combo/ComboStrap/StringUtility.php (revision 1fa8c418ed5809db58049141be41b7738471dd32)
137748cd8SNickeau<?php
237748cd8SNickeau
337748cd8SNickeaunamespace ComboStrap;
437748cd8SNickeau
537748cd8SNickeau/**
637748cd8SNickeau * Class StringUtility
737748cd8SNickeau * @package ComboStrap
837748cd8SNickeau * A class with string utility
937748cd8SNickeau */
1037748cd8SNickeauclass StringUtility
1137748cd8SNickeau{
1237748cd8SNickeau
1337748cd8SNickeau
1437748cd8SNickeau    /**
1537748cd8SNickeau     * Generate a text with a max length of $length
1637748cd8SNickeau     * and add ... if above
1737748cd8SNickeau     * @param $myString
1837748cd8SNickeau     * @param $length
1937748cd8SNickeau     * @return string
2037748cd8SNickeau     */
21*1fa8c418SNickeau    static function truncateString($myString, $length): string
2237748cd8SNickeau    {
2337748cd8SNickeau
2437748cd8SNickeau        if (strlen($myString) > $length) {
2537748cd8SNickeau            $suffix = ' ...';
2637748cd8SNickeau            $myString = substr($myString, 0, ($length - 1) - strlen($suffix)) . $suffix;
2737748cd8SNickeau        }
2837748cd8SNickeau        return $myString;
2937748cd8SNickeau    }
3037748cd8SNickeau
3137748cd8SNickeau    /**
3237748cd8SNickeau     * @param $string
3337748cd8SNickeau     * @return string - the string without any carriage return
3437748cd8SNickeau     * Used to compare string without worrying about carriage return
3537748cd8SNickeau     */
3637748cd8SNickeau    public static function normalized($string)
3737748cd8SNickeau    {
3837748cd8SNickeau        return str_replace("\n", "", $string);
3937748cd8SNickeau    }
4037748cd8SNickeau
4137748cd8SNickeau    /**
4237748cd8SNickeau     * @param $needle
4337748cd8SNickeau     * @param $haystack
4437748cd8SNickeau     * @return bool
4537748cd8SNickeau     */
4637748cd8SNickeau    public static function contain($needle, $haystack)
4737748cd8SNickeau    {
4837748cd8SNickeau        $pos = strpos($haystack, $needle);
4937748cd8SNickeau        if ($pos === FALSE) {
5037748cd8SNickeau            return false;
5137748cd8SNickeau        } else {
5237748cd8SNickeau            return true;
5337748cd8SNickeau        }
5437748cd8SNickeau    }
5537748cd8SNickeau
5637748cd8SNickeau    public static function toString($value)
5737748cd8SNickeau    {
5837748cd8SNickeau        /**
5937748cd8SNickeau         * No transformation if it's a string
6037748cd8SNickeau         * var_export below is not idempotent
6137748cd8SNickeau         * ie \ would become \\
6237748cd8SNickeau         */
6337748cd8SNickeau        if(is_string($value)){
6437748cd8SNickeau            return $value;
6537748cd8SNickeau        }
6637748cd8SNickeau
6737748cd8SNickeau        $string = var_export($value, true);
6837748cd8SNickeau
6937748cd8SNickeau        // An array value gets command in var_export
7037748cd8SNickeau        $lastCharacterIndex = strlen($string) - 1;
7137748cd8SNickeau        if ($string[0] === "'" && $string[$lastCharacterIndex] === "'") {
7237748cd8SNickeau            $string = substr($string, 1, strlen($string) - 2);
7337748cd8SNickeau        }
7437748cd8SNickeau        return $string;
7537748cd8SNickeau
7637748cd8SNickeau    }
7737748cd8SNickeau
7837748cd8SNickeau    /**
7937748cd8SNickeau     * Add an EOL if not present at the end of the string
8037748cd8SNickeau     * @param $doc
8137748cd8SNickeau     */
8237748cd8SNickeau    public static function addEolCharacterIfNotPresent(&$doc)
8337748cd8SNickeau    {
8437748cd8SNickeau        if ($doc[strlen($doc) - 1] != DOKU_LF) {
8537748cd8SNickeau            $doc .= DOKU_LF;
8637748cd8SNickeau        }
8737748cd8SNickeau    }
8837748cd8SNickeau
8937748cd8SNickeau    /**
9037748cd8SNickeau     * Delete the string from the end
9137748cd8SNickeau     * This is used generally to delete the previous opening tag of an header or a blockquote
9237748cd8SNickeau     * @param $doc
9337748cd8SNickeau     * @param $string
9437748cd8SNickeau     */
9537748cd8SNickeau    public static function rtrim(&$doc, $string)
9637748cd8SNickeau    {
9737748cd8SNickeau
9837748cd8SNickeau        /**
9937748cd8SNickeau         * We trim because in the process, we may get extra {@link DOKU_LF} at the end
10037748cd8SNickeau         */
10137748cd8SNickeau        $doc = trim($doc);
10237748cd8SNickeau        $string = trim($string);
10337748cd8SNickeau        $length = strlen($doc) - strlen($string);
10437748cd8SNickeau        if (substr($doc, $length) === $string) {
10537748cd8SNickeau            $doc = substr($doc, 0, $length);
10637748cd8SNickeau        }
10737748cd8SNickeau
10837748cd8SNickeau    }
10937748cd8SNickeau
11037748cd8SNickeau    /**
11137748cd8SNickeau     * Delete the string from the beginning
11237748cd8SNickeau     * This is used to delete a tag for instance
11337748cd8SNickeau     * @param $doc
11437748cd8SNickeau     * @param $string
11537748cd8SNickeau     */
11637748cd8SNickeau    public static function ltrim(&$doc, $string)
11737748cd8SNickeau    {
11837748cd8SNickeau
11937748cd8SNickeau        $doc = trim($doc);
12037748cd8SNickeau        $string = trim($string);
12137748cd8SNickeau        $length = strlen($string);
12237748cd8SNickeau        if (substr($doc, 0, $length) === $string) {
12337748cd8SNickeau            $doc = substr($doc, $length);
12437748cd8SNickeau        }
12537748cd8SNickeau
12637748cd8SNickeau    }
12737748cd8SNickeau
12837748cd8SNickeau    /**
12937748cd8SNickeau     * The word count does not take into account
13037748cd8SNickeau     * words with non-words characters such as < =
13137748cd8SNickeau     * Therefore the node <node> and attribute name=value are not taken in the count
13237748cd8SNickeau     * @param $text
13337748cd8SNickeau     * @return int the number of words
13437748cd8SNickeau     */
13537748cd8SNickeau    public static function getWordCount($text)
13637748cd8SNickeau    {
13737748cd8SNickeau        /**
13837748cd8SNickeau         * Delete the frontmatter
13937748cd8SNickeau         */
14037748cd8SNickeau        $text = preg_replace("/^---(json)?$.*^---$/Ums", "", $text);
14137748cd8SNickeau        /**
14237748cd8SNickeau         * New line for node
14337748cd8SNickeau         */
14437748cd8SNickeau        $text = str_replace("<", "\n<", $text);
14537748cd8SNickeau        $text = str_replace(">", ">\n", $text);
14637748cd8SNickeau        // \s shorthand for whitespace
14737748cd8SNickeau        // | the table and links are separated with a |
14837748cd8SNickeau        // / to take into account expression such as and/or
14937748cd8SNickeau        // /u for unicode support (https://www.php.net/manual/en/reference.pcre.pattern.modifiers.php)
15037748cd8SNickeau        $wordSeparator = '/[\s|\/]/u';
15137748cd8SNickeau        $preg_split = preg_split($wordSeparator, $text);
15237748cd8SNickeau        $wordsWithoutEmpty = array_filter($preg_split, 'self::isWord');
15337748cd8SNickeau        return count($wordsWithoutEmpty);
15437748cd8SNickeau    }
15537748cd8SNickeau
15637748cd8SNickeau    public static function normalize($expected)
15737748cd8SNickeau    {
15837748cd8SNickeau        $expected = preg_replace("/[\s]/", " ", $expected);
15937748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
16037748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
16137748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
16237748cd8SNickeau        $expected = str_replace("  ", " ", $expected);
16337748cd8SNickeau        return trim($expected);
16437748cd8SNickeau
16537748cd8SNickeau    }
16637748cd8SNickeau
16737748cd8SNickeau    /**
16837748cd8SNickeau     * @param $text
16937748cd8SNickeau     * @return bool
17037748cd8SNickeau     */
17137748cd8SNickeau    public static function isWord($text)
17237748cd8SNickeau    {
17337748cd8SNickeau        if (empty($text)) {
17437748cd8SNickeau            return false;
17537748cd8SNickeau        }
17637748cd8SNickeau        /**
17737748cd8SNickeau         * We also allow `-` minus
17837748cd8SNickeau         *
17937748cd8SNickeau         * And because otherwise the words are not counted:
18037748cd8SNickeau         *   * `'` (used to highlight words)
18137748cd8SNickeau         *   * `[]` used in links
18237748cd8SNickeau         *   * `,` used at the end of a sentenct
18337748cd8SNickeau         */
18437748cd8SNickeau        $preg_match = preg_match("/^[\w\-'\]\[,]*$/u", $text);
18537748cd8SNickeau        return $preg_match == 1;
18637748cd8SNickeau    }
18737748cd8SNickeau
18837748cd8SNickeau    public static function match($subject, $pattern)
18937748cd8SNickeau    {
19037748cd8SNickeau        return preg_match("/$pattern/", $subject) === 1;
19137748cd8SNickeau    }
19237748cd8SNickeau
19337748cd8SNickeau    public static function endWiths($string, $suffix)
19437748cd8SNickeau    {
19537748cd8SNickeau        $suffixStartPosition = strlen($string) - strlen($suffix);
19637748cd8SNickeau        return strrpos($string, $suffix) === $suffixStartPosition;
19737748cd8SNickeau    }
19837748cd8SNickeau
19937748cd8SNickeau    public static function explodeAndTrim($string, $delimiter = ",")
20037748cd8SNickeau    {
20137748cd8SNickeau        return array_map('trim', explode($delimiter, $string));
20237748cd8SNickeau    }
20337748cd8SNickeau
20437748cd8SNickeau    public static function lastIndexOf($haystack, $needle)
20537748cd8SNickeau    {
20637748cd8SNickeau        /**
20737748cd8SNickeau         * strRpos
20837748cd8SNickeau         * and not strpos
20937748cd8SNickeau         */
21037748cd8SNickeau        return strrpos($haystack, $needle);
21137748cd8SNickeau    }
21237748cd8SNickeau
21337748cd8SNickeau    public static function startWiths($string, $prefix)
21437748cd8SNickeau    {
21537748cd8SNickeau        return strrpos($string, $prefix) === 0;
21637748cd8SNickeau    }
21737748cd8SNickeau
21837748cd8SNickeau}
219