GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

To::toUpperCase()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
1
<?php namespace Gears\String\Methods;
2
////////////////////////////////////////////////////////////////////////////////
3
// __________ __             ________                   __________
4
// \______   \  |__ ______  /  _____/  ____ _____ ______\______   \ _______  ___
5
//  |     ___/  |  \\____ \/   \  ____/ __ \\__  \\_  __ \    |  _//  _ \  \/  /
6
//  |    |   |   Y  \  |_> >    \_\  \  ___/ / __ \|  | \/    |   (  <_> >    <
7
//  |____|   |___|  /   __/ \______  /\___  >____  /__|  |______  /\____/__/\_ \
8
//                \/|__|           \/     \/     \/             \/            \/
9
// -----------------------------------------------------------------------------
10
//          Designed and Developed by Brad Jones <brad @="bjc.id.au" />
11
// -----------------------------------------------------------------------------
12
////////////////////////////////////////////////////////////////////////////////
13
14
use voku\helper\UTF8;
15
16
trait To
17
{
18
    /**
19
     * Explicitly turn Str back into a scalar string.
20
     *
21
     * @return string
22
     */
23
    public function toString()
24
    {
25
        return $this->__toString();
26
    }
27
28
    /**
29
     * Converts the string to an array of characters.
30
     *
31
     * Each character is an instance of Str.
32
     *
33
     * @return static[]
34
     */
35
    public function toArray()
36
    {
37
        return $this->getIterator()->getArrayCopy();
38
    }
39
40
    /**
41
     * Converts all characters in the string to lowercase.
42
     *
43
     * @return static
44
     */
45
    public function toLowerCase()
46
    {
47
        return $this->newSelf
48
        (
49
            UTF8::strtolower($this->scalarString, $this->encoding)
50
        );
51
    }
52
53
    /**
54
     * Converts all characters in the string to lowercase.
55
     *
56
     * @return static
57
     */
58
    public function toUpperCase()
59
    {
60
        return $this->newSelf
61
        (
62
            UTF8::strtoupper($this->scalarString, $this->encoding)
63
        );
64
    }
65
66
    /**
67
     * Returns the singular version of the word.
68
     *
69
     * @param  string $language The language for the inflector.
70
     *
71
     * @return static
72
     */
73 View Code Duplication
    public function toSingular($language = 'en')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
    {
75
        if (!class_exists('\\ICanBoogie\\Inflector'))
76
        {
77
            throw new \RuntimeException
78
            (
79
                "This method requires ICanBoogie\Inflector. ".
80
                "Install with: composer require icanboogie/inflector"
81
            );
82
        }
83
84
        return $this->newSelf
85
        (
86
            \ICanBoogie\Inflector::get($language)
87
            ->singularize($this->scalarString)
88
        );
89
    }
90
91
    /**
92
     * Returns the plural version of the word.
93
     *
94
     * @param  string $language The language for the inflector.
95
     *
96
     * @return static
97
     */
98 View Code Duplication
    public function toPlural($language = 'en')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
99
    {
100
        if (!class_exists('\\ICanBoogie\\Inflector'))
101
        {
102
            throw new \RuntimeException
103
            (
104
                "This method requires ICanBoogie\Inflector. ".
105
                "Install with: composer require icanboogie/inflector"
106
            );
107
        }
108
109
        return $this->newSelf
110
        (
111
            \ICanBoogie\Inflector::get($language)
112
            ->pluralize($this->scalarString)
113
        );
114
    }
115
116
    /**
117
     * Returns an ASCII version of the string.
118
     *
119
     * A set of non-ASCII characters are replaced with their closest ASCII
120
     * counterparts, and the rest are removed unless instructed otherwise.
121
     *
122
     * @return static
123
     */
124
    public function toAscii()
125
    {
126
        return $this->newSelf
127
        (
128
            UTF8::toAscii($this->scalarString)
0 ignored issues
show
Deprecated Code introduced by
The method voku\helper\UTF8::toAscii() has been deprecated.

This method has been deprecated.

Loading history...
129
        );
130
    }
131
132
    /**
133
     * Returns a boolean representation of the given logical string value.
134
     *
135
     * For example, 'true', '1', 'on' and 'yes' will return true. 'false', '0',
136
     * 'off', and 'no' will return false. In all instances, case is ignored.
137
     * For other numeric strings, their sign will determine the return value.
138
     * In addition, blank strings consisting of only whitespace will return
139
     * false. For all other strings, the return value is a result of a
140
     * boolean cast.
141
     *
142
     * @return bool
143
     */
144
    public function toBoolean()
145
    {
146
        $key = $this->toLowerCase()->scalarString;
147
148
        $map =
149
        [
150
            'true'  => true,
151
            '1'     => true,
152
            'on'    => true,
153
            'yes'   => true,
154
            'false' => false,
155
            '0'     => false,
156
            'off'   => false,
157
            'no'    => false
158
        ];
159
160
        if (array_key_exists($key, $map))
161
        {
162
            return $map[$key];
163
        }
164
        elseif (is_numeric($this->scalarString))
165
        {
166
            return ((int)$this->scalarString > 0);
167
        }
168
        else
169
        {
170
            return (bool)$this->regexReplace('[[:space:]]', '')->scalarString;
171
        }
172
    }
173
174
    /**
175
     * Converts tabs to spaces.
176
     *
177
     * Each tab in the string is replaced with some number of spaces,
178
     * as defined by $tabLength. By default, each tab is converted to
179
     * 4 consecutive spaces.
180
     *
181
     * @param  int    $tabLength Number of spaces to replace each tab with.
182
     *
183
     * @return static
184
     */
185
    public function toSpaces($tabLength = 4)
186
    {
187
        return $this->newSelf
188
        (
189
            UTF8::str_replace
190
            (
191
                "\t",
192
                UTF8::str_repeat(' ', $tabLength),
193
                $this->scalarString
194
            )
195
        );
196
    }
197
198
    /**
199
     * Converts spaces to tabs.
200
     *
201
     * Replaces each occurrence of some consecutive number of spaces,
202
     * as defined by $tabLength, to a tab. By default, each 4 consecutive
203
     * spaces are converted to a tab.
204
     *
205
     * @param  int    $tabLength Number of spaces to replace with a tab.
206
     *
207
     * @return static
208
     */
209
    public function toTabs($tabLength = 4)
210
    {
211
        return $this->newSelf
212
        (
213
            UTF8::str_replace
214
            (
215
                UTF8::str_repeat(' ', $tabLength),
216
                "\t",
217
                $this->scalarString
218
            )
219
        );
220
    }
221
222
    /**
223
     * Returns a lowercase and trimmed string separated by dashes.
224
     *
225
     * Dashes are inserted before uppercase characters (with the exception of
226
     * the first character of the string), and in place of spaces as well as
227
     * underscores.
228
     *
229
     * @return static
230
     */
231
    public function toDashed()
232
    {
233
        return $this->delimit('-');
234
    }
235
236
    /**
237
     * Returns a lowercase and trimmed string separated by underscores.
238
     *
239
     * Underscores are inserted before uppercase characters (with the exception
240
     * of the first character of the string), and in place of spaces as well as
241
     * dashes.
242
     *
243
     * @return static
244
     */
245
    public function toUnderScored()
246
    {
247
        return $this->delimit('_');
248
    }
249
250
    /**
251
     * Returns a camelCase version of the string.
252
     *
253
     * Trims surrounding spaces, capitalizes letters following digits, spaces,
254
     * dashes & underscores and removes spaces & dashes as well as underscores.
255
     *
256
     * @param  bool    $upperFirst If true, the first char will be UPPERCASE.
257
     *
258
     * @return static
259
     */
260
    public function toCamelCase($upperFirst = false)
261
    {
262
        $camelCase = $this->trim()->lowerCaseFirst();
263
264
        $camelCase = preg_replace('/^[-_]+/', '', (string)$camelCase);
265
266
        $camelCase = preg_replace_callback
267
        (
268
            '/[-_\s]+(.)?/u',
269
            function ($match)
270
            {
271
                if (isset($match[1]))
272
                {
273
                    return UTF8::strtoupper($match[1], $this->encoding);
274
                }
275
                else
276
                {
277
                    return '';
278
                }
279
            },
280
            $camelCase
281
        );
282
283
        $camelCase = preg_replace_callback
284
        (
285
            '/[\d]+(.)?/u',
286
            function ($match)
287
            {
288
                return UTF8::strtoupper($match[0], $this->encoding);
289
            },
290
            $camelCase
291
        );
292
293
        $camelCase = $this->newSelf($camelCase);
294
295
        if ($upperFirst === true) $camelCase = $camelCase->upperCaseFirst();
296
297
        return $camelCase;
298
    }
299
300
    /**
301
     * Convert a string to e.g.: "snake_case"
302
     *
303
     * @return static
304
     */
305
    public function toSnakeCase()
306
    {
307
        $snake = UTF8::normalize_whitespace($this->scalarString);
308
309
        $snake = str_replace('-', '_', $snake);
310
311
        $snake = preg_replace_callback
312
        (
313
            '/([\d|A-Z])/u',
314
            function ($matches)
315
            {
316
                $match = $matches[1];
317
                $matchInt = (int)$match;
318
319
                if ("$matchInt" == $match)
320
                {
321
                    return '_' . $match . '_';
322
                }
323
                else
324
                {
325
                    return '_' . UTF8::strtolower($match, $this->encoding);
326
                }
327
            },
328
            $snake
329
        );
330
331
        $snake = preg_replace
332
        (
333
            [
334
                '/\s+/',        // convert spaces to "_"
335
                '/^\s+|\s+$/',  // trim leading & trailing spaces
336
                '/_+/'          // remove double "_"
337
            ],
338
            [
339
                '_',
340
                '',
341
                '_'
342
            ],
343
            $snake
344
        );
345
346
        // trim leading & trailing "_"
347
        $snake = UTF8::trim($snake, '_');
348
349
        // trim leading & trailing whitespace
350
        $snake = UTF8::trim($snake);
351
352
        return $this->newSelf($snake);
353
    }
354
355
    /**
356
     * Returns a trimmed string with the first letter of each word capitalized.
357
     *
358
     * Also accepts an array, $ignore, allowing you to
359
     * list words not to be capitalized.
360
     *
361
     * @param  array|null $ignore An array of words not to capitalize
362
     *
363
     * @return static
364
     */
365
    public function toTitleCase($ignore = null)
366
    {
367
        return $this->newSelf(preg_replace_callback
368
        (
369
            '/([\S]+)/u',
370
            function ($match) use ($ignore)
371
            {
372
                if ($ignore && in_array($match[0], $ignore, true))
373
                {
374
                    return $match[0];
375
                }
376
                else
377
                {
378
                    return (string)$this->newSelf($match[0])
379
                    ->toLowerCase()->upperCaseFirst();
380
                }
381
            },
382
            (string)$this->trim()
383
        ));
384
    }
385
386
    /**
387
     * Returns a trimmed string with the first letter capitalized.
388
     *
389
     * TODO: Be smarter and capitalise after every period.
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
390
     *
391
     * @return static
392
     */
393
    public function toSentenceCase()
394
    {
395
        return $this->trim()->toLowerCase()->upperCaseFirst();
396
    }
397
398
    /**
399
     * Converts the string into an URL slug.
400
     *
401
     * This includes replacing non-ASCII characters with their closest ASCII
402
     * equivalents, removing remaining non-ASCII and non-alphanumeric
403
     * characters, and replacing whitespace with $replacement.
404
     *
405
     * The replacement defaults to a single dash
406
     * and the string is also converted to lowercase.
407
     *
408
     * @param string $replacement The string used to replace whitespace
409
     * @param string $language    The language for the url
410
     * @param bool   $strToLower  string to lower
411
     *
412
     * @return static
413
     */
414
    public function toSlugCase($replacement = '-', $language = 'en', $strToLower = true)
415
    {
416
        if (!class_exists('\\voku\\helper\\URLify'))
417
        {
418
            throw new \RuntimeException
419
            (
420
                "This method requires \voku\helper\URLify. ".
421
                "Install with: composer require voku/urlify"
422
            );
423
        }
424
425
        return $this->newSelf
426
        (
427
            \voku\helper\URLify::slug
428
            (
429
                $this->scalarString,
430
                $language,
431
                $replacement,
432
                $strToLower
433
            )
434
        );
435
    }
436
}
437