1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* PHP: Nelson Martell Library file |
4
|
|
|
* |
5
|
|
|
* Content: |
6
|
|
|
* - Class definition: [NelsonMartell] VersionComponent |
7
|
|
|
* |
8
|
|
|
* Copyright © 2015-2017 Nelson Martell (http://nelson6e65.github.io) |
9
|
|
|
* |
10
|
|
|
* Licensed under The MIT License (MIT) |
11
|
|
|
* For full copyright and license information, please see the LICENSE |
12
|
|
|
* Redistributions of files must retain the above copyright notice. |
13
|
|
|
* |
14
|
|
|
* @copyright 2015-2017 Nelson Martell |
15
|
|
|
* @link http://nelson6e65.github.io/php_nml/ |
16
|
|
|
* @since v0.1.1 |
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
18
|
|
|
* */ |
19
|
|
|
|
20
|
|
|
namespace NelsonMartell; |
21
|
|
|
|
22
|
|
|
use \InvalidArgumentException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Representa un componente de un número de Version. |
26
|
|
|
* Extiende la clase IntString, pero restringe los valores que puede tomar. |
27
|
|
|
* |
28
|
|
|
* @author Nelson Martell <[email protected]> |
29
|
|
|
* */ |
30
|
|
|
class VersionComponent extends IntString implements IEquatable |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* |
34
|
|
|
* |
35
|
|
|
* @param int|null $intValue |
36
|
|
|
* @param string|null $stringValue |
37
|
|
|
*/ |
38
|
49 |
|
public function __construct($intValue = null, $stringValue = null) |
39
|
|
|
{ |
40
|
|
|
// Validates filters for only null or int/string value types. |
41
|
49 |
|
parent::__construct($intValue, $stringValue); |
42
|
|
|
|
43
|
47 |
|
if ($intValue === null) { |
44
|
22 |
|
$this->intValue = $intValue; |
45
|
|
|
|
46
|
|
|
// Ignore string value if intValue is null. |
47
|
22 |
|
$this->stringValue = ''; |
48
|
|
|
} else { |
49
|
|
|
// Validation of values |
50
|
38 |
View Code Duplication |
if ($this->IntValue < 0) { |
|
|
|
|
51
|
|
|
$args = [ |
52
|
5 |
|
'position' => '1st', |
53
|
5 |
|
'actual' => $intValue, |
54
|
|
|
]; |
55
|
|
|
|
56
|
5 |
|
$msg = msg('Invalid argument value.'); |
|
|
|
|
57
|
5 |
|
$msg .= msg( |
58
|
5 |
|
' {position} argument must to be a positive number; "{actual}" given.', |
59
|
5 |
|
$args |
60
|
|
|
); |
61
|
|
|
|
62
|
5 |
|
throw new InvalidArgumentException($msg); |
63
|
|
|
} // Integer is valid |
64
|
|
|
|
65
|
34 |
|
if ($stringValue !== null) { |
66
|
33 |
|
if ($this->StringValue != '') { |
67
|
15 |
|
$pattern = '~^([a-z])$~'; // 1 char |
68
|
|
|
|
69
|
15 |
|
if (strlen($this->StringValue) > 1) { |
70
|
15 |
|
$start = '~^([a-z]|-)'; |
|
|
|
|
71
|
15 |
|
$middle = '([a-z]|[0-9]|-)*'; |
72
|
15 |
|
$end = '([a-z]|[0-9])$~'; |
|
|
|
|
73
|
|
|
|
74
|
15 |
|
$pattern = $start.$middle.$end; |
75
|
|
|
} |
76
|
|
|
|
77
|
15 |
|
$correct = (boolean) preg_match($pattern, $this->StringValue); |
78
|
|
|
|
79
|
15 |
|
if ($correct) { |
80
|
|
|
//Último chequeo: que no hayan 2 '-' consecutivos. |
81
|
12 |
|
$correct = strpos($this->StringValue, '--') == false ? true : false; |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
15 |
View Code Duplication |
if (!$correct) { |
|
|
|
|
85
|
|
|
$args = [ |
86
|
4 |
|
'position' => '2nd', |
87
|
4 |
|
'actual' => $stringValue, |
88
|
|
|
]; |
89
|
|
|
|
90
|
4 |
|
$msg = msg('Invalid argument value.'); |
|
|
|
|
91
|
4 |
|
$msg .= msg( |
92
|
4 |
|
' {position} parameter has invalid chars; "{actual}" given.', |
93
|
4 |
|
$args |
94
|
|
|
); |
95
|
|
|
|
96
|
4 |
|
throw new InvalidArgumentException($msg); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} // String is valid |
100
|
|
|
} |
101
|
40 |
|
} |
102
|
|
|
|
103
|
43 |
|
public static function parse($obj) |
104
|
|
|
{ |
105
|
43 |
|
if ($obj instanceof VersionComponent) { |
106
|
7 |
|
return $obj; |
107
|
|
|
} else { |
108
|
42 |
|
if ($obj === null || (is_string($obj) && trim($obj) === '')) { |
109
|
19 |
|
return new VersionComponent(); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
36 |
|
$objConverted = parent::parse($obj); |
114
|
|
|
|
115
|
29 |
|
return new VersionComponent($objConverted->IntValue, $objConverted->StringValue); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Determina si este componente tiene los valores predeterminados (0). |
120
|
|
|
* |
121
|
|
|
* @return boolean |
122
|
|
|
* */ |
123
|
9 |
|
public function isDefault() |
124
|
|
|
{ |
125
|
9 |
|
if ($this->IntValue === 0) { |
126
|
1 |
|
if ($this->StringValue === '') { |
127
|
1 |
|
return true; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
8 |
|
return false; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Getter method for VersionComponent::IntValue property. |
137
|
|
|
* |
138
|
|
|
* @return integer|NULL |
139
|
|
|
* */ |
140
|
126 |
|
public function getIntValue() |
141
|
|
|
{ |
142
|
126 |
|
return $this->intValue; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Determina si este componente NO tiene los valores predeterminados. |
148
|
|
|
* |
149
|
|
|
* @return boolean |
150
|
|
|
* */ |
151
|
9 |
|
public function isNotDefault() |
152
|
|
|
{ |
153
|
9 |
|
return !$this->isDefault(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Determina si esta instancia es nula. |
158
|
|
|
* |
159
|
|
|
* @return boolean |
160
|
|
|
* */ |
161
|
51 |
|
public function isNull() |
162
|
|
|
{ |
163
|
51 |
|
if ($this->IntValue === null) { |
164
|
23 |
|
return true; |
165
|
|
|
} |
166
|
|
|
|
167
|
39 |
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Determina si esta instancia NO es nula. |
172
|
|
|
* |
173
|
|
|
* @return boolean |
174
|
|
|
* */ |
175
|
38 |
|
public function isNotNull() |
176
|
|
|
{ |
177
|
38 |
|
return !$this->isNull(); |
178
|
|
|
} |
179
|
|
|
|
180
|
40 |
|
public function equals($other) |
181
|
|
|
{ |
182
|
40 |
|
if ($other instanceof VersionComponent) { |
183
|
33 |
|
if ($this->IntValue === $other->IntValue) { |
184
|
28 |
|
if ($this->StringValue === $other->StringValue) { |
185
|
23 |
|
return true; |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
} else { |
189
|
15 |
|
return parent::equals($other); |
190
|
|
|
} |
191
|
|
|
|
192
|
14 |
|
return false; |
193
|
|
|
} |
194
|
|
|
|
195
|
21 |
|
public function compareTo($other) |
196
|
|
|
{ |
197
|
21 |
|
if ($other === null) { |
198
|
1 |
|
return 1; |
199
|
20 |
|
} elseif ($this->equals($other)) { |
200
|
3 |
|
return 0; |
201
|
18 |
|
} elseif ($other instanceof VersionComponent) { |
202
|
|
|
// null < int |
203
|
11 |
|
if ($this->isNull()) { |
204
|
1 |
|
$r = -1; |
|
|
|
|
205
|
10 |
|
} elseif ($other->isNull()) { |
206
|
1 |
|
$r = 1; |
207
|
|
|
} else { |
208
|
|
|
// Here are evaluated as integers |
209
|
9 |
|
$r = $this->IntValue - $other->IntValue; |
210
|
|
|
|
211
|
9 |
|
if ($r === 0) { |
212
|
5 |
|
$r = strnatcmp($this->StringValue, $other->StringValue); |
213
|
|
|
} |
214
|
|
|
} |
215
|
8 |
|
} elseif (is_integer($other) || is_array($other)) { |
216
|
3 |
|
$r = 1; |
217
|
5 |
|
} elseif (is_string($other)) { |
218
|
|
|
try { |
219
|
3 |
|
$r = $this->compareTo(static::parse($other)); |
220
|
2 |
|
} catch (InvalidArgumentException $e) { |
221
|
2 |
|
$r = 1; |
222
|
|
|
} |
223
|
|
|
} else { |
224
|
2 |
|
$r = null; |
225
|
|
|
} |
226
|
|
|
|
227
|
18 |
|
return $r; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
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.