|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sugarcrm\UpgradeSpec\Version; |
|
4
|
|
|
|
|
5
|
|
|
class OrderedList implements \IteratorAggregate, \Countable, \Serializable |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @var Version[] |
|
9
|
|
|
*/ |
|
10
|
|
|
private $versions = []; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Range constructor. |
|
14
|
|
|
* |
|
15
|
|
|
* @param array $versions |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(array $versions = []) |
|
18
|
|
|
{ |
|
19
|
|
|
foreach ($versions as $version) { |
|
20
|
|
|
$this->addVersion($version); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
// sort versions (jASC) |
|
24
|
|
|
usort($this->versions, function (Version $v1, Version $v2) { |
|
25
|
|
|
$v1 = (string) $v1->getFull(); |
|
|
|
|
|
|
26
|
|
|
$v2 = (string) $v2->getFull(); |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
return version_compare($v1, $v2, '<') ? -1 : (version_compare($v1, $v2, '>') ? 1 : strnatcmp((string) $v1, (string) $v2)); |
|
29
|
|
|
}); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param array $versions |
|
34
|
|
|
* @param bool $checkAliases |
|
35
|
|
|
* |
|
36
|
|
|
* @return OrderedList |
|
37
|
|
|
*/ |
|
38
|
|
|
public function addVersions(array $versions, $checkAliases = false) |
|
39
|
|
|
{ |
|
40
|
|
|
return $this->merge(new OrderedList($versions), $checkAliases); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param OrderedList $range |
|
45
|
|
|
* @param bool $checkAliases |
|
46
|
|
|
* |
|
47
|
|
|
* @return OrderedList |
|
48
|
|
|
*/ |
|
49
|
|
|
public function merge(OrderedList $range, $checkAliases = false) |
|
50
|
|
|
{ |
|
51
|
|
|
$filtered = $range->filter(function (Version $version) use ($checkAliases) { |
|
52
|
|
|
return !$this->contains($version, $checkAliases); |
|
53
|
|
|
}); |
|
54
|
|
|
|
|
55
|
|
|
return new self(array_merge($this->versions, iterator_to_array($filtered))); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param Version $version |
|
60
|
|
|
* @param bool $checkAliases |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function contains(Version $version, $checkAliases = false) |
|
65
|
|
|
{ |
|
66
|
|
|
if ($checkAliases) { |
|
67
|
|
|
return in_array($version->getFull(), $this->map(function (Version $version) { |
|
68
|
|
|
return $version->getFull(); |
|
69
|
|
|
})); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return in_array((string) $version, array_map('strval', $this->versions)); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return Version |
|
77
|
|
|
*/ |
|
78
|
|
|
public function last() |
|
79
|
|
|
{ |
|
80
|
|
|
if (!count($this->versions)) { |
|
81
|
|
|
throw new \LogicException('You can\'t get anything from empty version range'); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return $this->versions[count($this->versions) - 1]; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Creates filtered range |
|
89
|
|
|
* |
|
90
|
|
|
* @param callable $callback |
|
91
|
|
|
* |
|
92
|
|
|
* @return OrderedList |
|
93
|
|
|
*/ |
|
94
|
|
|
public function filter(callable $callback) |
|
95
|
|
|
{ |
|
96
|
|
|
return new OrderedList(array_filter($this->versions, $callback)); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Checks if range equals to the given one |
|
101
|
|
|
* |
|
102
|
|
|
* @param OrderedList $range |
|
103
|
|
|
* @param bool $checkAliases |
|
104
|
|
|
* |
|
105
|
|
|
* @return bool |
|
106
|
|
|
*/ |
|
107
|
|
|
public function isEqualTo(OrderedList $range, $checkAliases = false) |
|
108
|
|
|
{ |
|
109
|
|
|
if ($checkAliases) { |
|
110
|
|
|
$getFull = function (Version $version) { |
|
111
|
|
|
return $version->getFull(); |
|
112
|
|
|
}; |
|
113
|
|
|
|
|
114
|
|
|
return (new OrderedList($range->map($getFull)))->isEqualTo(new OrderedList($this->map($getFull)), false); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $this->__toString() === (string) $range; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Maps versions to something else |
|
122
|
|
|
* |
|
123
|
|
|
* @param callable $callback |
|
124
|
|
|
* |
|
125
|
|
|
* @return array |
|
126
|
|
|
*/ |
|
127
|
|
|
public function map(callable $callback) |
|
128
|
|
|
{ |
|
129
|
|
|
return array_map($callback, $this->versions); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Retrieve an external iterator. |
|
134
|
|
|
* |
|
135
|
|
|
* @return \Traversable |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getIterator() |
|
138
|
|
|
{ |
|
139
|
|
|
return new \ArrayIterator($this->versions); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Count elements of an object. |
|
144
|
|
|
* |
|
145
|
|
|
* @return int The custom count as an integer. |
|
146
|
|
|
*/ |
|
147
|
|
|
public function count() |
|
148
|
|
|
{ |
|
149
|
|
|
return count($this->versions); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* String representation of object. |
|
154
|
|
|
* |
|
155
|
|
|
* @return string the string representation of the object or null |
|
156
|
|
|
*/ |
|
157
|
|
|
public function serialize() |
|
158
|
|
|
{ |
|
159
|
|
|
return serialize($this->versions); |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Constructs the object. |
|
164
|
|
|
* |
|
165
|
|
|
* @param string $serialized |
|
166
|
|
|
* |
|
167
|
|
|
* @return void |
|
168
|
|
|
*/ |
|
169
|
|
|
public function unserialize($serialized) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->versions = unserialize($serialized); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* @return string |
|
176
|
|
|
*/ |
|
177
|
|
|
public function __toString() |
|
178
|
|
|
{ |
|
179
|
|
|
return '[' . implode(', ', $this->versions) . ']'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* @param $version |
|
184
|
|
|
* @param bool $checkAliases |
|
185
|
|
|
*/ |
|
186
|
|
|
private function addVersion($version, $checkAliases = false) |
|
187
|
|
|
{ |
|
188
|
|
|
if (!($version instanceof Version)) { |
|
189
|
|
|
$version = new Version($version); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
if (!$this->contains($version, $checkAliases)) { |
|
193
|
|
|
$this->versions[] = $version; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
} |
|
197
|
|
|
|