StringHelpers::trimStrip()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Flynt\Utils;
4
5
class StringHelpers
6
{
7
    /**
8
     * Converts a string from camel case to kebap case.
9
     *
10
     * @since 0.1.0
11
     *
12
     * @param string $str The string to convert.
13
     *
14
     * @return string
15
     */
16
    public static function camelCaseToKebap($str)
17
    {
18
        return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $str));
19
    }
20
21
    /**
22
     * Strips all HTML tags including script and style,
23
     * and trims text to a certain number of words.
24
     *
25
     * @since 0.1.0
26
     *
27
     * @param string $str    The string to trim and strip.
28
     * @param number $length The string length to return.
29
     *
30
     * @return string
31
     */
32
    public static function trimStrip($str = '', $length = 25)
33
    {
34
        return wp_trim_words(wp_strip_all_tags($str), $length, '&hellip;');
0 ignored issues
show
Bug introduced by
It seems like $length can also be of type double; however, parameter $num_words of wp_trim_words() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

34
        return wp_trim_words(wp_strip_all_tags($str), /** @scrutinizer ignore-type */ $length, '&hellip;');
Loading history...
35
    }
36
37
    /**
38
     * Splits a camel case string.
39
     *
40
     * @since 0.1.0
41
     *
42
     * @param string $str The string to split.
43
     *
44
     * @return string
45
     */
46
    public static function splitCamelCase($str)
47
    {
48
        $a = preg_split(
49
            '/(^[^A-Z]+|[A-Z][^A-Z]+)/',
50
            $str,
51
            -1, // no limit for replacement count
52
            PREG_SPLIT_NO_EMPTY // don't return empty elements
53
            | PREG_SPLIT_DELIM_CAPTURE // don't strip anything from output array
54
        );
55
        return implode(' ', $a);
56
    }
57
58
    /**
59
     * Converts a string from kebap case to camel case.
60
     *
61
     * @since 0.1.0
62
     *
63
     * @param string $str                        The string to convert.
64
     * @param boolean $capitalizeFirstCharacter  Sets if the first character should be capitalized.
65
     *
66
     * @return string
67
     */
68
    public static function kebapCaseToCamelCase($str, $capitalizeFirstCharacter = false)
69
    {
70
        $str = str_replace(' ', '', ucwords(str_replace('-', ' ', $str)));
71
        if (false === $capitalizeFirstCharacter) {
72
            $str[0] = strtolower($str[0]);
73
        }
74
        return $str;
75
    }
76
77
    /**
78
     * Removes a prefix from a string.
79
     *
80
     * @since 0.1.0
81
     *
82
     * @param string $prefix The prefix to be removed.
83
     * @param string $str    The string to manipulate.
84
     *
85
     * @return string
86
     */
87
    public static function removePrefix($prefix, $str)
88
    {
89
        if (substr($str, 0, strlen($prefix)) == $prefix) {
90
            return substr($str, strlen($prefix));
91
        }
92
        return $str;
93
    }
94
95
    /**
96
     * Checks if a string starts with a certain string.
97
     *
98
     * @since 0.1.0
99
     *
100
     * @param string $search   The string to search for.
101
     * @param string $subject  The string to look into.
102
     *
103
     * @return boolean Returns true if the subject string starts with the search string.
104
     */
105
    public static function startsWith($search, $subject)
106
    {
107
        return substr($subject, 0, strlen($search)) === $search;
108
    }
109
110
    /**
111
     * Checks if a string ends with a certain string.
112
     *
113
     * @since 0.1.0
114
     *
115
     * @param string $search   The string to search for.
116
     * @param string $subject  The string to look into.
117
     *
118
     * @return boolean Returns true if the subject string ends with the search string.
119
     */
120
    public static function endsWith($search, $subject)
121
    {
122
        $searchLength = strlen($search);
123
        $subjectLength = strlen($subject);
124
        if ($searchLength > $subjectLength) {
125
            return false;
126
        }
127
        return substr_compare($subject, $search, $subjectLength - $searchLength, $searchLength) === 0;
128
    }
129
}
130