Completed
Push — master ( f62c32...7b8f47 )
by Zeeshan
03:29 queued 01:46
created

Strings::toLower()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace __\Traits;
4
5
use __;
6
7
trait Strings
8
{
9
    /**
10
     * Split a string by string.
11
     *
12
     * Based on explode, see http://php.net/manual/en/function.explode.php.
13
     *
14
     * __::split('a-b-c', '-', 2);
15
     *      >> ['a', 'b-c']
16
     *
17
     * @param string $input     The string to split.
18
     * @param string $delimiter The boundary string.
19
     * @param int    $limit     (optional) If limit is set and positive, the returned array
20
     *                          will contain a maximum of limit elements with the last element containing the
21
     *                          rest of string.
22
     *                          If the limit parameter is negative, all components except the last -limit are returned.
23
     *                          If the limit parameter is zero, then this is treated as 1.
24
     *
25
     * @return string
26
     */
27 18
    public static function split($input, $delimiter, $limit = PHP_INT_MAX)
28
    {
29 18
        return explode($delimiter, $input, $limit);
0 ignored issues
show
Bug Best Practice introduced by
The expression return explode($delimiter, $input, $limit) returns the type array which is incompatible with the documented return type string.
Loading history...
30
    }
31
32
    /**
33
     * Converts string to [camel case](https://en.wikipedia.org/wiki/CamelCase).
34
     *
35
     * __::camelCase('Foo Bar');
36
     *      >> 'fooBar'
37
     *
38
     * @param string $input
39
     *
40
     * @return string
41
     *
42
     */
43 1
    public static function camelCase($input)
44
    {
45 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
46
47 1
        return array_reduce(
48 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

48
            /** @scrutinizer ignore-type */ $words,
Loading history...
49 1
            function ($result, $word) use ($words) {
50 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

50
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
51 1
                $word    = __::toLower($word);
52
53 1
                return $result . (!$isFirst ? __::capitalize($word) : $word);
54 1
            },
55 1
            ''
56
        );
57
    }
58
59
    /**
60
     * Converts the first character of string to upper case and the remaining to lower case.
61
     *
62
     * __::capitalize('FRED');
63
     *      >> 'Fred'
64
     *
65
     * @param string $input
66
     *
67
     * @return string
68
     *
69
     */
70 2
    public static function capitalize($input)
71
    {
72 2
        return __::upperFirst(__::toLower($input));
73
    }
74
75
    /**
76
     * Converts string to
77
     * [kebab case](https://en.wikipedia.org/wiki/Letter_case#Special_case_styles).
78
     *
79
     * __::kebabCase('Foo Bar');
80
     *      >> 'foo-bar'
81
     *
82
     * @param string $input
83
     *
84
     * @return string
85
     *
86
     */
87 1
    public static function kebabCase($input)
88
    {
89 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
90
91 1
        return array_reduce(
92 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

92
            /** @scrutinizer ignore-type */ $words,
Loading history...
93 1
            function ($result, $word) use ($words) {
94 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

94
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
95
96 1
                return $result . (!$isFirst ? '-' : '') . __::toLower($word);
97 1
            },
98 1
            ''
99
        );
100
    }
101
102
    /**
103
     * Converts the first character of string to lower case, like lcfirst.
104
     *
105
     * __::lowerFirst('Fred');
106
     *      >> 'fred'
107
     *
108
     * @param string $input
109
     *
110
     * @return string
111
     *
112
     */
113 1
    public static function lowerFirst($input)
114
    {
115 1
        return \lcfirst($input);
116
    }
117
118
    /**
119
     * Converts string to
120
     * [snake case](https://en.wikipedia.org/wiki/Snake_case).
121
     *
122
     * __::snakeCase('Foo Bar');
123
     *      >> 'foo_bar'
124
     *
125
     * @param string $input
126
     *
127
     * @return string
128
     *
129
     */
130 1
    public static function snakeCase($input)
131
    {
132 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
133
134 1
        return array_reduce(
135 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

135
            /** @scrutinizer ignore-type */ $words,
Loading history...
136 1
            function ($result, $word) use ($words) {
137 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

137
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
138
139 1
                return $result . (!$isFirst ? '_' : '') . __::toLower($word);
140 1
            },
141 1
            ''
142
        );
143
    }
144
145
    /**
146
     * Converts string to [start case](https://en.wikipedia.org/wiki/Letter_case#Stylistic_or_specialised_usage).
147
     *
148
     * __::startCase('--foo-bar--');
149
     *      >> 'Foo Bar'
150
     *
151
     * @param string $input
152
     *
153
     * @return string
154
     *
155
     */
156 1
    public static function startCase($input)
157
    {
158 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
159
160 1
        return array_reduce(
161 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

161
            /** @scrutinizer ignore-type */ $words,
Loading history...
162 1
            function ($result, $word) use ($words) {
163 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

163
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
164
165 1
                return $result . (!$isFirst ? ' ' : '') . __::upperFirst($word);
166 1
            },
167 1
            ''
168
        );
169
    }
170
171
    /**
172
     * Converts string, as a whole, to lower case just like strtolower.
173
     *
174
     * __::toLower('fooBar');
175
     *      >> 'foobar'
176
     *
177
     * @param string $input
178
     *
179
     * @return string
180
     *
181
     */
182 6
    public static function toLower($input)
183
    {
184 6
        return \strtolower($input);
185
    }
186
187
    /**
188
     * Converts string, as a whole, to lower case just like strtoupper.
189
     *
190
     * __::toUpper('fooBar');
191
     *      >> 'FOOBAR'
192
     *
193
     * @param string $input
194
     *
195
     * @return string
196
     *
197
     */
198 2
    public static function toUpper($input)
199
    {
200 2
        return \strtoupper($input);
201
    }
202
203
    /**
204
     * Converts string, as space separated words, to upper case.
205
     *
206
     * __::upperCase('--foo-bar');
207
     *      >> 'FOO BAR'
208
     *
209
     * @param string $input
210
     *
211
     * @return string
212
     *
213
     */
214 1
    public static function upperCase($input)
215
    {
216 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
217
218 1
        return array_reduce(
219 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

219
            /** @scrutinizer ignore-type */ $words,
Loading history...
220 1
            function ($result, $word) use ($words) {
221 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

221
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
222
223 1
                return $result . (!$isFirst ? ' ' : '') . __::toUpper($word);
224 1
            },
225 1
            ''
226
        );
227
    }
228
229
    /**
230
     * Converts the first character of string to upper case, like ucfirst.
231
     *
232
     * __::upperFirst('fred');
233
     *      >> 'Fred'
234
     *
235
     * @param string $input
236
     *
237
     * @return string
238
     *
239
     */
240 4
    public static function upperFirst($input)
241
    {
242 4
        return \ucfirst($input);
243
    }
244
245
    /**
246
     * Splits string into an array of its words.
247
     *
248
     * __::words('fred, barney, & pebbles');
249
     *      >> ['fred', 'barney', 'pebbles']
250
     *
251
     * __::words('fred, barney, & pebbles', '/[^, ]+/');
252
     *      >> ['fred', 'barney', '&', 'pebbles']
253
     *
254
     * @param string $input
255
     * @param string $pattern : The pattern to match words.
256
     *
257
     * @return string
258
     *
259
     */
260 7
    public static function words($input, $pattern = null)
261
    {
262
        /** Used to compose unicode character classes. */
263 7
        $rsAstralRange         = '\x{e800}-\x{efff}';
264 7
        $rsComboMarksRange     = '\x{0300}-\x{036f}';
265 7
        $reComboHalfMarksRange = '\x{fe20}-\x{fe2f}';
266 7
        $rsComboSymbolsRange   = '\x{20d0}-\x{20ff}';
267 7
        $rsComboRange          = $rsComboMarksRange . $reComboHalfMarksRange . $rsComboSymbolsRange;
268 7
        $rsDingbatRange        = '\x{2700}-\x{27bf}';
269 7
        $rsLowerRange          = 'a-z\\xdf-\\xf6\\xf8-\\xff';
270 7
        $rsMathOpRange         = '\\xac\\xb1\\xd7\\xf7';
271 7
        $rsNonCharRange        = '\\x00-\\x2f\\x3a-\\x40\\x5b-\\x60\\x7b-\\xbf';
272 7
        $rsPunctuationRange    = '\x{2000}-\x{206f}';
273 7
        $rsSpaceRange          = ' \\t\\x0b\\f\\xa0\x{feff}\\n\\r\x{2028}\x{2029}\x{1680}\x{180e}\x{2000}\x{2001}\x{2002}\x{2003}\x{2004}\x{2005}\x{2006}\x{2007}\x{2008}\x{2009}\x{200a}\x{202f}\x{205f}\x{3000}';
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 140 characters; contains 211 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
274 7
        $rsUpperRange          = 'A-Z\\xc0-\\xd6\\xd8-\\xde';
275 7
        $rsVarRange            = '\x{fe0e}\x{fe0f}';
276 7
        $rsBreakRange          = $rsMathOpRange . $rsNonCharRange . $rsPunctuationRange . $rsSpaceRange;
277
        /** Used to compose unicode capture groups. */
278 7
        $rsApos      = "['\x{2019}]";
279 7
        $rsBreak     = '[' . $rsBreakRange . ']';
280 7
        $rsCombo     = '[' . $rsComboRange . ']';
281 7
        $rsDigits    = '\\d+';
282 7
        $rsDingbat   = '[' . $rsDingbatRange . ']';
283 7
        $rsLower     = '[' . $rsLowerRange . ']';
284 7
        $rsMisc      = '[^' . $rsAstralRange . $rsBreakRange . $rsDigits . $rsDingbatRange . $rsLowerRange . $rsUpperRange . ']';
285 7
        $rsFitz      = '\\x{e83c}[\x{effb}-\x{efff}]';
286 7
        $rsModifier  = '(?:' . $rsCombo . '|' . $rsFitz . ')';
287 7
        $rsNonAstral = '[^' . $rsAstralRange . ']';
288 7
        $rsRegional  = '(?:\x{e83c}[\x{ede6}-\x{edff}]){2}';
289 7
        $rsSurrPair  = '[\x{e800}-\x{ebff}][\x{ec00}-\x{efff}]';
290 7
        $rsUpper     = '[' . $rsUpperRange . ']';
291 7
        $rsZWJ       = '\x{200d}';
292
        /** Used to compose unicode regexes. */
293 7
        $rsMiscLower         = '(?:' . $rsLower . '|' . $rsMisc . ')';
294 7
        $rsMiscUpper         = '(?:' . $rsUpper . '|' . $rsMisc . ')';
295 7
        $rsOptContrLower     = '(?:' . $rsApos . '(?:d|ll|m|re|s|t|ve))?';
296 7
        $rsOptContrUpper     = '(?:' . $rsApos . '(?:D|LL|M|RE|S|T|VE))?';
297 7
        $reOptMod            = $rsModifier . '?';
298 7
        $rsOptVar            = '[' . $rsVarRange . ']?';
299 7
        $rsOrdLower          = '\\d*(?:(?:1st|2nd|3rd|(?![123])\\dth)\\b)';
300 7
        $rsOrdUpper          = '\\d*(?:(?:1ST|2ND|3RD|(?![123])\\dTH)\\b)';
301 7
        $asciiWords          = '/[^\x00-\x2f\x3a-\x40\x5b-\x60\x7b-\x7f]+/';
302 7
        $hasUnicodeWordRegex = '/[a-z][A-Z]|[A-Z]{2,}[a-z]|[0-9][a-zA-Z]|[a-zA-Z][0-9]|[^a-zA-Z0-9 ]/';
303 7
        $rsOptJoin           = '(?:' . $rsZWJ . '(?:' . join('|',
304 7
                [$rsNonAstral, $rsRegional, $rsSurrPair]) . ')' . $rsOptVar . $reOptMod . ')*';
305 7
        $rsSeq               = $rsOptVar . $reOptMod . $rsOptJoin;
306 7
        $rsEmoji             = '(?:' . join('|', [$rsDingbat, $rsRegional, $rsSurrPair]) . ')' . $rsSeq;
307
308
        /**
309
         * Splits a Unicode `string` into an array of its words.
310
         *
311
         * @private
312
         *
313
         * @param {string} The string to inspect.
314
         * @returns {Array} Returns the words of `string`.
315
         */
316 7
        $unicodeWords = '/' . join('|', [
317 7
                $rsUpper . '?' . $rsLower . '+' . $rsOptContrLower . '(?=' . join('|',
318 7
                    [$rsBreak, $rsUpper, '$']) . ')',
319 7
                $rsMiscUpper . '+' . $rsOptContrUpper . '(?=' . join('|',
320 7
                    [$rsBreak, $rsUpper . $rsMiscLower, '$']) . ')',
321 7
                $rsUpper . '?' . $rsMiscLower . '+' . $rsOptContrLower,
322 7
                $rsUpper . '+' . $rsOptContrUpper,
323 7
                $rsOrdUpper,
324 7
                $rsOrdLower,
325 7
                $rsDigits,
326 7
                $rsEmoji,
327 7
            ]) . '/u';
328 7
        if ($pattern === null) {
329 7
            $hasUnicodeWord = \preg_match($hasUnicodeWordRegex, $input);
330 7
            $pattern        = $hasUnicodeWord ? $unicodeWords : $asciiWords;
331
        }
332 7
        $r = \preg_match_all($pattern, $input, $matches, PREG_PATTERN_ORDER);
333 7
        if ($r === false) {
0 ignored issues
show
introduced by
The condition $r === false can never be true.
Loading history...
334
            throw new RuntimeException('Regex exception');
0 ignored issues
show
Bug introduced by
The type __\Traits\RuntimeException was not found. Did you mean RuntimeException? If so, make sure to prefix the type with \.
Loading history...
335
        }
336
337 7
        return \count($matches[0]) > 0 ? $matches[0] : [];
0 ignored issues
show
Bug Best Practice introduced by
The expression return count($matches[0]...? $matches[0] : array() also could return the type array which is incompatible with the documented return type string.
Loading history...
338
    }
339
340
    /**
341
     * Converts string, as space separated words, to lower case.
342
     *
343
     * __::lowerCase('--Foo-Bar--');
344
     *      >> 'foo bar'
345
     *
346
     * @param string $input
347
     *
348
     * @return string
349
     *
350
     */
351 1
    public static function lowerCase($input)
352
    {
353 1
        $words = __::words(\preg_replace("/['\x{2019}]/u", '', $input));
354
355 1
        return array_reduce(
356 1
            $words,
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $input of array_reduce(). ( Ignorable by Annotation )

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

356
            /** @scrutinizer ignore-type */ $words,
Loading history...
357 1
            function ($result, $word) use ($words) {
358 1
                $isFirst = __::first($words) === $word;
0 ignored issues
show
Bug introduced by
$words of type string is incompatible with the type array expected by parameter $array of __::first(). ( Ignorable by Annotation )

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

358
                $isFirst = __::first(/** @scrutinizer ignore-type */ $words) === $word;
Loading history...
359
360 1
                return $result . (!$isFirst ? ' ' : '') . __::toLower($word);
361 1
            },
362 1
            ''
363
        );
364
    }
365
}