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

Chain   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 46.67%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 101
ccs 21
cts 45
cp 0.4667
rs 10
wmc 14
lcom 1
cbo 0

14 Methods

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