Completed
Push — feature/configuration ( 48ff2d...765854 )
by Stéphane
53:12 queued 25:50
created

Collection::count()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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