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.
Completed
Push — develop ( 805516...c4143e )
by Baptiste
03:27
created

StringPrimitive::chunk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Innmind\Immutable;
4
5
use Innmind\Immutable\Exception\TypeException;
6
use Innmind\Immutable\Exception\RegexException;
7
use Innmind\Immutable\Exception\SubstringException;
8
9
class StringPrimitive implements PrimitiveInterface, StringableInterface
10
{
11
    const PAD_RIGHT = STR_PAD_RIGHT;
12
    const PAD_LEFT = STR_PAD_LEFT;
13 52
    const PAD_BOTH = STR_PAD_BOTH;
14
    const PREG_NO_FLAGS = 0;
15 52
    const PREG_SPLIT_NO_EMPTY = PREG_SPLIT_NO_EMPTY;
16 1
    const PREG_SPLIT_DELIM_CAPTURE = PREG_SPLIT_DELIM_CAPTURE;
17
    const PREG_SPLIT_OFFSET_CAPTURE = PREG_SPLIT_OFFSET_CAPTURE;
18
    const PREG_OFFSET_CAPTURE = PREG_OFFSET_CAPTURE;
19 51
20 51
    private $value;
21
22
    public function __construct($value)
23
    {
24
        if (!is_string($value)) {
25 2
            throw new TypeException('Value must be a string');
26
        }
27 2
28
        $this->value = (string) $value;
29
    }
30
31
    /**
32
     * {@inheritdoc}
33 27
     */
34
    public function toPrimitive()
35 27
    {
36
        return $this->value;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function __toString()
43
    {
44
        return $this->value;
45 3
    }
46
47 3
    /**
48 3
     * Split the string into a collection of ones
49 3
     *
50 3
     * @param string $delimiter
51
     *
52 3
     * @return TypedCollection
53 3
     */
54 3
    public function split($delimiter = null)
55
    {
56 3
        $delimiter = (string) $delimiter;
57 3
        $parts = empty($delimiter) ?
58
                str_split($this->value) : explode($delimiter, $this->value);
59 3
        $strings = [];
60
61
        foreach ($parts as $part) {
62
            $strings[] = new self($part);
63
        }
64
65
        return new TypedCollection(
66
            self::class,
67
            $strings
68
        );
69 1
    }
70
71 1
    /**
72
     * Returns a collection of the string splitted by the given chunk size
73
     *
74
     * @param int $size
75
     *
76
     * @return TypedCollection
77
     */
78
    public function chunk($size = 1)
79
    {
80
        return $this->split()->chunk((int) $size);
81
    }
82
83
    /**
84 3
     * Returns the position of the first occurence of the string
85
     *
86 3
     * @param string $needle
87
     * @param int $offset
88 3
     *
89 1
     * @throws SubstringException If the string is not found
90 1
     *
91
     * @return int
92 1
     */
93
    public function pos($needle, $offset = 0)
94
    {
95 2
        $position = mb_strpos($this->value, (string) $needle, (int) $offset);
96
97
        if ($position === false) {
98
            throw new SubstringException(sprintf(
99
                'Substring "%s" not found',
100
                $needle
101
            ));
102
        }
103
104
        return (int) $position;
105
    }
106 1
107
    /**
108 1
     * Replace all occurences of the search string with the replacement one
109 1
     *
110 1
     * @param string $search
111 1
     * @param string $replacement
112 1
     *
113
     * @return StringPrimitive
114
     */
115
    public function replace($search, $replacement)
116
    {
117
        return new self(str_replace(
118
            (string) $search,
119
            (string) $replacement,
120
            $this->value
121
        ));
122
    }
123
124 2
    /**
125
     * Returns the string following the given delimiter
126 2
     *
127
     * @param string $delimiter
128 2
     *
129 1
     * @throws SubstringException If the string is not found
130 1
     *
131
     * @return StringInterface
132 1
     */
133
    public function str($delimiter)
134
    {
135 1
        $sub = mb_strstr($this->value, (string) $delimiter);
136
137
        if ($sub === false) {
138
            throw new SubstringException(sprintf(
139
                'Substring "%s" not found',
140
                $delimiter
141
            ));
142
        }
143 1
144
        return new self($sub);
145 1
    }
146
147
    /**
148
     * Return the string in upper case
149
     *
150
     * @return StringPrimitive
151
     */
152
    public function toUpper()
153 1
    {
154
        return new self(mb_strtoupper($this->value));
155 1
    }
156
157
    /**
158
     * Return the string in lower case
159
     *
160
     * @return StringPrimitive
161
     */
162
    public function toLower()
163 2
    {
164
        return new self(mb_strtolower($this->value));
165 2
    }
166
167
    /**
168
     * Return the string length
169
     *
170
     * @return int
171
     */
172
    public function length()
173 1
    {
174
        return strlen($this->value);
175 1
    }
176
177
    /**
178
     * Reverse the string
179
     *
180
     * @return StringPrimitive
181
     */
182
    public function reverse()
183
    {
184
        return new self(strrev($this->value));
185
    }
186
187 1
    /**
188
     * Pad the string
189 1
     *
190 1
     * @param int $length
191 1
     * @param string $character
192 1
     * @param int $direction
193
     *
194 1
     * @return StringPrimitive
195
     */
196
    public function pad($length, $character = ' ', $direction = self::PAD_RIGHT)
197
    {
198
        return new self(str_pad(
199
            $this->value,
200
            (int) $length,
201
            (string) $character,
202
            $direction
203
        ));
204
    }
205 1
206
    /**
207 1
     * Pad to the right
208
     *
209
     * @param int $length
210
     * @param string $character
211
     *
212
     * @return StringPrimitive
213
     */
214
    public function rightPad($length, $character = ' ')
215
    {
216
        return $this->pad($length, $character, self::PAD_RIGHT);
217
    }
218 1
219
    /**
220 1
     * Pad to the left
221
     *
222
     * @param int $length
223
     * @param string $character
224
     *
225
     * @return StringPrimitive
226
     */
227
    public function leftPad($length, $character = ' ')
228
    {
229
        return $this->pad($length, $character, self::PAD_LEFT);
230
    }
231 1
232
    /**
233 1
     * Pad both sides
234
     *
235
     * @param int $length
236
     * @param string $character
237
     *
238
     * @return StringPrimitive
239
     */
240
    public function uniPad($length, $character = ' ')
241
    {
242
        return $this->pad($length, $character, self::PAD_BOTH);
243
    }
244
245 1
    /**
246
     * Find length of initial segment not matching mask
247 1
     *
248
     * @param string $mask
249 1
     * @param int $start
250 1
     * @param int $length
251 1
     *
252 1
     * @return int
253 1
     */
254 1
    public function cspn($mask, $start = 0, $length = null)
255 1
    {
256
        $start = (int) $start;
257 1
258
        if ($length === null) {
259
            $value = strcspn($this->value, (string) $mask, $start);
260 1
        } else {
261
            $value = strcspn(
262
                $this->value,
263
                (string) $mask,
264
                $start,
265
                (int) $length
266
            );
267
        }
268
269
        return (int) $value;
270 1
    }
271
272 1
    /**
273
     * Repeat the string n times
274
     *
275
     * @param int $repeat
276
     *
277
     * @return StringPrimitive
278
     */
279
    public function repeat($repeat)
280 1
    {
281
        return new self(str_repeat($this->value, (int) $repeat));
282 1
    }
283
284
    /**
285
     * Shuffle the string
286
     *
287
     * @return StringPrimitive
288
     */
289
    public function shuffle()
290 1
    {
291
        return new self(str_shuffle($this->value));
292 1
    }
293
294
    /**
295
     * Strip slashes
296
     *
297
     * @return StringPrimitive
298
     */
299
    public function stripSlashes()
300 1
    {
301
        return new self(stripslashes($this->value));
302 1
    }
303
304
    /**
305
     * Strip C-like slashes
306
     *
307
     * @return StringPrimitive
308
     */
309
    public function stripCSlashes()
310
    {
311
        return new self(stripcslashes($this->value));
312 1
    }
313
314 1
    /**
315 1
     * Return the word count
316 1
     *
317
     * @param string $charlist
318 1
     *
319
     * @return int
320
     */
321
    public function wordCount($charlist = '')
322
    {
323
        return (int) str_word_count(
324
            $this->value,
325
            0,
326
            (string) $charlist
327
        );
328 1
    }
329
330 1
    /**
331
     * Return the collection of words
332 1
     *
333 1
     * @param string $charlist
334 1
     *
335
     * @return TypedCollection
336 1
     */
337 1
    public function words($charlist = '')
338
    {
339 1
        $words = str_word_count($this->value, 2, (string) $charlist);
340
341
        foreach ($words as &$word) {
342
            $word = new self($word);
343
        }
344
345
        return new TypedCollection(
346
            self::class,
347
            $words
348
        );
349
    }
350
351 1
    /**
352
     * Split the string using a regular expression
353 1
     *
354
     * @param string $regex
355 1
     * @param int $limit
356 1
     * @param int $flags
357 1
     *
358
     * @return TypedCollection
359 1
     */
360 1
    public function pregSplit($regex, $limit = -1, $flags = self::PREG_NO_FLAGS)
361
    {
362 1
        $strings = preg_split((string) $regex, $this->value, $limit, $flags);
363
364
        foreach ($strings as &$string) {
365
            $string = new self($string);
366
        }
367
368
        return new TypedCollection(
369
            self::class,
370
            $strings
371
        );
372
    }
373
374
    /**
375
     * Check if the string match the given regular expression
376 2
     *
377
     * @param string $regex
378 2
     * @param int $offset
379 2
     *
380
     * @throws Exception If the regex failed
381 2
     *
382 1
     * @return bool
383
     */
384
    public function match($regex, $offset = 0)
385 1
    {
386
        $matches = [];
387
        $value = preg_match((string) $regex, $this->value, $matches, 0, $offset);
388
389
        if ($value === false) {
390
            throw new RegexException('', preg_last_error());
391
        }
392
393
        return (bool) $value;
394
    }
395
396
    /**
397
     * Return a collection of the elements matching the regex
398
     *
399 2
     * @param string $regex
400
     * @param int $offset
401 2
     * @param int $flags
402 2
     *
403 2
     * @throws Exception If the regex failed
404 2
     *
405 2
     * @return TypedCollection
406 2
     */
407
    public function getMatches($regex, $offset = 0, $flags = self::PREG_NO_FLAGS)
408 2
    {
409
        $matches = [];
410 2
        $value = preg_match(
411 1
            (string) $regex,
412 2
            $this->value,
413
            $matches,
414 2
            $flags,
415 1
            $offset
416
        );
417
418 1
        foreach ($matches as &$match) {
419
            $match = new self($match);
420
        }
421
422
        if ($value === false) {
423
            throw new RegexException('', preg_last_error());
424
        }
425
426
        return new TypedCollection(self::class, $matches);
427
    }
428
429
    /**
430
     * Replace part of the string by using a regular expression
431
     *
432 1
     * @param string $regex
433
     * @param string $replacement
434 1
     * @param int $limit
435 1
     *
436 1
     * @throws Exception If the regex failed
437 1
     *
438
     * @return StringPrimitive
439 1
     */
440
    public function pregReplace($regex, $replacement, $limit = -1)
441 1
    {
442
        $value = preg_replace(
443
            (string) $regex,
444
            (string) $replacement,
445 1
            $this->value,
446
            $limit
447
        );
448
449
        if ($value === null) {
450
            throw new RegexException('', preg_last_error());
451
        }
452
453
        return new self($value);
454
    }
455
456 1
    /**
457
     * Return part of the string
458 1
     *
459 1
     * @param int $start
460 1
     * @param int $length
461 1
     *
462
     * @return StringPrimitive
463
     */
464 1
    public function substring($start, $length = null)
465
    {
466
        if ($length === null) {
467
            $sub = substr($this->value, (int) $start);
468
        } else {
469
            $sub = substr($this->value, (int) $start, (int) $length);
470
        }
471
472
        return new self($sub);
473
    }
474
475
    /**
476
     * Return a formatted string
477
     *
478
     * @return StringPrimitive
479
     */
480
    public function sprintf()
481
    {
482
        $params = func_get_args();
483
        array_unshift($params, $this->value);
484
        $formatted = call_user_func_array('sprintf', $params);
485
486
        return new self($formatted);
487
    }
488
}
489