Completed
Pull Request — master (#17)
by Woody
02:04
created

Chain::reduce()   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
cc 1
eloc 2
c 1
b 0
f 1
nc 1
nop 2
dl 0
loc 4
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Equip;
4
5
use function Equip\Arr\to_array;
6
7
class Chain
8
{
9 1
    public static function from($source)
10
    {
11 1
        return new static(to_array($source));
12
    }
13
14
    /**
15
     * @var array
16
     */
17
    private $source;
18
19 1
    public function __construct(array $source)
20
    {
21 1
        $this->source = $source;
22 1
    }
23
24
    public function toArray()
25
    {
26
        return $this->source;
27
    }
28
29 1
    public function hasKey($needle)
30
    {
31 1
        return array_key_exists($needle, $this->source);
32
    }
33
34 1
    public function hasValue($needle)
35
    {
36 1
        return in_array($needle, $this->source);
37
    }
38
39
    public function reduce(callable $fn, $initial = null)
40
    {
41
        return array_reduce($this->source, $fn, $initial);
42
    }
43
44 1
    public function filter(callable $fn)
45
    {
46 1
        $copy = clone $this;
47 1
        $copy->source = array_filter($this->source, $fn);
48
49 1
        return $copy;
50
    }
51
52
    public function map(callable $fn)
53
    {
54
        $copy = clone $this;
55
        $copy->source = array_map($fn, $this->source);
56
57
        return $copy;
58
    }
59
60
    public function merge(array $values)
61
    {
62
        $copy = clone $this;
63
        $copy->source = array_merge($this->source, $values);
64
65
        return $copy;
66
    }
67
68
    public function intersect(array $values)
69
    {
70
        $copy = clone $this;
71
        $copy->source = array_intersect($this->source, $values);
72
73
        return $copy;
74
    }
75
76
    public function diff(array $values)
77
    {
78
        $copy = clone $this;
79
        $copy->source = array_diff($this->source, $values);
80
81
        return $copy;
82
    }
83
84
    public function unique()
85
    {
86
        $copy = clone $this;
87
        $copy->source = array_unique($this->source);
88
89
        return $copy;
90
    }
91
92
    public function values()
93
    {
94
        $copy = clone $this;
95
        $copy->source = array_values($this->source);
96
97
        return $copy;
98
    }
99
100 1
    public function keys()
101
    {
102 1
        $copy = clone $this;
103 1
        $copy->source = array_keys($this->source);
104
105 1
        return $copy;
106
    }
107
}
108