|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Z38\SwissPayment\Money; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Base class for all currencies. |
|
7
|
|
|
*/ |
|
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) |
|
21
|
|
|
{ |
|
22
|
2 |
|
$this->cents = intval($cents); |
|
23
|
2 |
|
} |
|
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() |
|
45
|
|
|
{ |
|
46
|
4 |
|
$base = pow(10, $this->getDecimals()); |
|
47
|
4 |
|
$sign = ($this->cents < 0 ? '-' : ''); |
|
48
|
|
|
|
|
49
|
4 |
|
return sprintf('%s%d.%0'.$this->getDecimals().'d', $sign, intval(abs($this->cents) / $base), abs($this->cents) % $base); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns the amount of money in cents |
|
54
|
|
|
* |
|
55
|
|
|
* @return int The amount in cents |
|
56
|
|
|
*/ |
|
57
|
1 |
|
public function getAmount() |
|
58
|
|
|
{ |
|
59
|
1 |
|
return $this->cents; |
|
60
|
|
|
} |
|
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) |
|
72
|
|
|
{ |
|
73
|
5 |
|
if ($this->getCurrency() !== $addend->getCurrency()) { |
|
74
|
1 |
|
throw new \InvalidArgumentException('Can not add different currencies'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
return new static($this->cents + $addend->getAmount()); |
|
78
|
|
|
} |
|
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) |
|
90
|
|
|
{ |
|
91
|
5 |
|
if ($this->getCurrency() !== $subtrahend->getCurrency()) { |
|
92
|
1 |
|
throw new \InvalidArgumentException('Can not subtract different currencies'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
return new static($this->cents - $subtrahend->getAmount()); |
|
96
|
|
|
} |
|
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) |
|
108
|
|
|
{ |
|
109
|
4 |
|
if ($this->getCurrency() !== $b->getCurrency()) { |
|
110
|
|
|
throw new \InvalidArgumentException('Can not compare different currencies'); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
4 |
|
if ($this->getAmount() < $b->getAmount()) { |
|
114
|
1 |
|
return -1; |
|
115
|
3 |
|
} elseif ($this->getAmount() == $b->getAmount()) { |
|
116
|
1 |
|
return 0; |
|
117
|
|
|
} else { |
|
118
|
2 |
|
return 1; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
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) |
|
130
|
|
|
{ |
|
131
|
1 |
|
if (!($obj instanceof self)) { |
|
132
|
1 |
|
return false; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
1 |
|
if ($this->getCurrency() !== $obj->getCurrency()) { |
|
136
|
1 |
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
1 |
|
return ($this->getAmount() == $obj->getAmount()); |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|