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