nelson6e65 /
php_nml
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * PHP: Nelson Martell Library file |
||
| 5 | * |
||
| 6 | * Copyright © 2015-2021 Nelson Martell (http://nelson6e65.github.io) |
||
| 7 | * |
||
| 8 | * Licensed under The MIT License (MIT) |
||
| 9 | * For full copyright and license information, please see the LICENSE |
||
| 10 | * Redistributions of files must retain the above copyright notice. |
||
| 11 | * |
||
| 12 | * @copyright 2015-2021 Nelson Martell |
||
| 13 | * @link http://nelson6e65.github.io/php_nml/ |
||
| 14 | * @since 0.1.1 |
||
| 15 | * @license http://www.opensource.org/licenses/mit-license.php The MIT License (MIT) |
||
| 16 | * */ |
||
| 17 | |||
| 18 | declare(strict_types=1); |
||
| 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 | * @since 0.1.1 |
||
| 30 | * */ |
||
| 31 | class VersionComponent extends IntString implements IEquatable |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * |
||
| 35 | * |
||
| 36 | * @param int|null $intValue |
||
| 37 | * @param string|null $stringValue |
||
| 38 | */ |
||
| 39 | 37 | public function __construct($intValue = null, $stringValue = null) |
|
| 40 | { |
||
| 41 | // Validates filters for only null or int/string value types. |
||
| 42 | 37 | parent::__construct($intValue, $stringValue); |
|
| 43 | |||
| 44 | 35 | $intValue = $this->intValue; |
|
| 45 | 35 | $stringValue = $this->stringValue; |
|
| 46 | |||
| 47 | 35 | if ($intValue === null) { |
|
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 48 | // Ignore string value if intValue is null. |
||
| 49 | 19 | $stringValue = ''; |
|
| 50 | } else { |
||
| 51 | // Validation of values |
||
| 52 | 29 | if ($intValue < 0) { |
|
| 53 | 4 | $args = [ |
|
| 54 | 4 | 'position' => '1st', |
|
| 55 | 4 | 'actual' => $intValue, |
|
| 56 | 4 | ]; |
|
| 57 | |||
| 58 | 4 | $msg = msg('Invalid argument value.'); |
|
| 59 | 4 | $msg .= msg( |
|
| 60 | 4 | ' {position} argument must to be a positive number; "{actual}" given.', |
|
| 61 | 4 | $args |
|
| 62 | 4 | ); |
|
| 63 | |||
| 64 | 4 | throw new InvalidArgumentException($msg); |
|
| 65 | } // Integer is valid |
||
| 66 | |||
| 67 | 26 | if ($stringValue !== null) { |
|
|
0 ignored issues
–
show
|
|||
| 68 | 26 | if ($stringValue != '') { |
|
| 69 | 8 | $pattern = '~^([a-z])$~'; // 1 char |
|
| 70 | |||
| 71 | 8 | if (strlen($stringValue) > 1) { |
|
| 72 | 8 | $start = '~^([a-z]|-)'; |
|
| 73 | 8 | $middle = '([a-z]|[0-9]|-)*'; |
|
| 74 | 8 | $end = '([a-z]|[0-9])$~'; |
|
| 75 | |||
| 76 | 8 | $pattern = $start . $middle . $end; |
|
| 77 | } |
||
| 78 | |||
| 79 | 8 | $correct = (bool) preg_match($pattern, $stringValue); |
|
| 80 | |||
| 81 | 8 | if ($correct) { |
|
| 82 | //Último chequeo: que no hayan 2 '-' consecutivos. |
||
| 83 | 6 | $correct = strpos($stringValue, '--') == false ? true : false; |
|
|
0 ignored issues
–
show
|
|||
| 84 | } |
||
| 85 | |||
| 86 | 8 | if (!$correct) { |
|
| 87 | 2 | $args = [ |
|
| 88 | 2 | 'position' => '2nd', |
|
| 89 | 2 | 'actual' => $stringValue, |
|
| 90 | 2 | ]; |
|
| 91 | |||
| 92 | 2 | $msg = msg('Invalid argument value.'); |
|
| 93 | 2 | $msg .= msg( |
|
| 94 | 2 | ' {position} parameter has invalid chars; "{actual}" given.', |
|
| 95 | 2 | $args |
|
| 96 | 2 | ); |
|
| 97 | |||
| 98 | 2 | throw new InvalidArgumentException($msg); |
|
| 99 | } |
||
| 100 | } |
||
| 101 | } // String is valid |
||
| 102 | } |
||
| 103 | |||
| 104 | 31 | parent::__construct($intValue, $stringValue); |
|
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Converts the object to an instance of `VersionComponent` if compatible. |
||
| 109 | * |
||
| 110 | * @param mixed $obj Object to convert. |
||
| 111 | * |
||
| 112 | * @return VersionComponent |
||
| 113 | * @throws InvalidArgumentException if object is not a string or format is invalid. |
||
| 114 | */ |
||
| 115 | 43 | public static function parse($obj) |
|
| 116 | { |
||
| 117 | 43 | if ($obj instanceof VersionComponent) { |
|
| 118 | 7 | return $obj; |
|
| 119 | } else { |
||
| 120 | 42 | if ($obj === null || (is_string($obj) && trim($obj) === '')) { |
|
| 121 | 19 | return new VersionComponent(); |
|
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | 36 | $objConverted = parent::parse($obj); |
|
| 126 | |||
| 127 | 32 | return new VersionComponent($objConverted->intValue, $objConverted->stringValue); |
|
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Determina si este componente tiene los valores predeterminados (0). |
||
| 132 | * |
||
| 133 | * @return bool |
||
| 134 | * */ |
||
| 135 | 9 | public function isDefault(): bool |
|
| 136 | { |
||
| 137 | 9 | if ($this->intValue === 0) { |
|
| 138 | 1 | if ($this->stringValue === '') { |
|
| 139 | 1 | return true; |
|
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | 8 | return false; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Determina si este componente NO tiene los valores predeterminados. |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | * */ |
||
| 151 | 9 | public function isNotDefault(): bool |
|
| 152 | { |
||
| 153 | 9 | return !$this->isDefault(); |
|
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Determina si esta instancia es nula. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | * */ |
||
| 161 | 20 | public function isNull(): bool |
|
| 162 | { |
||
| 163 | 20 | if ($this->intValue === null) { |
|
| 164 | 7 | return true; |
|
| 165 | } |
||
| 166 | |||
| 167 | 14 | return false; |
|
| 168 | } |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Determina si esta instancia NO es nula. |
||
| 172 | * |
||
| 173 | * @return bool |
||
| 174 | * */ |
||
| 175 | 9 | public function isNotNull(): bool |
|
| 176 | { |
||
| 177 | 9 | return !$this->isNull(); |
|
| 178 | } |
||
| 179 | |||
| 180 | 40 | public function equals($other): bool |
|
| 181 | { |
||
| 182 | 40 | if ($other instanceof VersionComponent) { |
|
| 183 | 33 | if ($this->intValue === $other->intValue) { |
|
| 184 | 28 | if ($this->stringValue === $other->stringValue) { |
|
| 185 | 33 | 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 | 11 | $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 | 3 | $r = 1; |
|
| 222 | } |
||
| 223 | } else { |
||
| 224 | 2 | $r = null; |
|
| 225 | } |
||
| 226 | |||
| 227 | 18 | return $r; |
|
| 228 | } |
||
| 229 | } |
||
| 230 |