forked from muglug/psl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathchunk.php
51 lines (45 loc) · 1.62 KB
/
chunk.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Psl\Str;
use Psl;
use Psl\Internal;
/**
* Returns an array containing the string split into chunks of the given size.
*
* Example:
*
* Str\chunk('Hello, World')
* => Arr('H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd')
*
* Str\chunk('PHP', 3)
* => Arr('PHP')
*
* Str\chunk('مرحبا')
* => Arr('م', 'ر', 'ح', 'ب', 'ا')
*
* Str\chunk('مرحبا', 2)
* => Arr('مر', 'حب', 'ا')
*
* @param int $chunk_length maximum length of the chunk
*
* @throws Psl\Exception\InvariantViolationException If the given $chunk_length is negative or above
* the limit ( 65535 ), or an invalid $encoding is provided.
*
* @return list<string> if $chunk_length parameter is specified, the returned array will be broken down
* into chunks with each being $chunk_length in length, otherwise each chunk will be
* one character in length.
* If the $chunk_length length exceeds the length of string, the entire string is returned
* as the first (and only) array element.
*
* @pure
*/
function chunk(string $string, int $chunk_length = 1, ?string $encoding = null): array
{
Psl\invariant($chunk_length >= 1, 'Expected a non-negative chunk size.');
if ('' === $string) {
return [];
}
Psl\invariant(65535 >= $chunk_length, 'Maximum chunk length must not exceed 65535.');
/** @var list<string> */
return mb_str_split($string, $chunk_length, Internal\internal_encoding($encoding));
}