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

Chain::values()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 1
eloc 4
c 2
b 0
f 2
nc 1
nop 0
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
1
<?php
2
3
namespace Equip;
4
5
use function Equip\Arr\to_array;
6
7
class Chain
8
{
9
    /**
10
     * Create a new chain from a source.
11
     *
12
     * @param array|Traversable $source
13
     *
14
     * @return static
15
     */
16 12
    public static function from($source)
17
    {
18 12
        return new static(to_array($source));
19
    }
20
21
    /**
22
     * @var array
23
     */
24
    private $source;
25
26
    /**
27
     * @param array $source
28
     */
29 12
    public function __construct(array $source)
30
    {
31 12
        $this->source = $source;
32 12
    }
33
34
    /**
35
     * Get the current source.
36
     *
37
     * @return array
38
     */
39 9
    public function toArray()
40
    {
41 9
        return $this->source;
42
    }
43
44
    /**
45
     * Check if a key is defined in the source.
46
     *
47
     * @param mixed $needle
48
     *
49
     * @return boolean
50
     */
51 1
    public function hasKey($needle)
52
    {
53 1
        return array_key_exists($needle, $this->source);
54
    }
55
56
    /**
57
     * Check if a value is defined in the source.
58
     *
59
     * @param mixed $needle
60
     *
61
     * @return boolean
62
     */
63 1
    public function hasValue($needle)
64
    {
65 1
        return in_array($needle, $this->source);
66
    }
67
68
    /**
69
     * Get a copy with only values.
70
     *
71
     * @return static
72
     */
73 1
    public function values()
74
    {
75 1
        $copy = clone $this;
76 1
        $copy->source = array_values($this->source);
77
78 1
        return $copy;
79
    }
80
81
    /**
82
     * Get a copy with only keys.
83
     *
84
     * @return static
85
     */
86 1
    public function keys()
87
    {
88 1
        $copy = clone $this;
89 1
        $copy->source = array_keys($this->source);
90
91 1
        return $copy;
92
    }
93
94
    /**
95
     * Get a copy merged with new values.
96
     *
97
     * @param array $values
98
     *
99
     * @return static
100
     */
101 1
    public function merge(array $values)
102
    {
103 1
        $copy = clone $this;
104 1
        $copy->source = array_merge($this->source, $values);
105
106 1
        return $copy;
107
    }
108
109
    /**
110
     * Get a copy intersected with new values.
111
     *
112
     * @param array $values
113
     *
114
     * @return static
115
     */
116 1
    public function intersect(array $values)
117
    {
118 1
        $copy = clone $this;
119 1
        $copy->source = array_intersect($this->source, $values);
120
121 1
        return $copy;
122
    }
123
124
    /**
125
     * Get a copy diffed with other values.
126
     *
127
     * @param array $values
128
     *
129
     * @return static
130
     */
131 1
    public function diff(array $values)
132
    {
133 1
        $copy = clone $this;
134 1
        $copy->source = array_diff($this->source, $values);
135
136 1
        return $copy;
137
    }
138
139
    /**
140
     * Get a copy with only unique values.
141
     *
142
     * @return static
143
     */
144 1
    public function unique()
145
    {
146 1
        $copy = clone $this;
147 1
        $copy->source = array_unique($this->source);
148
149 1
        return $copy;
150
    }
151
152
    /**
153
     * Reduce the array with a callback.
154
     *
155
     * @param callable $fn
156
     * @param mixed $initial
157
     *
158
     * @return mixed
159
     */
160 1
    public function reduce(callable $fn, $initial = null)
161
    {
162 1
        return array_reduce($this->source, $fn, $initial);
163
    }
164
165
    /**
166
     * Get a copy filtered with a callback.
167
     *
168
     * @param callable $fn
169
     *
170
     * @return static
171
     */
172 1
    public function filter(callable $fn)
173
    {
174 1
        $copy = clone $this;
175 1
        $copy->source = array_filter($this->source, $fn);
176
177 1
        return $copy;
178
    }
179
180
    /**
181
     * Get a copy mapped with a callback.
182
     *
183
     * @param callable $fn
184
     *
185
     * @return static
186
     */
187 1
    public function map(callable $fn)
188
    {
189 1
        $copy = clone $this;
190 1
        $copy->source = array_map($fn, $this->source);
191
192 1
        return $copy;
193
    }
194
}
195