dbunt1tled /
php-value-objects
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace DBUnt1tled\VO\VObjects\Scalar; |
||
| 6 | |||
| 7 | use DBUnt1tled\VO\Exception\InvalidVOArgumentException; |
||
| 8 | use DBUnt1tled\VO\VObjects\ValueObject; |
||
| 9 | |||
| 10 | class Number extends ValueObject |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @param mixed $value |
||
| 14 | * @param mixed ...$other |
||
| 15 | * @throws \ReflectionException |
||
| 16 | */ |
||
| 17 | 2 | public function guard($value, ...$other): void |
|
| 18 | { |
||
| 19 | 2 | parent::guard($value); |
|
| 20 | 2 | if (!is_numeric($value) || is_bool($value)) { |
|
| 21 | 1 | throw new InvalidVOArgumentException('Value is invalid. Allowed type is integer, float, double, real', $value); |
|
| 22 | } |
||
| 23 | 1 | } |
|
| 24 | |||
| 25 | /** |
||
| 26 | * @param string $string |
||
| 27 | * @return Number |
||
| 28 | * @throws \ReflectionException |
||
| 29 | */ |
||
| 30 | 2 | public static function createFromString(string $string): self |
|
| 31 | { |
||
| 32 | 2 | if (filter_var($string, FILTER_VALIDATE_INT) !== false) { |
|
| 33 | 1 | return new static((int)$string); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 34 | } |
||
| 35 | 2 | if (is_numeric($string)) { |
|
| 36 | 1 | return new static((float)$string); |
|
|
0 ignored issues
–
show
|
|||
| 37 | } |
||
| 38 | 1 | throw new InvalidVOArgumentException('Value is invalid. Allowed type is integer, float, double, real', $string); |
|
| 39 | } |
||
| 40 | } |
||
| 41 |