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 | 2 | public function __construct($cents) |
|
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() |
|
51 | |||
52 | /** |
||
53 | * Returns the amount of money in cents |
||
54 | * |
||
55 | * @return int The amount in cents |
||
56 | */ |
||
57 | 1 | public function getAmount() |
|
61 | |||
62 | /** |
||
63 | * Returns the sum of this and an other amount of money |
||
64 | * |
||
65 | * @param Money $addend The addend |
||
66 | * |
||
67 | * @return Money The sum |
||
68 | * |
||
69 | * @throws \InvalidArgumentException When the currencies do not match |
||
70 | */ |
||
71 | 5 | public function plus(Money $addend) |
|
79 | |||
80 | /** |
||
81 | * Returns the subtraction of this and an other amount of money |
||
82 | * |
||
83 | * @param Money $subtrahend The subtrahend |
||
84 | * |
||
85 | * @return Money The difference |
||
86 | * |
||
87 | * @throws \InvalidArgumentException When the currencies do not match |
||
88 | */ |
||
89 | 5 | public function minus(Money $subtrahend) |
|
97 | |||
98 | /** |
||
99 | * Compares this instance with an other instance. |
||
100 | * |
||
101 | * @param Money $b The instance to which this instance is to be compared. |
||
102 | * |
||
103 | * @return int -1, 0 or 1 as this instance is less than, equal to, or greater than $b |
||
104 | * |
||
105 | * @throws \InvalidArgumentException When the currencies do not match |
||
106 | */ |
||
107 | 4 | public function compareTo(Money $b) |
|
121 | |||
122 | /** |
||
123 | * Returns true if the argument contains the same amount and the same currency. |
||
124 | * |
||
125 | * @param object $obj |
||
126 | * |
||
127 | * @return bool True if $obj is equal to this instance |
||
128 | */ |
||
129 | 1 | public function equals($obj) |
|
141 | } |
||
142 |