Completed
Push — master ( d5dc61...bf2878 )
by Randy
01:53
created

functions.php ➔ separate()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Iterator;
4
5
/**
6
 * @param array $data
7
 *
8
 * @return Iterator
9
 */
10
function iter(array $data): Iterator
11
{
12 17
    return new Iterator($data);
13
}
14
15
/**
16
 * @param string $str
17
 *
18
 * @return Iterator
19
 */
20
function chars(string $str): Iterator
21
{
22
    return new Iterator(str_split($str, 1));
23 18
}
24 5
25
/**
26
 * @param string $str
27 13
 * @param string $delimiter
28
 *
29
 * @return Iterator
30
 */
31
function separate(string $str, string $delimiter): Iterator
32
{
33
    return new Iterator(explode($delimiter, $str));
34
}
35
36
/**
37 1
 * @param string $str
38
 *
39
 * @return Iterator
40
 */
41
function lines(string $str): Iterator
42
{
43
    $iter = new Iterator(preg_split("/\r\n|\n|\r/", $str));
44
45
    return $iter->map('trim')->filter();
46
}
47
48
/**
49
 * @param $value
50
 *
51
 * @return Iterator
52
 */
53
function only($value): Iterator
54
{
55
    return new Iterator(is_array($value) ? $value : [$value]);
56
}