1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects) |
7
|
|
|
* |
8
|
|
|
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository |
9
|
|
|
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/) |
10
|
|
|
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace MichaelRubel\ValueObjects\Collection\Primitive; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use MichaelRubel\ValueObjects\Concerns\SanitizesNumbers; |
17
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
18
|
|
|
use PHP\Math\BigNumber\BigNumber; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* "Number" object that represents numeric values. |
22
|
|
|
* |
23
|
|
|
* @author Michael Rubél <[email protected]> |
24
|
|
|
* |
25
|
|
|
* @template TKey of array-key |
26
|
|
|
* @template TValue |
27
|
|
|
* |
28
|
|
|
* @method static static make(int|string $number, int $scale = 2) |
29
|
|
|
* @method static static from(int|string $number, int $scale = 2) |
30
|
|
|
* @method static static makeOrNull(int|string|null $number, int $scale = 2) |
31
|
|
|
* |
32
|
|
|
* @method string abs() |
33
|
|
|
* @method string add(float|int|string $value) |
34
|
|
|
* @method string divide(float|int|string $value) |
35
|
|
|
* @method string multiply(float|int|string $value) |
36
|
|
|
* @method string mod(float|int|string $value) |
37
|
|
|
* @method string pow(int|string $value) |
38
|
|
|
* @method string sqrt() |
39
|
|
|
* @method string subtract(float|int|string $value) |
40
|
|
|
* |
41
|
|
|
* @see \PHP\Math\BigNumber\BigNumber |
42
|
|
|
* |
43
|
|
|
* @extends ValueObject<TKey, TValue> |
44
|
|
|
*/ |
45
|
|
|
class Number extends ValueObject |
46
|
|
|
{ |
47
|
|
|
use SanitizesNumbers; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var BigNumber |
51
|
|
|
*/ |
52
|
|
|
protected BigNumber $bigNumber; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Create a new instance of the value object. |
56
|
|
|
* |
57
|
|
|
* @param int|string $number |
58
|
|
|
* @param int $scale |
59
|
|
|
*/ |
60
|
21 |
|
public function __construct(int|string $number, protected int $scale = 2) |
61
|
|
|
{ |
62
|
21 |
|
if (isset($this->bigNumber)) { |
63
|
1 |
|
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE); |
64
|
|
|
} |
65
|
|
|
|
66
|
21 |
|
$this->bigNumber = new BigNumber( |
67
|
21 |
|
$this->sanitize($number), $this->scale, mutable: false |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the object value. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
11 |
|
public function value(): string |
77
|
|
|
{ |
78
|
11 |
|
return $this->bigNumber->getValue(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get the number as an integer. |
83
|
|
|
* |
84
|
|
|
* @return int |
85
|
|
|
*/ |
86
|
1 |
|
public function asInteger(): int |
87
|
|
|
{ |
88
|
1 |
|
return (int) $this->bigNumber->getValue(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the number as a float. |
93
|
|
|
* |
94
|
|
|
* @return float |
95
|
|
|
*/ |
96
|
1 |
|
public function asFloat(): float |
97
|
|
|
{ |
98
|
1 |
|
return (float) $this->bigNumber->getValue(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the underlying `BigNumber` object. |
103
|
|
|
* |
104
|
|
|
* @return BigNumber |
105
|
|
|
*/ |
106
|
1 |
|
public function asBigNumber(): BigNumber |
107
|
|
|
{ |
108
|
1 |
|
return $this->bigNumber; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Forward call to underlying object if the method |
113
|
|
|
* doesn't exist in `Number` and doesn't have a macro. |
114
|
|
|
* |
115
|
|
|
* @param string $method |
116
|
|
|
* @param array $parameters |
117
|
|
|
* |
118
|
|
|
* @return mixed |
119
|
|
|
*/ |
120
|
4 |
|
public function __call($method, $parameters) |
121
|
|
|
{ |
122
|
4 |
|
if (static::hasMacro($method)) { |
123
|
2 |
|
return parent::__call($method, $parameters); |
124
|
|
|
} |
125
|
|
|
|
126
|
2 |
|
return $this->bigNumber->{$method}(...$parameters)->getValue(); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|