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