Completed
Push — master ( 092247...6dd5e4 )
by Stéphane
06:20 queued 03:28
created

Collection::offsetGet()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
/**
3
 * This file is part of the bee4/transport package.
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 *
7
 * @copyright Bee4 2015
8
 * @author  Stephane HULARD <[email protected]>
9
 * @package Bee4\Transport
10
 */
11
12
namespace Bee4\Transport;
13
14
use ArrayAccess;
15
use Countable;
16
use ArrayIterator;
17
18
/**
19
 * Data collection implementation
20
 * @package Bee4\Transport
21
 */
22
class Collection implements ArrayAccess, Countable
23
{
24
    /**
25
     * Data collection as array
26
     * @var array
27
     */
28
    private $data;
29
30
    /**
31
     * Callback filter to be applied on all values
32
     * @var callable|null
33
     */
34
    private $valueFilter;
35
36
    /**
37
     * Callback filter to be applied on all keys
38
     * @var callable|null
39
     */
40
    private $keyFilter;
41
42
    /**
43
     * Build Collection
44
     * @param array $data
45
     */
46 31
    public function __construct(array $data = [])
47
    {
48 31
        $this->data        = $data;
49 31
    }
50
51
    /**
52
     * Set the keyFilter callable
53
     * @param  callable $key
54
     * @return Collection
55
     */
56 23
    public function withKeyFilter(callable $key)
57
    {
58 23
        $this->keyFilter = $key;
59 23
        return $this;
60
    }
61
62
    /**
63
     * Set the valueFilter callable
64
     * @param  callable $value
65
     * @return Collection
66
     */
67
    public function withValueFilter(callable $value)
68
    {
69
        $this->valueFilter = $value;
70
        return $this;
71
    }
72
73
    /**
74
     * Build a new Collection from an existing array
75
     * @param  array  $data
76
     * @return Collection
77
     */
78
    public static function fromArray(array $data)
79
    {
80
        return new self($data);
81
    }
82
83
    /**
84
     * @see \Countable::count
85
     * @return integer
86
     */
87
    public function count()
88
    {
89
        return count($this->data);
90
    }
91
92
    /**
93
     * @see \ArrayAccess::offsetGet
94
     * @return mixed
95
     */
96 22
    public function offsetGet($offset)
97
    {
98 22
        $offset = $this->key($offset);
99 22
        return isset($this->data[$offset])?$this->data[$offset]:null;
100
    }
101
102
    /**
103
     * @see \ArrayAccess::offsetSet
104
     * @param mixed $offset
105
     * @param mixed $value
106
     */
107 22
    public function offsetSet($offset, $value)
108
    {
109 22
        if (is_null($offset)) {
110
            $this->data[] = $this->value($value);
111
        } else {
112 22
            $offset = $this->key($offset);
113 22
            $this->data[$offset] = $this->value($value);
114
        }
115 22
    }
116
117
    /**
118
     * @see \ArrayAccess::offsetExists
119
     * @param mixed $offset
120
     * @return boolean
121
     */
122 21
    public function offsetExists($offset)
123
    {
124 21
        return array_key_exists($this->key($offset), $this->data);
125
    }
126
127
    /**
128
     * @see \ArrayAccess::offsetUnset
129
     * @param mixed $offset
130
     */
131 1
    public function offsetUnset($offset)
132
    {
133 1
        $offset = $this->key($offset);
134 1
        if ($this->offsetExists($offset)) {
135 1
            unset($this->data[$offset]);
136
        }
137 1
    }
138
139
    /**
140
     * Handle value to be used in the collection
141
     * @param  mixed $item
142
     * @return mixed The filtered value
143
     */
144 22
    protected function value($item)
145
    {
146 22
        return $this->handle(
147
            $item,
148 22
            $this->valueFilter
149
        );
150
    }
151
152
    /**
153
     * Handle key to be used in the collection
154
     * @param  mixed $item
155
     * @return mixed The filtered key
156
     */
157 25
    protected function key($item)
158
    {
159 25
        return $this->handle(
160
            $item,
161 25
            $this->keyFilter
162
        );
163
    }
164
165
    /**
166
     * Filter an item with a callback
167
     * @param  mixed         $item
168
     * @param  callable|null $filter
169
     * @return mixed
170
     */
171 25
    protected function handle($item, callable $filter = null)
172
    {
173 25
        if (null !== $filter) {
174 7
            return call_user_func($filter, $item);
175
        }
176 25
        return $item;
177
    }
178
179
    /**
180
     * Flush all items from the collection
181
     * @return Collection
182
     */
183 1
    public function flush()
184
    {
185 1
        $this->data = [];
186 1
        return $this;
187
    }
188
189
    /**
190
     * Transform the collection to a basic array
191
     * @return array
192
     */
193 2
    public function toArray()
194
    {
195 2
        return $this->data;
196
    }
197
198
    /**
199
     * @see \IteratorAggregate::getIterator
200
     * @return \ArrayIterator
201
     */
202 12
    public function getIterator()
203
    {
204 12
        return new ArrayIterator($this->data);
205
    }
206
}
207