Completed
Push — master ( f62226...8b6b73 )
by Stéphane
10s
created

Collection   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 187
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 89.58%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 19
c 2
b 0
f 0
lcom 1
cbo 0
dl 0
loc 187
ccs 43
cts 48
cp 0.8958
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A withKeyFilter() 0 5 1
A withValueFilter() 0 5 1
A fromArray() 0 4 1
A count() 0 4 1
A offsetGet() 0 5 2
A offsetSet() 0 9 2
A offsetExists() 0 4 1
A offsetUnset() 0 7 2
A value() 0 7 1
A key() 0 7 1
A handle() 0 7 2
A flush() 0 5 1
A toArray() 0 4 1
A getIterator() 0 4 1
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
     * @param callable|null $key
0 ignored issues
show
Bug introduced by
There is no parameter named $key. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
46
     * @param callable|null $value
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
47 24
     */
48
    public function __construct(array $data = [])
49 24
    {
50 24
        $this->data        = $data;
51 24
    }
52 24
53
    /**
54
     * Set the keyFilter callable
55
     * @param  callable $key
56
     * @return Collection
57
     */
58
    public function withKeyFilter(callable $key)
59
    {
60
        $this->keyFilter = $key;
61
        return $this;
62
    }
63
64
    /**
65
     * Set the valueFilter callable
66
     * @param  callable $value
67 5
     * @return Collection
68
     */
69 5
    public function withValueFilter(callable $value)
70 5
    {
71
        $this->valueFilter = $value;
72
        return $this;
73
    }
74
75
    /**
76
     * Build a new Collection from an existing array
77
     * @param  array  $data
78 15
     * @return Collection
79
     */
80 15
    public static function fromArray(array $data)
81
    {
82
        return new self($data);
83 15
    }
84 15
85
    /**
86 15
     * @see \Countable::count
87
     * @return integer
88
     */
89
    public function count()
90
    {
91
        return count($this->data);
92
    }
93 5
94
    /**
95
     * @see \ArrayAccess::offsetGet
96 5
     * @return mixed
97 5
     */
98
    public function offsetGet($offset)
99
    {
100
        $offset = $this->key($offset);
101
        return isset($this->data[$offset])?$this->data[$offset]:null;
102
    }
103
104 1
    /**
105
     * @see \ArrayAccess::offsetSet
106 1
     * @param mixed $offset
107 1
     * @param mixed $value
108 1
     */
109 1
    public function offsetSet($offset, $value)
110 1
    {
111
        if (is_null($offset)) {
112
            $this->data[] = $this->value($value);
113
        } else {
114
            $offset = $this->key($offset);
115
            $this->data[$offset] = $this->value($value);
116
        }
117 15
    }
118
119 15
    /**
120 15
     * @see \ArrayAccess::offsetExists
121 15
     * @param mixed $offset
122 15
     * @return boolean
123
     */
124
    public function offsetExists($offset)
125
    {
126
        return array_key_exists($this->key($offset), $this->data);
127
    }
128
129
    /**
130 15
     * @see \ArrayAccess::offsetUnset
131
     * @param mixed $offset
132 15
     */
133 15
    public function offsetUnset($offset)
134 15
    {
135 15
        $offset = $this->key($offset);
136
        if ($this->offsetExists($offset)) {
137
            unset($this->data[$offset]);
138
        }
139
    }
140
141
    /**
142
     * Handle value to be used in the collection
143
     * @param  mixed $item
144 15
     * @return mixed The filtered value
145
     */
146 15
    protected function value($item)
147 15
    {
148
        return $this->handle(
149 15
            $item,
150
            $this->valueFilter
151
        );
152
    }
153
154
    /**
155
     * Handle key to be used in the collection
156 1
     * @param  mixed $item
157
     * @return mixed The filtered key
158 1
     */
159 1
    protected function key($item)
160
    {
161
        return $this->handle(
162
            $item,
163
            $this->keyFilter
164
        );
165
    }
166 3
167
    /**
168 3
     * Filter an item with a callback
169
     * @param  mixed         $item
170
     * @param  callable|null $filter
171
     * @return mixed
172
     */
173
    protected function handle($item, callable $filter = null)
174
    {
175 10
        if (null !== $filter) {
176
            return call_user_func($filter, $item);
177 10
        }
178
        return $item;
179
    }
180
181
    /**
182
     * Flush all items from the collection
183
     * @return Collection
184
     */
185
    public function flush()
186
    {
187
        $this->data = [];
188
        return $this;
189
    }
190
191
    /**
192
     * Transform the collection to a basic array
193
     * @return array
194
     */
195
    public function toArray()
196
    {
197
        return $this->data;
198
    }
199
200
    /**
201
     * @see \IteratorAggregate::getIterator
202
     * @return \ArrayIterator
203
     */
204
    public function getIterator()
205
    {
206
        return new ArrayIterator($this->data);
207
    }
208
}
209