Completed
Branch release/v1.0.0 (b932d7)
by Edward
03:07 queued 40s
created

Collection   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 178
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 17
c 3
b 0
f 1
lcom 2
cbo 2
dl 0
loc 178
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A push() 0 4 1
A pop() 0 4 1
A shift() 0 4 1
A unshift() 0 4 1
A add() 0 6 1
A remove() 0 10 2
A set() 0 6 1
A slice() 0 4 1
A splice() 0 4 1
A map() 0 4 1
A reduce() 0 4 1
A sort() 0 4 1
A asort() 0 4 1
A each() 0 10 3
1
<?php
2
/**
3
 * File Collection.php
4
 *
5
 * @author Edward Pfremmer <[email protected]>
6
 */
7
namespace Epfremme\Collection;
8
9
use \Closure;
10
11
use Epfremme\Collection\Traits\ClearableTrait;
12
13
/**
14
 * Class Collection
15
 *
16
 * @package Epfremme\Collection
17
 */
18
class Collection extends BaseCollection
19
{
20
    use ClearableTrait;
21
22
    /**
23
     * Push new element to collection
24
     *
25
     * @param mixed $element
26
     * @return int
27
     */
28
    public function push($element)
29
    {
30
        return array_push($this->elements, $element);
31
    }
32
33
    /**
34
     * Pop last element from collection
35
     *
36
     * @return mixed
37
     */
38
    public function pop()
39
    {
40
        return array_pop($this->elements);
41
    }
42
43
    /**
44
     * Shift first element from collection
45
     *
46
     * @return mixed
47
     */
48
    public function shift()
49
    {
50
        return array_shift($this->elements);
51
    }
52
53
    /**
54
     * Unshift element to collection
55
     *
56
     * @param mixed $element
57
     * @return int
58
     */
59
    public function unshift($element)
60
    {
61
        return array_unshift($this->elements, $element);
62
    }
63
64
    /**
65
     * Add element to collection
66
     *
67
     * @param $element
68
     * @return $this
69
     */
70
    public function add($element)
71
    {
72
        $this->push($element);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Remove element from collection
79
     *
80
     * @param $element
81
     * @return bool
82
     */
83
    public function remove($element)
84
    {
85
        if (!$this->contains($element)) {
86
            return false;
87
        }
88
89
        $this->offsetUnset($this->indexOf($element));
90
91
        return true;
92
    }
93
94
    /**
95
     * Set new value to collection at position
96
     *
97
     * @param mixed $key
98
     * @param mixed $value
99
     * @return $this
100
     */
101
    public function set($key, $value)
102
    {
103
        $this->offsetSet($key, $value);
104
105
        return $this;
106
    }
107
108
    /**
109
     * Return new collection sliced from current elements
110
     *
111
     * @param int $offset
112
     * @param int $length
113
     * @return static
114
     */
115
    public function slice($offset = 0, $length = 0)
116
    {
117
        return new static(array_slice($this->elements, $offset, $length, true));
118
    }
119
120
    /**
121
     * Splice and return new collection from current elements
122
     *
123
     * @param int $offset
124
     * @param int $length
125
     * @param array|mixed $replacement
126
     * @return static
127
     */
128
    public function splice($offset = 0, $length = 0, $replacement = [])
129
    {
130
        return new static(array_splice($this->elements, $offset, $length, $replacement));
131
    }
132
133
    /**
134
     * Map function on against collection elements
135
     *
136
     * @param Closure $fn
137
     * @return static
138
     */
139
    public function map(Closure $fn)
140
    {
141
        return new static(array_map($fn, $this->getValues(), $this->getKeys()));
142
    }
143
144
    /**
145
     * Reduce collection to a single value
146
     *
147
     * @param Closure $fn
148
     * @param null $initial
149
     * @return mixed
150
     */
151
    public function reduce(Closure $fn, $initial = null)
152
    {
153
        return array_reduce($this->elements, $fn, $initial);
154
    }
155
156
    /**
157
     * Sort collection by user defined function
158
     *
159
     * @param Closure $fn
160
     * @return bool
161
     */
162
    public function sort(Closure $fn)
163
    {
164
        return usort($this->elements, $fn);
165
    }
166
167
    /**
168
     * Sort collection by use defined function and
169
     * maintain element keys/indicies
170
     *
171
     * @param Closure $fn
172
     * @return bool
173
     */
174
    public function asort(Closure $fn)
175
    {
176
        return uasort($this->elements, $fn);
177
    }
178
179
    /**
180
     * CAll function on each element of collection
181
     *
182
     * @param Closure $fn
183
     * @return bool
184
     */
185
    public function each(Closure $fn)
186
    {
187
        foreach ($this->elements as $key => $element) {
188
            if ( !$fn($key, $element)) {
189
                return false;
190
            }
191
        }
192
193
        return true;
194
    }
195
}
196