Str   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 0
cbo 0
dl 0
loc 190
ccs 0
cts 66
cp 0
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A camelize() 0 12 2
A endsWith() 0 4 1
A firstStringBetween() 0 4 1
A includes() 0 4 1
A isAnagram() 0 4 1
A isLowerCase() 0 4 1
A isUpperCase() 0 4 1
A palindrome() 0 4 1
A startsWith() 0 4 1
A countVowels() 0 6 1
A decapitalize() 0 4 2
A substringByWord() 0 8 3
A letterPlusNumber() 0 8 2
1
<?php
2
3
namespace Baka\Support;
4
5
class Str
6
{
7
    /**
8
     * Converts the underscore_notation to the UpperCamelCase
9
     *
10
     * @param string $string
11
     * @param string $delimiter
12
     *
13
     * @return string
14
     */
15
    public static function camelize(string $string, string $delimiter = '_'): string
16
    {
17
        $delimiterArray = str_split($delimiter);
18
        foreach ($delimiterArray as $delimiter) {
19
            $stringParts = explode($delimiter, $string);
20
            $stringParts = array_map('strtolower', $stringParts);
21
            $stringParts = array_map('ucfirst', $stringParts);
22
            $string = implode('', $stringParts);
23
        }
24
25
        return $string;
26
    }
27
28
    /**
29
     * Check if a string is ends with a given substring.
30
     * public static function endsWith($haystack, $needle)
31
     *
32
     * @param string $haystack
33
     * @param string $needle
34
     *
35
     * @return bool
36
     */
37
    public static function endsWith(string $haystack, string $needle): bool
38
    {
39
        return mb_substr($haystack, -mb_strlen($needle)) === $needle;
40
    }
41
42
    /**
43
     * Returns the first string there is between the strings from the parameter start and end.
44
     *
45
     * @param string $haystack
46
     * @param string $start
47
     * @param string $end
48
     *
49
     * @return string
50
     */
51
    public static function firstStringBetween(string $haystack, string $start, string $end): string
52
    {
53
        return trim(mb_strstr(mb_strstr($haystack, $start), $end, true), $start . $end);
54
    }
55
56
    /**
57
     * Lets you determine whether or not a string includes another string.
58
     *
59
     * @param string $needle
60
     * @param string $haystack
61
     *
62
     * @return bool
63
     */
64
    public static function includes(string $needle, string $haystack): bool
65
    {
66
        return false !== mb_strpos($haystack, $needle);
67
    }
68
69
    /**
70
     * Compare two strings and returns true if both strings are anagram, false otherwise.
71
     *
72
     * @param string $string1
73
     * @param string $string2
74
     *
75
     * @return bool
76
     */
77
    public static function isAnagram(string $string1, string $string2): bool
78
    {
79
        return count_chars($string1, 1) === count_chars($string2, 1);
80
    }
81
82
    /**
83
     * Returns true if the given string is lower case, false otherwise.
84
     *
85
     * @param string $string
86
     *
87
     * @return bool
88
     */
89
    public static function isLowerCase(string $string): bool
90
    {
91
        return $string === mb_strtolower($string);
92
    }
93
94
    /**
95
     * Returns true if the given string is upper case, false otherwise.
96
     *
97
     * @param string $string
98
     *
99
     * @return bool
100
     */
101
    public static function isUpperCase(string $string): bool
102
    {
103
        return $string === mb_strtoupper($string);
104
    }
105
106
    /**
107
     * Returns true if the given string is a palindrome, false otherwise.
108
     *
109
     * @param string $string
110
     *
111
     * @return bool
112
     */
113
    public static function palindrome(string $string): bool
114
    {
115
        return strrev($string) === $string;
116
    }
117
118
    /**
119
     *  Check if a string is starts with a given substring.
120
     *
121
     * @param string $haystack
122
     * @param string $needle
123
     *
124
     * @return bool
125
     */
126
    public static function startsWith(string $haystack, string $needle): bool
127
    {
128
        return mb_substr($haystack, 0, mb_strlen($needle)) === $needle;
129
    }
130
131
    /**
132
     * Retuns number of vowels in provided string.
133
     * Use a regular expression to count the number of vowels (A, E, I, O, U) in a string.
134
     *
135
     * @param string $string
136
     *
137
     * @return int
138
     */
139
    public static function countVowels(string $string): int
140
    {
141
        preg_match_all('/[aeiou]/i', $string, $matches);
142
143
        return \count($matches[0]);
144
    }
145
146
    /**
147
     * Decapitalizes the first letter of the sring and then adds it with rest of the string. Omit the upperRest parameter to keep the
148
     * rest of the string intact, or set it to true to convert to uppercase.
149
     *
150
     * @param string $string
151
     * @param bool $upperRest
152
     *
153
     * @return string
154
     */
155
    public static function decapitalize(string $string, bool $upperRest = false): string
156
    {
157
        return mb_strtolower(mb_substr($string, 0, 1)) . ($upperRest ? mb_strtoupper(mb_substr($string, 1)) : mb_substr($string, 1));
158
    }
159
160
    /**
161
     *  Substring a string to specific lenght, but removing whole words
162
     *
163
     * @param string $string
164
     * @param int $to
165
     *
166
     * @return string
167
     */
168
    public static function substringByWord(string $string, int $to): string
169
    {
170
        if (mb_strlen($string) > $to && preg_match("/^.{1,$to}\b/s", $string, $matches)) {
171
            $string = $matches[0];
172
        }
173
174
        return mb_substr($string, 0, $to);
175
    }
176
177
    /**
178
     * Convert a number to an excel column letter
179
     * EJ: 'A'+22 = W;
180
     *
181
     * @param string $letter Initial Letter
182
     * @param int $number Number of letters to increase
183
     *
184
     * @return string
185
     */
186
    public static function letterPlusNumber(string $letter, int $number): string
187
    {
188
        for ($i = 0; $i < $number; ++$i) {
189
            ++$letter;
190
        }
191
192
        return (string) $letter;
193
    }
194
}
195