1 | <?php |
||
2 | |||
3 | namespace ConferenceTools\Tickets\Domain\ValueObject; |
||
4 | |||
5 | use JMS\Serializer\Annotation as Jms; |
||
6 | use Doctrine\ORM\Mapping as ORM; |
||
7 | |||
8 | /** |
||
9 | * Class Money |
||
10 | * @ORM\Embeddable() |
||
11 | */ |
||
12 | final class Money |
||
13 | { |
||
14 | /** |
||
15 | * @JMS\Type("integer") |
||
16 | * @ORM\Column(type="integer") |
||
17 | */ |
||
18 | private $amount; |
||
19 | |||
20 | /** |
||
21 | * @JMS\Type("string") |
||
22 | * @ORM\Column(type="string") |
||
23 | */ |
||
24 | private $currency; |
||
25 | |||
26 | /** |
||
27 | * Money constructor. |
||
28 | * @param $amount |
||
29 | * @param $currency |
||
30 | */ |
||
31 | 53 | public function __construct(int $amount, string $currency) |
|
32 | { |
||
33 | 53 | $this->amount = $amount; |
|
34 | 53 | $this->currency = $currency; |
|
35 | 53 | } |
|
36 | |||
37 | /** |
||
38 | * @return integer |
||
39 | */ |
||
40 | 1 | public function getAmount(): int |
|
41 | { |
||
42 | 1 | return $this->amount; |
|
43 | } |
||
44 | |||
45 | /** |
||
46 | * @return string |
||
47 | */ |
||
48 | 8 | public function getCurrency(): string |
|
49 | { |
||
50 | 8 | return $this->currency; |
|
51 | } |
||
52 | |||
53 | 1 | public static function __callStatic(string $method, array $arguments): Money |
|
54 | { |
||
55 | 1 | return new self($arguments[0], $method); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param Money $other |
||
60 | * @return bool |
||
61 | */ |
||
62 | 46 | public function isSameCurrency(Money $other): bool |
|
63 | { |
||
64 | 46 | return $this->currency === $other->currency; |
|
65 | } |
||
66 | |||
67 | /** |
||
68 | * @throws \InvalidArgumentException |
||
69 | */ |
||
70 | 33 | private function assertSameCurrency(Money $other) |
|
71 | { |
||
72 | 33 | if (!$this->isSameCurrency($other)) { |
|
73 | 1 | throw new \InvalidArgumentException('Different currencies'); |
|
74 | } |
||
75 | 32 | } |
|
76 | |||
77 | 31 | public function equals(Money $other): bool |
|
78 | { |
||
79 | 31 | return ($this->isSameCurrency($other) && $other->amount === $this->amount); |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * @param Money $other |
||
84 | * @return int |
||
85 | */ |
||
86 | 13 | public function compare(Money $other): int |
|
87 | { |
||
88 | 13 | $this->assertSameCurrency($other); |
|
89 | 12 | if ($this->amount < $other->amount) { |
|
90 | 10 | return -1; |
|
91 | 12 | } elseif ($this->amount == $other->amount) { |
|
92 | 2 | return 0; |
|
93 | } else { |
||
94 | 10 | return 1; |
|
95 | } |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * @param Money $other |
||
100 | * @return bool |
||
101 | */ |
||
102 | 1 | public function greaterThan(Money $other): bool |
|
103 | { |
||
104 | 1 | return 1 === $this->compare($other); |
|
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param Money $other |
||
109 | * @return bool |
||
110 | */ |
||
111 | 7 | public function lessThan(Money $other): bool |
|
112 | { |
||
113 | 7 | return -1 === $this->compare($other); |
|
114 | } |
||
115 | |||
116 | 19 | public function add(Money $addend): Money |
|
117 | { |
||
118 | 19 | $this->assertSameCurrency($addend); |
|
119 | |||
120 | 19 | return new self($this->amount + $addend->amount, $this->currency); |
|
121 | } |
||
122 | |||
123 | 6 | public function subtract(Money $subtrahend): Money |
|
124 | { |
||
125 | 6 | $this->assertSameCurrency($subtrahend); |
|
126 | |||
127 | 6 | return new self($this->amount - $subtrahend->amount, $this->currency); |
|
128 | } |
||
129 | |||
130 | 15 | public function multiply($multiple): Money |
|
131 | { |
||
132 | 15 | return new self(ceil($this->amount * $multiple), $this->currency); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
133 | } |
||
134 | } |
||
135 |