|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
namespace Tebru\Collection; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class AbstractCollection |
|
11
|
|
|
* |
|
12
|
|
|
* Provides a skeletal implementation of the [@see CollectionInterface] |
|
13
|
|
|
* |
|
14
|
|
|
* @author Nate Brunette <[email protected]> |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class AbstractCollection implements CollectionInterface |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Ensure all elements of a collection exists in this collection |
|
20
|
|
|
* |
|
21
|
|
|
* Return true if the collection has changed, and false if it hasn't |
|
22
|
|
|
* |
|
23
|
|
|
* @param CollectionInterface $collection |
|
24
|
|
|
* @return bool |
|
25
|
|
|
*/ |
|
26
|
3 |
|
public function addAll(CollectionInterface $collection): bool |
|
27
|
|
|
{ |
|
28
|
3 |
|
return $this->addAllArray($collection->toArray()); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Ensure all elements of an array exists in this collection |
|
33
|
|
|
* |
|
34
|
|
|
* Return true if the collection has changed, and false if it hasn't |
|
35
|
|
|
* |
|
36
|
|
|
* @param array $collection |
|
37
|
|
|
* @return bool |
|
38
|
|
|
*/ |
|
39
|
63 |
|
public function addAllArray(array $collection): bool |
|
40
|
|
|
{ |
|
41
|
63 |
|
$size = $this->count(); |
|
42
|
63 |
|
foreach ($collection as $element) { |
|
43
|
63 |
|
$this->add($element); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
63 |
|
return $size !== $this->count(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Returns true if the collection contains element |
|
51
|
|
|
* |
|
52
|
|
|
* @param mixed $element |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
40 |
|
public function contains($element): bool |
|
56
|
|
|
{ |
|
57
|
40 |
|
return in_array($element, $this->toArray(), true); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Returns true if the collection contains all elements from another collection |
|
62
|
|
|
* |
|
63
|
|
|
* @param CollectionInterface $collection |
|
64
|
|
|
* @return bool |
|
65
|
|
|
*/ |
|
66
|
9 |
|
public function containsAll(CollectionInterface $collection): bool |
|
67
|
|
|
{ |
|
68
|
9 |
|
return $this->containsAllArray($collection->toArray()); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns true if the collection contains all elements from an array |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $collection |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
12 |
|
public function containsAllArray(array $collection): bool |
|
78
|
|
|
{ |
|
79
|
12 |
|
foreach ($collection as $element) { |
|
80
|
12 |
|
if (!$this->contains($element)) { |
|
81
|
12 |
|
return false; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
9 |
|
return true; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Returns the size of the collection |
|
90
|
|
|
* |
|
91
|
|
|
* @return int |
|
92
|
|
|
*/ |
|
93
|
75 |
|
public function count(): int |
|
94
|
|
|
{ |
|
95
|
75 |
|
return count($this->toArray()); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns true if the collection is empty |
|
100
|
|
|
* |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
6 |
|
public function isEmpty(): bool |
|
104
|
|
|
{ |
|
105
|
6 |
|
return 0 === $this->count(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* Remove all items in a collection from this collection |
|
110
|
|
|
* |
|
111
|
|
|
* Returns true if the collection was modified |
|
112
|
|
|
* |
|
113
|
|
|
* @param CollectionInterface $collection |
|
114
|
|
|
* @return bool |
|
115
|
|
|
*/ |
|
116
|
9 |
|
public function removeAll(CollectionInterface $collection): bool |
|
117
|
|
|
{ |
|
118
|
9 |
|
return $this->removeAllArray($collection->toArray()); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Remove all items in an array from this collection |
|
123
|
|
|
* |
|
124
|
|
|
* Returns true if the collection was modified |
|
125
|
|
|
* |
|
126
|
|
|
* @param array $collection |
|
127
|
|
|
* @return bool |
|
128
|
|
|
*/ |
|
129
|
12 |
|
public function removeAllArray(array $collection): bool |
|
130
|
|
|
{ |
|
131
|
12 |
|
$size = $this->count(); |
|
132
|
12 |
|
foreach ($collection as $element) { |
|
133
|
12 |
|
$this->remove($element); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
12 |
|
return $size !== $this->count(); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Remove all items from this collection that don't exist in specified collection |
|
141
|
|
|
* |
|
142
|
|
|
* Returns true if the collection was modified |
|
143
|
|
|
* |
|
144
|
|
|
* @param CollectionInterface $collection |
|
145
|
|
|
* @return bool |
|
146
|
|
|
*/ |
|
147
|
9 |
|
public function retainAll(CollectionInterface $collection): bool |
|
148
|
|
|
{ |
|
149
|
9 |
|
return $this->retainAllArray($collection->toArray()); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* Remove all items from this collection that don't exist in specified array |
|
154
|
|
|
* |
|
155
|
|
|
* Returns true if the collection was modified |
|
156
|
|
|
* |
|
157
|
|
|
* @param array $collection |
|
158
|
|
|
* @return bool |
|
159
|
|
|
*/ |
|
160
|
12 |
|
public function retainAllArray(array $collection): bool |
|
161
|
|
|
{ |
|
162
|
12 |
|
$size = $this->count(); |
|
163
|
12 |
|
foreach ($this as $element) { |
|
164
|
12 |
|
if (!in_array($element, $collection, true)) { |
|
165
|
12 |
|
$this->remove($element); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
12 |
|
return $size !== $this->count(); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Find the first element in collection |
|
174
|
|
|
* |
|
175
|
|
|
* The closure will get passed each element. Returning true will end the |
|
176
|
|
|
* loop and return that element |
|
177
|
|
|
* |
|
178
|
|
|
* @param callable $find |
|
179
|
|
|
* @return mixed |
|
180
|
|
|
*/ |
|
181
|
6 |
|
public function find(callable $find) |
|
182
|
|
|
{ |
|
183
|
6 |
|
foreach ($this as $element) { |
|
184
|
6 |
|
if (true === $find($element)) { |
|
185
|
6 |
|
return $element; |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
3 |
|
return null; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Use a closure to determine existence in the collection |
|
194
|
|
|
* |
|
195
|
|
|
* The closure will get passed each element. Returning true from the |
|
196
|
|
|
* closure will return true from this method. |
|
197
|
|
|
* |
|
198
|
|
|
* @param callable $exists |
|
199
|
|
|
* @return bool |
|
200
|
|
|
*/ |
|
201
|
3 |
|
public function exists(callable $exists): bool |
|
202
|
|
|
{ |
|
203
|
3 |
|
foreach ($this as $element) { |
|
204
|
3 |
|
if (true === $exists($element)) { |
|
205
|
3 |
|
return true; |
|
206
|
|
|
} |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
3 |
|
return false; |
|
210
|
|
|
} |
|
211
|
|
|
} |
|
212
|
|
|
|