1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Baleen\Migrations\Delta\Collection; |
21
|
|
|
|
22
|
|
|
use Baleen\Migrations\Exception\InvalidArgumentException; |
23
|
|
|
use Baleen\Migrations\Exception\Version\Collection\CollectionException; |
24
|
|
|
use Baleen\Migrations\Migration\Options\Direction; |
25
|
|
|
use Baleen\Migrations\Common\Collection\AbstractCollection; |
26
|
|
|
use Baleen\Migrations\Delta\Collection\Resolver\DefaultResolverStackFactory; |
27
|
|
|
use Baleen\Migrations\Delta\Collection\Resolver\ResolverInterface; |
28
|
|
|
use Baleen\Migrations\Delta\Comparator\ComparatorInterface; |
29
|
|
|
use Baleen\Migrations\Delta\Comparator\MigrationComparator; |
30
|
|
|
use Baleen\Migrations\Delta\DeltaInterface; |
31
|
|
|
use Zend\Stdlib\ArrayUtils; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Class CollectionAbstract. |
35
|
|
|
* |
36
|
|
|
* @author Gabriel Somoza <[email protected]> |
37
|
|
|
* |
38
|
|
|
* IMPROVE: this class has many methods. Consider refactoring it to keep number of methods under 10. |
39
|
|
|
* |
40
|
|
|
* @SuppressWarnings(PHPMD.TooManyMethods) |
41
|
|
|
* @SuppressWarnings(PHPMD.CouplingBetweenObjects) |
42
|
|
|
*/ |
43
|
|
|
class Collection extends AbstractCollection implements ResolvableCollectionInterface |
44
|
|
|
{ |
45
|
|
|
/** @var ResolverInterface */ |
46
|
|
|
private $resolver; |
47
|
|
|
|
48
|
|
|
/** @var ComparatorInterface */ |
49
|
|
|
private $comparator; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param DeltaInterface[]|\Traversable $versions |
53
|
|
|
* @param ResolverInterface $resolver |
|
|
|
|
54
|
|
|
* @param ComparatorInterface $comparator |
|
|
|
|
55
|
|
|
* |
56
|
|
|
* @throws InvalidArgumentException |
57
|
|
|
*/ |
58
|
157 |
|
public function __construct( |
59
|
1 |
|
$versions = [], |
60
|
|
|
ResolverInterface $resolver = null, |
61
|
|
|
ComparatorInterface $comparator = null |
62
|
|
|
) { |
63
|
157 |
|
if (null === $resolver) { |
64
|
122 |
|
$resolver = DefaultResolverStackFactory::create(); |
65
|
122 |
|
} |
66
|
157 |
|
$this->resolver = $resolver; |
67
|
|
|
|
68
|
157 |
|
if (null === $comparator) { |
69
|
147 |
|
$comparator = new MigrationComparator(); |
70
|
147 |
|
} |
71
|
157 |
|
$this->comparator = $comparator; |
72
|
|
|
|
73
|
157 |
|
parent::__construct($versions); |
|
|
|
|
74
|
155 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return ResolverInterface |
78
|
|
|
*/ |
79
|
149 |
|
final protected function getResolver() |
80
|
|
|
{ |
81
|
149 |
|
return $this->resolver; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Gets an element. |
86
|
|
|
* |
87
|
|
|
* @param mixed $element If an alias is given then it will be resolved to an element. Otherwise the $key will be used |
|
|
|
|
88
|
|
|
* to fetch the element by index. |
89
|
|
|
* @param bool $resolve Whether to use the resolver or not. |
90
|
|
|
* |
91
|
|
|
* @return DeltaInterface|null Null if not present |
92
|
|
|
*/ |
93
|
32 |
|
public function find($element, $resolve = true) |
94
|
|
|
{ |
95
|
32 |
|
$result = null; |
96
|
|
|
|
97
|
32 |
|
if (is_object($element)) { |
98
|
1 |
|
$element = (string) $element; |
99
|
1 |
|
} |
100
|
|
|
|
101
|
32 |
|
if ($resolve && is_string($element)) { |
102
|
32 |
|
$result = $this->getResolver()->resolve($element, $this); |
103
|
32 |
|
} |
104
|
|
|
|
105
|
32 |
|
if (null === $result && is_scalar($element)) { |
106
|
1 |
|
$result = $this->get($element); |
107
|
1 |
|
} |
108
|
|
|
|
109
|
32 |
|
return $result; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Returns whether the key exists in the collection. |
114
|
|
|
* |
115
|
|
|
* @param $index |
116
|
|
|
* @param bool $resolve |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
1 |
|
public function has($index, $resolve = true) |
121
|
|
|
{ |
122
|
1 |
|
return $this->find($index, $resolve) !== null; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @inheritdoc |
127
|
|
|
*/ |
128
|
144 |
|
public function validate(DeltaInterface $version) |
129
|
2 |
|
{ |
130
|
144 |
|
return !empty($version->getId()); // basically: any DeltaInterface is valid for this collection |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* invalidateResolverCache |
135
|
|
|
*/ |
136
|
147 |
|
final protected function invalidateResolverCache() |
137
|
|
|
{ |
138
|
147 |
|
$this->getResolver()->clearCache($this); |
139
|
147 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Add a version to the collection |
143
|
|
|
* |
144
|
|
|
* @param mixed $version |
145
|
|
|
* |
146
|
|
|
* @return bool |
147
|
|
|
* |
148
|
|
|
* @throws CollectionException |
149
|
|
|
* @throws InvalidArgumentException |
150
|
|
|
*/ |
151
|
144 |
|
public function add(DeltaInterface $version) |
152
|
|
|
{ |
153
|
144 |
|
$this->validate($version); |
154
|
143 |
|
$result = parent::add($version); |
155
|
143 |
|
if ($result) { |
156
|
143 |
|
$this->invalidateResolverCache(); |
157
|
143 |
|
} |
158
|
|
|
|
159
|
143 |
|
return $result; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param $key |
164
|
|
|
* |
165
|
|
|
* @return DeltaInterface|null |
166
|
|
|
*/ |
167
|
4 |
|
public function remove($key) |
168
|
|
|
{ |
169
|
4 |
|
$result = parent::remove($key); |
170
|
4 |
|
if ($result) { |
171
|
3 |
|
$this->invalidateResolverCache(); |
172
|
3 |
|
} |
173
|
|
|
|
174
|
4 |
|
return $result; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* {@inheritDoc} |
179
|
|
|
*/ |
180
|
10 |
|
public function replace(DeltaInterface $version) |
181
|
|
|
{ |
182
|
10 |
|
$this->validate($version); |
183
|
10 |
|
$removedElement = parent::replace($version); |
184
|
10 |
|
$this->invalidateResolverCache(); |
185
|
|
|
|
186
|
10 |
|
return $removedElement; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritDoc} |
191
|
|
|
*/ |
192
|
8 |
|
public function clear() |
193
|
|
|
{ |
194
|
8 |
|
parent::clear(); |
195
|
8 |
|
$this->invalidateResolverCache(); |
196
|
8 |
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Sort the collection |
200
|
|
|
* @param ComparatorInterface $comparator |
|
|
|
|
201
|
|
|
* @return static |
202
|
|
|
*/ |
203
|
19 |
|
public function sort(ComparatorInterface $comparator = null) |
204
|
|
|
{ |
205
|
19 |
|
if (null === $comparator) { |
206
|
9 |
|
$comparator = $this->comparator; |
207
|
9 |
|
} |
208
|
19 |
|
$elements = $this->toArray(); |
209
|
19 |
|
uasort($elements, $comparator); |
210
|
19 |
|
return new static($elements, $this->getResolver(), $comparator); |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* Returns a collection with elements sorted in reverse order. |
215
|
|
|
* |
216
|
|
|
* @return static |
217
|
|
|
*/ |
218
|
8 |
|
public function getReverse() |
219
|
|
|
{ |
220
|
8 |
|
return $this->sort($this->comparator->getReverse()); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return ComparatorInterface |
225
|
|
|
*/ |
226
|
19 |
|
public function getComparator() |
227
|
|
|
{ |
228
|
19 |
|
return $this->comparator; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.