1
|
|
|
<?php namespace BuildR\Collection\Collection; |
2
|
|
|
|
3
|
|
|
use BuildR\Collection\Collection\CollectionInterface; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Abstract collection implementation |
7
|
|
|
* |
8
|
|
|
* BuildR PHP Framework |
9
|
|
|
* |
10
|
|
|
* @author Zoltán Borsos <[email protected]> |
11
|
|
|
* @package Collection |
12
|
|
|
* @subpackage Collection |
13
|
|
|
* |
14
|
|
|
* @copyright Copyright 2015, Zoltán Borsos. |
15
|
|
|
* @license https://github.com/Zolli/BuildR/blob/master/LICENSE.md |
16
|
|
|
* @link https://github.com/Zolli/BuildR |
17
|
|
|
*/ |
18
|
|
|
abstract class AbstractCollection implements CollectionInterface { |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @type array |
22
|
|
|
*/ |
23
|
|
|
protected $data = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
11 |
|
public function toArray() { |
29
|
11 |
|
return (array) $this->data; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
1 |
|
public function clear() { |
36
|
1 |
|
$this->data = []; |
37
|
1 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
23 |
|
public function contains($element) { |
43
|
23 |
|
return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
10 |
View Code Duplication |
public function containsAll($elements) { |
|
|
|
|
50
|
10 |
|
$elements = $this->collectionToArray($elements); |
51
|
|
|
|
52
|
10 |
|
$result = TRUE; |
53
|
|
|
|
54
|
10 |
|
foreach($elements as $item) { |
55
|
10 |
|
if($this->contains($item) === FALSE) { |
56
|
5 |
|
$result = FALSE; |
57
|
|
|
|
58
|
5 |
|
break; |
59
|
|
|
} |
60
|
10 |
|
} |
61
|
|
|
|
62
|
10 |
|
return $result; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* {@inheritdoc} |
67
|
|
|
*/ |
68
|
6 |
|
public function containsAny($elements) { |
69
|
6 |
|
$elements = $this->collectionToArray($elements); |
70
|
|
|
|
71
|
6 |
|
foreach($elements as $item) { |
72
|
6 |
|
if($this->contains($item) === TRUE) { |
73
|
5 |
|
return TRUE; |
74
|
|
|
} |
75
|
6 |
|
} |
76
|
|
|
|
77
|
6 |
|
return FALSE; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
1 |
|
public function isEmpty() { |
84
|
1 |
|
return empty($this->data); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
14 |
|
public function size() { |
91
|
14 |
|
return (is_array($this->data)) ? count($this->data) : 0; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
* |
97
|
|
|
* @codeCoverageIgnore |
98
|
|
|
*/ |
99
|
|
|
public function current() { |
100
|
|
|
return current($this->data); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
* |
106
|
|
|
* @codeCoverageIgnore |
107
|
|
|
*/ |
108
|
|
|
public function next() { |
109
|
|
|
return next($this->data); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* {@inheritdoc} |
114
|
|
|
* |
115
|
|
|
* @codeCoverageIgnore |
116
|
|
|
*/ |
117
|
|
|
public function key() { |
118
|
|
|
return key($this->data); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* {@inheritdoc} |
123
|
|
|
* |
124
|
|
|
* @codeCoverageIgnore |
125
|
|
|
*/ |
126
|
|
|
public function valid() { |
127
|
|
|
return key($this->data) !== NULL; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
* |
133
|
|
|
* @codeCoverageIgnore |
134
|
|
|
*/ |
135
|
|
|
public function rewind() { |
136
|
|
|
return reset($this->data); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* {@inheritdoc} |
141
|
|
|
*/ |
142
|
8 |
|
public function count() { |
143
|
8 |
|
return $this->size(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param array|\BuildR\Collection\Collection\CollectionInterface $elements |
148
|
|
|
* |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
21 |
|
protected function collectionToArray($elements) { |
152
|
21 |
|
if($elements instanceof CollectionInterface) { |
153
|
1 |
|
$elements = $elements->toArray(); |
154
|
1 |
|
} |
155
|
|
|
|
156
|
21 |
|
return $elements; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
} |
160
|
|
|
|
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.