-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmount.php
133 lines (109 loc) · 3.54 KB
/
Amount.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
129
130
131
132
133
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Money;
use SonsOfPHP\Component\Money\Operator\Amount\AddAmountOperator;
use SonsOfPHP\Component\Money\Operator\Amount\DivideAmountOperator;
use SonsOfPHP\Component\Money\Operator\Amount\MultiplyAmountOperator;
use SonsOfPHP\Component\Money\Operator\Amount\SubtractAmountOperator;
use SonsOfPHP\Component\Money\Query\Amount\IsEqualToAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsGreaterThanAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsGreaterThanOrEqualToAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsLessThanAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsLessThanOrEqualToAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsNegativeAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsPositiveAmountQuery;
use SonsOfPHP\Component\Money\Query\Amount\IsZeroAmountQuery;
use SonsOfPHP\Contract\Money\AmountInterface;
use SonsOfPHP\Contract\Money\AmountOperatorInterface;
use SonsOfPHP\Contract\Money\AmountQueryInterface;
use Stringable;
/**
* @author Joshua Estes <[email protected]>
*/
final class Amount implements AmountInterface, Stringable
{
private readonly string $amount;
public function __construct($amount)
{
$this->amount = (string) $amount;
}
/**
* @see AmountInterface::toString()
*/
public function __toString(): string
{
return $this->toString();
}
public function toString(): string
{
return $this->amount;
}
public function toInt(): int
{
return (int) $this->amount;
}
public function toFloat(): float
{
return (float) $this->amount;
}
public function getAmount(): string
{
return $this->amount;
}
public function with(AmountOperatorInterface $operator): static
{
return $operator->apply($this);
}
public function query(AmountQueryInterface $query)/*: mixed*/
{
return $query->queryFrom($this);
}
public function add(AmountInterface $amount): static
{
return $this->with(new AddAmountOperator($amount));
}
public function subtract(AmountInterface $amount): static
{
return $this->with(new SubtractAmountOperator($amount));
}
public function multiply($multiplier): static
{
return $this->with(new MultiplyAmountOperator($multiplier));
}
public function divide($divisor): static
{
return $this->with(new DivideAmountOperator($divisor));
}
public function isEqualTo(AmountInterface $amount): bool
{
return $this->query(new IsEqualToAmountQuery($amount));
}
public function isGreaterThan(AmountInterface $amount): bool
{
return $this->query(new IsGreaterThanAmountQuery($amount));
}
public function isGreaterThanOrEqualTo(AmountInterface $amount): bool
{
return $this->query(new IsGreaterThanOrEqualToAmountQuery($amount));
}
public function isLessThan(AmountInterface $amount): bool
{
return $this->query(new IsLessThanAmountQuery($amount));
}
public function isLessThanOrEqualTo(AmountInterface $amount): bool
{
return $this->query(new IsLessThanOrEqualToAmountQuery($amount));
}
public function isNegative(): bool
{
return $this->query(new IsNegativeAmountQuery());
}
public function isPositive(): bool
{
return $this->query(new IsPositiveAmountQuery());
}
public function isZero(): bool
{
return $this->query(new IsZeroAmountQuery());
}
}