1
|
|
|
<?php namespace BuildR\Collection\Collection; |
2
|
|
|
|
3
|
|
|
use BuildR\Collection\Interfaces\CollectionInterface; |
4
|
|
|
use BuildR\Foundation\Object\ArrayConvertibleInterface; |
5
|
|
|
|
6
|
|
|
/** |
7
|
|
|
* Abstract collection implementation |
8
|
|
|
* |
9
|
|
|
* BuildR PHP Framework |
10
|
|
|
* |
11
|
|
|
* @author Zoltán Borsos <[email protected]> |
12
|
|
|
* @package Collection |
13
|
|
|
* @subpackage Collection |
14
|
|
|
* |
15
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
16
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
17
|
|
|
* @link https://github.com/Zolli/BuildR |
18
|
|
|
*/ |
19
|
|
|
abstract class AbstractCollection implements CollectionInterface { |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @type array |
23
|
|
|
*/ |
24
|
|
|
protected $data = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
|
|
public function toArray() { |
30
|
11 |
|
return array_map(function($value) { |
31
|
11 |
|
if((is_object($value) && method_exists($value, 'toArray')) |
32
|
11 |
|
|| ($value instanceof ArrayConvertibleInterface)) { |
33
|
|
|
return $value->toArray(); |
34
|
|
|
} |
35
|
|
|
|
36
|
11 |
|
return $value; |
37
|
11 |
|
}, $this->data); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* {@inheritdoc} |
42
|
|
|
*/ |
43
|
1 |
|
public function clear() { |
44
|
1 |
|
$this->data = []; |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
|
|
*/ |
50
|
23 |
|
public function contains($element) { |
51
|
23 |
|
return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* {@inheritdoc} |
56
|
|
|
*/ |
57
|
10 |
View Code Duplication |
public function containsAll($elements) { |
|
|
|
|
58
|
10 |
|
$elements = $this->collectionToArray($elements); |
59
|
|
|
|
60
|
10 |
|
$result = TRUE; |
61
|
|
|
|
62
|
10 |
|
foreach($elements as $item) { |
63
|
10 |
|
if($this->contains($item) === FALSE) { |
64
|
5 |
|
$result = FALSE; |
65
|
|
|
|
66
|
5 |
|
break; |
67
|
|
|
} |
68
|
10 |
|
} |
69
|
|
|
|
70
|
10 |
|
return $result; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
6 |
|
public function containsAny($elements) { |
77
|
6 |
|
$elements = $this->collectionToArray($elements); |
78
|
|
|
|
79
|
6 |
|
foreach($elements as $item) { |
80
|
6 |
|
if($this->contains($item) === TRUE) { |
81
|
5 |
|
return TRUE; |
82
|
|
|
} |
83
|
6 |
|
} |
84
|
|
|
|
85
|
6 |
|
return FALSE; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
6 |
|
public function equals(CollectionInterface $collection) { |
92
|
|
|
//First check the size, if this not equals the tow collection |
93
|
|
|
//not be identical |
94
|
6 |
|
if($collection->size() !== $this->size()) { |
95
|
1 |
|
return FALSE; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
//Use strict comparison to check arrays are equals |
99
|
|
|
//(Values and orders) |
100
|
5 |
|
return $collection->toArray() === $this->data; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
1 |
|
public function isEmpty() { |
107
|
1 |
|
return empty($this->data); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
14 |
|
public function size() { |
114
|
14 |
|
return (is_array($this->data)) ? count($this->data) : 0; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* {@inheritdoc} |
119
|
|
|
* |
120
|
|
|
* @codeCoverageIgnore |
121
|
|
|
*/ |
122
|
|
|
public function current() { |
123
|
|
|
return $this->data[$this->position]; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* {@inheritdoc} |
128
|
|
|
* |
129
|
|
|
* @codeCoverageIgnore |
130
|
|
|
*/ |
131
|
|
|
public function next() { |
132
|
|
|
return next($this->data); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
* |
138
|
|
|
* @codeCoverageIgnore |
139
|
|
|
*/ |
140
|
|
|
public function key() { |
141
|
|
|
return key($this->data); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* {@inheritdoc} |
146
|
|
|
* |
147
|
|
|
* @codeCoverageIgnore |
148
|
|
|
*/ |
149
|
|
|
public function valid() { |
150
|
|
|
return key($this->data) !== NULL; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
* |
156
|
|
|
* @codeCoverageIgnore |
157
|
|
|
*/ |
158
|
|
|
public function rewind() { |
159
|
|
|
return reset($this->data); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
8 |
|
public function count() { |
166
|
8 |
|
return $this->size(); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param array|\BuildR\Collection\Interfaces\CollectionInterface $elements |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
21 |
|
protected function collectionToArray($elements) { |
175
|
21 |
|
if($elements instanceof CollectionInterface) { |
176
|
1 |
|
$elements = $elements->toArray(); |
177
|
1 |
|
} |
178
|
|
|
|
179
|
21 |
|
return $elements; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
} |
183
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.