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
|
|
|
* By default this method will use strict comparison checking, passing false |
24
|
|
|
* in will use a double equals (==) instead. |
25
|
|
|
* |
26
|
|
|
* @param CollectionInterface $collection |
27
|
|
|
* @param bool $strict |
28
|
|
|
* @return bool|mixed |
29
|
|
|
*/ |
30
|
24 |
|
public function addAll(CollectionInterface $collection, bool $strict = true): bool |
31
|
|
|
{ |
32
|
24 |
|
$size = $this->count(); |
33
|
24 |
|
foreach ($collection as $element) { |
34
|
24 |
|
$this->add($element); |
35
|
|
|
} |
36
|
|
|
|
37
|
24 |
|
return $size !== $this->count(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns true if the collection contains all elements from another collection |
42
|
|
|
* |
43
|
|
|
* By default this method will use strict comparison checking, passing false |
44
|
|
|
* in will use a double equals (==) instead. |
45
|
|
|
* |
46
|
|
|
* @param CollectionInterface $collection |
47
|
|
|
* @param bool $strict |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
6 |
|
public function containsAll(CollectionInterface $collection, bool $strict = true): bool |
51
|
|
|
{ |
52
|
6 |
|
$containsAll = true; |
53
|
6 |
|
foreach ($collection as $element) { |
54
|
6 |
|
if (!$this->contains($element, $strict)) { |
55
|
3 |
|
$containsAll = false; |
56
|
6 |
|
break; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
6 |
|
return $containsAll; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Returns true if the collection is empty |
65
|
|
|
* |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
6 |
|
public function isEmpty(): bool |
69
|
|
|
{ |
70
|
6 |
|
return 0 === $this->count(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Remove all items in a collection from this collection |
75
|
|
|
* |
76
|
|
|
* By default this method will use strict comparison checking, passing false |
77
|
|
|
* in will use a double equals (==) instead. |
78
|
|
|
* |
79
|
|
|
* Returns true if the collection was modified |
80
|
|
|
* |
81
|
|
|
* @param CollectionInterface $collection |
82
|
|
|
* @param bool $strict |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
12 |
View Code Duplication |
public function removeAll(CollectionInterface $collection, bool $strict = true): bool |
|
|
|
|
86
|
|
|
{ |
87
|
12 |
|
$size = $this->count(); |
88
|
12 |
|
foreach ($collection as $element) { |
89
|
12 |
|
$this->remove($element, $strict); |
90
|
|
|
} |
91
|
|
|
|
92
|
12 |
|
return $size !== $this->count(); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Remove all items from this collection that don't exist in specified collection |
97
|
|
|
* |
98
|
|
|
* By default this method will use strict comparison checking, passing false |
99
|
|
|
* in will use a double equals (==) instead. |
100
|
|
|
* |
101
|
|
|
* Returns true if the collection was modified |
102
|
|
|
* |
103
|
|
|
* @param CollectionInterface $collection |
104
|
|
|
* @param bool $strict |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
12 |
View Code Duplication |
public function retainAll(CollectionInterface $collection, bool $strict = true): bool |
|
|
|
|
108
|
|
|
{ |
109
|
12 |
|
$size = $this->count(); |
110
|
12 |
|
foreach ($this as $element) { |
111
|
12 |
|
if (!$collection->contains($element, $strict)) { |
112
|
12 |
|
$this->remove($element, $strict); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
12 |
|
return $size !== $this->count(); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Use a closure to determine existence in the collection |
121
|
|
|
* |
122
|
|
|
* The closure will get passed each element. Returning true from the |
123
|
|
|
* closure will return true from this method. |
124
|
|
|
* |
125
|
|
|
* @param callable $exists |
126
|
|
|
* @return bool |
127
|
|
|
*/ |
128
|
3 |
|
public function exists(callable $exists): bool |
129
|
|
|
{ |
130
|
3 |
|
foreach ($this as $element) { |
131
|
3 |
|
if (true === $exists($element)) { |
132
|
3 |
|
return true; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
136
|
3 |
|
return false; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
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.