1 | <?php |
||
8 | abstract class Money |
||
9 | { |
||
10 | /** |
||
11 | * @var int |
||
12 | */ |
||
13 | protected $cents; |
||
14 | |||
15 | /** |
||
16 | * Constructor |
||
17 | * |
||
18 | * @param int $cents Amount of money in cents |
||
19 | */ |
||
20 | 3 | public function __construct($cents) |
|
21 | { |
||
22 | 3 | $this->cents = is_int($cents) ? $cents : intval(round($cents)); |
|
23 | 3 | } |
|
24 | |||
25 | /** |
||
26 | * Gets the currency code |
||
27 | * |
||
28 | * @return string|null An ISO 4217 currency code or null if currency is not known |
||
29 | */ |
||
30 | abstract public function getCurrency(); |
||
31 | |||
32 | /** |
||
33 | * Gets the number of decimals |
||
34 | * |
||
35 | * @return int |
||
36 | */ |
||
37 | abstract protected function getDecimals(); |
||
38 | |||
39 | /** |
||
40 | * Returns a formatted string (e.g. 15.560) |
||
41 | * |
||
42 | * @return string The formatted value |
||
43 | */ |
||
44 | 4 | public function format() |
|
57 | |||
58 | /** |
||
59 | * Returns the amount of money in cents |
||
60 | * |
||
61 | * @return int The amount in cents |
||
62 | */ |
||
63 | 3 | public function getAmount() |
|
67 | |||
68 | /** |
||
69 | * Returns the sum of this and an other amount of money |
||
70 | * |
||
71 | * @param Money $addend The addend |
||
72 | * |
||
73 | * @return Money The sum |
||
74 | * |
||
75 | * @throws \InvalidArgumentException When the currencies do not match |
||
76 | */ |
||
77 | 5 | public function plus(self $addend) |
|
85 | |||
86 | /** |
||
87 | * Returns the subtraction of this and an other amount of money |
||
88 | * |
||
89 | * @param Money $subtrahend The subtrahend |
||
90 | * |
||
91 | * @return Money The difference |
||
92 | * |
||
93 | * @throws \InvalidArgumentException When the currencies do not match |
||
94 | */ |
||
95 | 5 | public function minus(self $subtrahend) |
|
103 | |||
104 | /** |
||
105 | * Compares this instance with an other instance. |
||
106 | * |
||
107 | * @param Money $b The instance to which this instance is to be compared. |
||
108 | * |
||
109 | * @return int -1, 0 or 1 as this instance is less than, equal to, or greater than $b |
||
110 | * |
||
111 | * @throws \InvalidArgumentException When the currencies do not match |
||
112 | */ |
||
113 | 4 | public function compareTo(self $b) |
|
127 | |||
128 | /** |
||
129 | * Returns true if the argument contains the same amount and the same currency. |
||
130 | * |
||
131 | * @param object $obj |
||
132 | * |
||
133 | * @return bool True if $obj is equal to this instance |
||
134 | */ |
||
135 | 1 | public function equals($obj) |
|
147 | } |
||
148 |