1 | <?php |
||
13 | final class Euro implements \JsonSerializable { |
||
14 | |||
15 | private const DECIMAL_COUNT = 2; |
||
16 | private const CENTS_PER_EURO = 100; |
||
17 | |||
18 | private $cents; |
||
19 | |||
20 | /** |
||
21 | * @param int $cents |
||
22 | * @throws InvalidArgumentException |
||
23 | */ |
||
24 | 56 | private function __construct( int $cents ) { |
|
31 | |||
32 | /** |
||
33 | * @return string |
||
34 | */ |
||
35 | 1 | public function __toString(): string { |
|
38 | |||
39 | /** |
||
40 | * @return string |
||
41 | */ |
||
42 | 1 | public function jsonSerialize(): string { |
|
45 | |||
46 | /** |
||
47 | * @param int $cents |
||
48 | * @return self |
||
49 | * @throws InvalidArgumentException |
||
50 | */ |
||
51 | 32 | public static function newFromCents( int $cents ): self { |
|
54 | |||
55 | /** |
||
56 | * Constructs a Euro object from a string representation such as "13.37". |
||
57 | * |
||
58 | * This method takes into account the errors that can arise from floating |
||
59 | * point number usage. Amounts with too many decimals are rounded to the |
||
60 | * nearest whole euro cent amount. |
||
61 | * |
||
62 | * @param string $euroAmount |
||
63 | * @return self |
||
64 | * @throws InvalidArgumentException |
||
65 | */ |
||
66 | 19 | public static function newFromString( string $euroAmount ): self { |
|
82 | |||
83 | 16 | private static function stringIsTooLong( string $euroString ): bool { |
|
86 | |||
87 | 13 | private static function centsFromString( string $cents ): int { |
|
95 | |||
96 | 2 | private static function roundCentsToInt( string $cents ): int { |
|
105 | |||
106 | /** |
||
107 | * This method takes into account the errors that can arise from floating |
||
108 | * point number usage. Amounts with too many decimals are rounded to the |
||
109 | * nearest whole euro cent amount. |
||
110 | * |
||
111 | * @param float $euroAmount |
||
112 | * @return self |
||
113 | * @throws InvalidArgumentException |
||
114 | */ |
||
115 | 9 | public static function newFromFloat( float $euroAmount ): self { |
|
124 | |||
125 | /** |
||
126 | * @param int|float $euroAmount |
||
127 | */ |
||
128 | 18 | private static function assertMaximumValueNotExceeded( $euroAmount ): void { |
|
137 | |||
138 | /** |
||
139 | * @param int $euroAmount |
||
140 | * @return self |
||
141 | * @throws InvalidArgumentException |
||
142 | */ |
||
143 | 9 | public static function newFromInt( int $euroAmount ): self { |
|
147 | |||
148 | 33 | public function getEuroCents(): int { |
|
151 | |||
152 | 12 | public function getEuroFloat(): float { |
|
155 | |||
156 | /** |
||
157 | * Returns the euro amount as string with two decimals always present in format "42.00". |
||
158 | */ |
||
159 | 8 | public function getEuroString(): string { |
|
162 | |||
163 | 10 | public function equals( Euro $euro ): bool { |
|
166 | |||
167 | } |
||
168 |