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 | use NelsonMartell\Extensions\Text; |
||
24 | |||
25 | /** |
||
26 | * Representa un elemento mixto, compuesto por un entero y una cadena unidos |
||
27 | * (en ese orden). |
||
28 | * El método IntString::toString obtiene esa cadena compuesta. |
||
29 | * |
||
30 | * @author Nelson Martell <[email protected]> |
||
31 | * @since 0.1.1 |
||
32 | * |
||
33 | * @property-read int $intValue Gets the integer part. |
||
34 | * @property-read string $stringValue Gets the string part. |
||
35 | * */ |
||
36 | class IntString extends StrictObject implements IEquatable, IComparable |
||
37 | { |
||
38 | /** |
||
39 | * Creates a new IntString instance. |
||
40 | * |
||
41 | * @param int|null $intValue Integer part. Default: `0` (zero). |
||
42 | * @param string|null $stringValue String part. Default: `''` (empty). |
||
43 | * |
||
44 | * @since 1.0.0-dev Allow `null` value for `$intValue`. |
||
45 | */ |
||
46 | 37 | public function __construct(?int $intValue = 0, $stringValue = '') |
|
47 | { |
||
48 | 37 | $this->intValue = $intValue; |
|
49 | |||
50 | 37 | if (!typeof($stringValue)->canBeString()) { |
|
51 | 2 | $args = [ |
|
52 | 2 | 'position' => '2nd', |
|
53 | 2 | 'expected' => typeof('string') . '", "' . typeof(null) . '" or "any object convertible to string', |
|
54 | 2 | 'actual' => typeof($stringValue), |
|
55 | 2 | ]; |
|
56 | |||
57 | 2 | $msg = msg('Invalid argument type.'); |
|
58 | 2 | $msg .= msg( |
|
59 | 2 | ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', |
|
60 | 2 | $args |
|
61 | 2 | ); |
|
62 | |||
63 | 2 | throw new InvalidArgumentException($msg); |
|
64 | } |
||
65 | |||
66 | 35 | $this->stringValue = (string) $stringValue; |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Converts the object to an instance of `IntString` if compatible. |
||
71 | * |
||
72 | * @param mixed $obj Object to convert to `IntString`. |
||
73 | * |
||
74 | * @return IntString |
||
75 | * @throws InvalidArgumentException if object is not a string or format is invalid. |
||
76 | */ |
||
77 | 25 | public static function parse($obj) |
|
78 | { |
||
79 | 25 | if ($obj instanceof IntString) { |
|
80 | return $obj; |
||
81 | } |
||
82 | |||
83 | 25 | if (is_integer($obj)) { |
|
84 | 17 | return new IntString($obj); |
|
85 | } |
||
86 | |||
87 | try { |
||
88 | 9 | $intValue = (int) Text::ensureIsString($obj); |
|
89 | } catch (InvalidArgumentException $e) { |
||
90 | $args = [ |
||
91 | 'position' => '1st', |
||
92 | 'expected' => 'string" or "integer', |
||
93 | 'actual' => typeof($obj), |
||
94 | ]; |
||
95 | |||
96 | $msg = msg('Invalid argument type.'); |
||
97 | $msg .= msg( |
||
98 | ' {position} parameter must to be an instance of "{expected}"; "{actual}" given.', |
||
99 | $args |
||
100 | ); |
||
101 | |||
102 | throw new InvalidArgumentException($msg, 1, $e); |
||
103 | } |
||
104 | |||
105 | 9 | $stringValue = ltrim($obj, "${intValue}"); |
|
106 | |||
107 | // Validate that 0 (zero) is not interpreted as '' (empty string) |
||
108 | 9 | if ($stringValue === $obj) { |
|
109 | 2 | $msg = msg('Invalid argument value.'); |
|
110 | 2 | $msg .= msg(' "{0}" (string) must to start with an integer.', $obj); |
|
111 | |||
112 | 2 | throw new InvalidArgumentException($msg); |
|
113 | } |
||
114 | |||
115 | 7 | return new IntString($intValue, $stringValue); |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * Integer part of the instance. |
||
120 | * |
||
121 | * @var int |
||
122 | */ |
||
123 | private $intValue; |
||
124 | |||
125 | /** |
||
126 | * String part of the instance. |
||
127 | * |
||
128 | * @var string |
||
129 | */ |
||
130 | private $stringValue; |
||
131 | |||
132 | /** |
||
133 | * Getter for $intValue property. |
||
134 | * |
||
135 | * @return int|null |
||
136 | */ |
||
137 | 66 | protected function getIntValue() |
|
138 | { |
||
139 | 66 | return $this->intValue; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Getter for $stringValue property. |
||
144 | * |
||
145 | * @return string |
||
146 | */ |
||
147 | 63 | protected function getStringValue(): string |
|
148 | { |
||
149 | 63 | return $this->stringValue; |
|
150 | } |
||
151 | |||
152 | /** |
||
153 | * {@inheritDoc} |
||
154 | */ |
||
155 | 9 | public function toString(): string |
|
156 | { |
||
157 | 9 | return $this->getIntValue() . $this->getStringValue(); |
|
158 | } |
||
159 | |||
160 | /** |
||
161 | * Indicates whether the specified object is equal to the current instance. |
||
162 | * |
||
163 | * @param mixed $other |
||
164 | * |
||
165 | * @return bool |
||
166 | */ |
||
167 | 15 | public function equals($other): bool |
|
168 | { |
||
169 | 15 | if ($other instanceof IntString) { |
|
170 | if ($this->getIntValue() === $other->getIntValue()) { |
||
171 | if ($this->getStringValue() === $other->getStringValue()) { |
||
172 | return true; |
||
173 | } |
||
174 | } |
||
175 | } |
||
176 | |||
177 | 15 | return false; |
|
178 | } |
||
179 | |||
180 | |||
181 | /** |
||
182 | * Determina la posición relativa de esta instancia con respecto al |
||
183 | * objeto especificado. |
||
184 | * Nota: Cualquier objeto que no sea instancia de IntString se |
||
185 | * considerará menor. |
||
186 | * |
||
187 | * @param mixed $other Objeto con el que se va a comparar. |
||
188 | * |
||
189 | * @return int Cero (0), si esta instancia es igual a $other; mayor |
||
190 | * a cero (>0), si es mayor a $other; menor a cero (<0), si es menor. |
||
191 | * */ |
||
192 | public function compareTo($other) |
||
193 | { |
||
194 | $r = $this->equals($other) ? 0 : 9999; |
||
195 | |||
196 | if ($r != 0) { |
||
197 | if ($other instanceof IntString) { |
||
198 | $r = $this->intValue - $other->intValue; |
||
199 | |||
200 | if ($r == 0) { |
||
201 | $r = strnatcmp($this->stringValue, $other->stringValue); |
||
202 | } |
||
203 | } else { |
||
204 | $r = 1; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return $r; |
||
209 | } |
||
210 | } |
||
211 |