Completed
Push — master ( 5c4fef...b408c6 )
by Michał
01:56
created

ArrayCollection::extractProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
namespace Barenote\Collection;
3
4
use ArrayIterator;
5
use Closure;
6
7
/**
8
 * Class ArrayCollection
9
 * @package Barenote\Collection
10
 */
11
class ArrayCollection implements \Countable, \IteratorAggregate, \ArrayAccess, \JsonSerializable
12
{
13
    /**
14
     * @var array
15
     */
16
    private $elements;
17
18
    /**
19
     * ArrayCollection constructor.
20
     * @param array $elements
21
     */
22
    public function __construct(array $elements = [])
23
    {
24
        $this->elements = $elements;
25
    }
26
27
    public function isEmpty()
28
    {
29
        return count($this->elements) == 0;
30
    }
31
32
    public function first()
33
    {
34
        return reset($this->elements);
35
    }
36
    public function firstOrNull()
37
    {
38
        if ($result = $this->first()) {
39
            return $result;
40
        }
41
        return null;
42
    }
43
44
    /**
45
     * @param $value
46
     */
47
    public function add($value)
48
    {
49
        $this->elements[] = $value;
50
    }
51
52
    /**
53
     * Clear the internal array
54
     */
55
    public function clear()
56
    {
57
        $this->elements = [];
58
    }
59
60
    /**
61
     * Checks if collection contains provided element.
62
     *
63
     * @param $element
64
     *
65
     * @return bool
66
     */
67
    public function contains($element)
68
    {
69
        return in_array($element, $this->elements, true);
70
    }
71
72
    public function getIterator()
73
    {
74
        return new ArrayIterator($this->elements);
75
    }
76
77
    public function offsetExists($offset)
78
    {
79
        return $this->containsKey($offset);
80
    }
81
82
    /**
83
     * Checks if collection contains provided key.
84
     *
85
     * @param $key
86
     *
87
     * @return bool
88
     */
89
    public function containsKey($key)
90
    {
91
        return isset($this->elements[$key]) || array_key_exists($key, $this->elements);
92
    }
93
94
    public function offsetGet($offset)
95
    {
96
        return $this->get($offset);
97
    }
98
99
    /**
100
     * @param $key
101
     * @return mixed|null
102
     */
103
    public function get($key)
104
    {
105
        if (isset($this->elements[$key])) {
106
            return $this->elements[$key];
107
        }
108
109
        return null;
110
    }
111
112
    public function offsetSet($offset, $value)
113
    {
114
        $this->set($offset, $value);
115
    }
116
117
    /**
118
     * @param $key
119
     * @param $element
120
     */
121
    public function set($key, $element)
122
    {
123
        $this->elements[$key] = $element;
124
    }
125
126
    public function offsetUnset($offset)
127
    {
128
        $this->remove($offset);
129
    }
130
131
    public function remove($key)
132
    {
133
        if (isset($this->elements[$key]) || array_key_exists($key, $this->elements)) {
134
            $removed = $this->elements[$key];
135
            unset($this->elements[$key]);
136
137
            return $removed;
138
        }
139
140
        return null;
141
    }
142
143
    public function count()
144
    {
145
        return count($this->elements);
146
    }
147
148
    public function jsonSerialize()
149
    {
150
        return $this->toArray();
151
    }
152
153
    /**
154
     * @return array
155
     */
156
    public function toArray()
157
    {
158
        return $this->elements;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function filter(Closure $p)
165
    {
166
        return new static(array_filter($this->elements, $p));
167
    }
168
169
    protected function extractProperties($propertyFetchMethod)
170
    {
171
        $values = [];
172
        $this->forAll(
173
            function ($entry) use (&$values, $propertyFetchMethod) {
174
                $values[] = $entry->{$propertyFetchMethod}();
175
            }
176
        );
177
178
        return $values;
179
    }
180
181
    /**
182
     * @param callable|Closure $p
183
     */
184
    public function forAll(Closure $p)
185
    {
186
        foreach ($this->elements as $key => $element) {
187
            $p($element, $key);
188
        }
189
    }
190
}