1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Deployer\Component\PharUpdate\Version; |
6
|
|
|
|
7
|
|
|
use Deployer\Component\PharUpdate\Version\Exception\InvalidIdentifierException; |
8
|
|
|
use Deployer\Component\PharUpdate\Version\Exception\InvalidNumberException; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Builds a new version number. |
12
|
|
|
* |
13
|
|
|
* @author Kevin Herrera <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
class Builder extends Version |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* Removes the build metadata identifiers. |
19
|
|
|
*/ |
20
|
|
|
public function clearBuild(): void |
21
|
|
|
{ |
22
|
|
|
$this->build = []; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Removes the pre-release version identifiers. |
27
|
|
|
*/ |
28
|
|
|
public function clearPreRelease(): void |
29
|
|
|
{ |
30
|
|
|
$this->preRelease = []; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates a new Version builder. |
35
|
|
|
* |
36
|
|
|
* @return Builder The Version builder. |
37
|
|
|
*/ |
38
|
|
|
public static function create(): Builder |
39
|
|
|
{ |
40
|
|
|
return new Builder(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns a readonly Version instance. |
45
|
|
|
* |
46
|
|
|
* @return Version The readonly Version instance. |
47
|
|
|
*/ |
48
|
|
|
public function getVersion(): Version |
49
|
|
|
{ |
50
|
|
|
return new Version( |
51
|
|
|
$this->major, |
52
|
|
|
$this->minor, |
53
|
|
|
$this->patch, |
54
|
|
|
$this->preRelease, |
55
|
|
|
$this->build, |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Imports the version components. |
61
|
|
|
* |
62
|
|
|
* @param array $components The components. |
63
|
|
|
* |
64
|
|
|
* @return Builder The Version builder. |
65
|
|
|
*/ |
66
|
|
|
public function importComponents(array $components): self |
67
|
|
|
{ |
68
|
|
|
if (isset($components[Parser::BUILD])) { |
69
|
|
|
$this->build = $components[Parser::BUILD]; |
70
|
|
|
} else { |
71
|
|
|
$this->build = []; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (isset($components[Parser::MAJOR])) { |
75
|
|
|
$this->major = $components[Parser::MAJOR]; |
76
|
|
|
} else { |
77
|
|
|
$this->major = 0; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (isset($components[Parser::MINOR])) { |
81
|
|
|
$this->minor = $components[Parser::MINOR]; |
82
|
|
|
} else { |
83
|
|
|
$this->minor = 0; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if (isset($components[Parser::PATCH])) { |
87
|
|
|
$this->patch = $components[Parser::PATCH]; |
88
|
|
|
} else { |
89
|
|
|
$this->patch = 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (isset($components[Parser::PRE_RELEASE])) { |
93
|
|
|
$this->preRelease = $components[Parser::PRE_RELEASE]; |
94
|
|
|
} else { |
95
|
|
|
$this->preRelease = []; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Imports the version string representation. |
103
|
|
|
* |
104
|
|
|
* @param string $version The string representation. |
105
|
|
|
* |
106
|
|
|
* @return Builder The Version builder. |
107
|
|
|
*/ |
108
|
|
|
public function importString(string $version): self |
109
|
|
|
{ |
110
|
|
|
return $this->importComponents(Parser::toComponents($version)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Imports an existing Version instance. |
115
|
|
|
* |
116
|
|
|
* @param Version $version A Version instance. |
117
|
|
|
* |
118
|
|
|
* @return Builder The Version builder. |
119
|
|
|
*/ |
120
|
|
|
public function importVersion(Version $version): self |
121
|
|
|
{ |
122
|
|
|
return $this |
123
|
|
|
->setMajor($version->getMajor()) |
124
|
|
|
->setMinor($version->getMinor()) |
125
|
|
|
->setPatch($version->getPatch()) |
126
|
|
|
->setPreRelease($version->getPreRelease()) |
127
|
|
|
->setBuild($version->getBuild()); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Increments the major version number and resets the minor and patch |
132
|
|
|
* version numbers to zero. |
133
|
|
|
* |
134
|
|
|
* @param int $amount Increment by what amount? |
135
|
|
|
* |
136
|
|
|
* @return Builder The Version builder. |
137
|
|
|
*/ |
138
|
|
|
public function incrementMajor(int $amount = 1): self |
139
|
|
|
{ |
140
|
|
|
$this->major += $amount; |
141
|
|
|
$this->minor = 0; |
142
|
|
|
$this->patch = 0; |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Increments the minor version number and resets the patch version number |
149
|
|
|
* to zero. |
150
|
|
|
* |
151
|
|
|
* @param int $amount Increment by what amount? |
152
|
|
|
* |
153
|
|
|
* @return Builder The Version builder. |
154
|
|
|
*/ |
155
|
|
|
public function incrementMinor(int $amount = 1): self |
156
|
|
|
{ |
157
|
|
|
$this->minor += $amount; |
158
|
|
|
$this->patch = 0; |
159
|
|
|
|
160
|
|
|
return $this; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Increments the patch version number. |
165
|
|
|
* |
166
|
|
|
* @param int $amount Increment by what amount? |
167
|
|
|
* |
168
|
|
|
* @return Builder The Version builder. |
169
|
|
|
*/ |
170
|
|
|
public function incrementPatch(int $amount = 1): self |
171
|
|
|
{ |
172
|
|
|
$this->patch += $amount; |
173
|
|
|
|
174
|
|
|
return $this; |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Sets the build metadata identifiers. |
179
|
|
|
* |
180
|
|
|
* @param array $identifiers The build metadata identifiers. |
181
|
|
|
* |
182
|
|
|
* @return Builder The Version builder. |
183
|
|
|
* |
184
|
|
|
* @throws InvalidIdentifierException If an identifier is invalid. |
185
|
|
|
*/ |
186
|
|
|
public function setBuild(array $identifiers): self |
187
|
|
|
{ |
188
|
|
|
foreach ($identifiers as $identifier) { |
189
|
|
|
if (!Validator::isIdentifier($identifier)) { |
190
|
|
|
throw new InvalidIdentifierException($identifier); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$this->build = $identifiers; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Sets the major version number. |
201
|
|
|
* |
202
|
|
|
* @param int $number The major version number. |
203
|
|
|
* |
204
|
|
|
* @return Builder The Version builder. |
205
|
|
|
* |
206
|
|
|
* @throws InvalidNumberException If the number is invalid. |
207
|
|
|
*/ |
208
|
|
|
public function setMajor(int $number): self |
209
|
|
|
{ |
210
|
|
|
if (!Validator::isNumber($number)) { |
211
|
|
|
throw new InvalidNumberException($number); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
$this->major = intval($number); |
215
|
|
|
|
216
|
|
|
return $this; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Sets the minor version number. |
221
|
|
|
* |
222
|
|
|
* @param int $number The minor version number. |
223
|
|
|
* |
224
|
|
|
* @return Builder The Version builder. |
225
|
|
|
* |
226
|
|
|
* @throws InvalidNumberException If the number is invalid. |
227
|
|
|
*/ |
228
|
|
|
public function setMinor(int $number): self |
229
|
|
|
{ |
230
|
|
|
if (!Validator::isNumber($number)) { |
231
|
|
|
throw new InvalidNumberException($number); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$this->minor = intval($number); |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Sets the patch version number. |
241
|
|
|
* |
242
|
|
|
* @param int $number The patch version number. |
243
|
|
|
* |
244
|
|
|
* @return Builder The Version builder. |
245
|
|
|
* |
246
|
|
|
* @throws InvalidNumberException If the number is invalid. |
247
|
|
|
*/ |
248
|
|
|
public function setPatch(int $number): self |
249
|
|
|
{ |
250
|
|
|
if (!Validator::isNumber($number)) { |
251
|
|
|
throw new InvalidNumberException($number); |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
$this->patch = intval($number); |
255
|
|
|
|
256
|
|
|
return $this; |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Sets the pre-release version identifiers. |
261
|
|
|
* |
262
|
|
|
* @param array $identifiers The pre-release version identifiers. |
263
|
|
|
* |
264
|
|
|
* @return Builder The Version builder. |
265
|
|
|
* |
266
|
|
|
* @throws InvalidIdentifierException If an identifier is invalid. |
267
|
|
|
*/ |
268
|
|
|
public function setPreRelease(array $identifiers): self |
269
|
|
|
{ |
270
|
|
|
foreach ($identifiers as $identifier) { |
271
|
|
|
if (!Validator::isIdentifier($identifier)) { |
272
|
|
|
throw new InvalidIdentifierException($identifier); |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->preRelease = $identifiers; |
277
|
|
|
|
278
|
|
|
return $this; |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|