1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventEspresso\core\domain\values\currency; |
4
|
|
|
|
5
|
|
|
use EventEspresso\core\entities\Label; |
6
|
|
|
|
7
|
|
|
defined('EVENT_ESPRESSO_VERSION') || exit; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Currency |
13
|
|
|
* DTO for data pertaining to a currency |
14
|
|
|
* |
15
|
|
|
* @package Event Espresso |
16
|
|
|
* @author Brent Christensen |
17
|
|
|
* @since $VID:$ |
18
|
|
|
*/ |
19
|
|
|
class Currency |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* eg 'US' |
24
|
|
|
* |
25
|
|
|
* @var string $code |
26
|
|
|
*/ |
27
|
|
|
private $code; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Label $label |
31
|
|
|
*/ |
32
|
|
|
private $label; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* currency sign |
36
|
|
|
* |
37
|
|
|
* @var string $sign |
38
|
|
|
* eg '$' |
39
|
|
|
*/ |
40
|
|
|
private $sign; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Whether the currency sign should come before the amount or not |
44
|
|
|
* |
45
|
|
|
* @var boolean $sign_b4 |
46
|
|
|
*/ |
47
|
|
|
private $sign_b4; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Space (or nothing) displayed between currency sign and amount |
51
|
|
|
* |
52
|
|
|
* @var string $sign_separator |
53
|
|
|
*/ |
54
|
|
|
private $sign_separator; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* How many digits should come after the decimal place |
58
|
|
|
* Although not theoretically true, it can effectively |
59
|
|
|
* be considered that all currencies are decimal based. |
60
|
|
|
* Therefore the number of decimal places can be used |
61
|
|
|
* to calculate number of subunits like so: |
62
|
|
|
* subunits = pow( 10, decimal places ) |
63
|
|
|
* |
64
|
|
|
* @var int $decimal_places |
65
|
|
|
*/ |
66
|
|
|
private $decimal_places; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Symbol to use for decimal mark |
70
|
|
|
* |
71
|
|
|
* @var string $decimal_mark |
72
|
|
|
* eg '.' |
73
|
|
|
*/ |
74
|
|
|
private $decimal_mark; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Symbol to use for thousands |
78
|
|
|
* |
79
|
|
|
* @var string $thousands |
80
|
|
|
* eg ',' |
81
|
|
|
*/ |
82
|
|
|
private $thousands; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* The number of fractional divisions of a currency's main unit |
86
|
|
|
* Can be used to determine the number of decimal places used. |
87
|
|
|
* Because |
88
|
|
|
* subunits = pow( 10, decimal places ) |
89
|
|
|
* then |
90
|
|
|
* decimal places = log( subunits ) |
91
|
|
|
* except that a result of 1 means there are zero decimal places |
92
|
|
|
* |
93
|
|
|
* @var int |
94
|
|
|
*/ |
95
|
|
|
private $subunits; |
96
|
|
|
|
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Currency constructor. |
100
|
|
|
* |
101
|
|
|
* @param string $code |
102
|
|
|
* @param Label $label |
103
|
|
|
* @param string $sign |
104
|
|
|
* @param bool $sign_b4 |
105
|
|
|
* @param int $decimal_places the number of decimal places to use when DISPLAYING the currency |
106
|
|
|
* @param string $decimal_mark |
107
|
|
|
* @param string $thousands |
108
|
|
|
* @param int $subunits number of fractional divisions of a currency's main unit |
109
|
|
|
* @param string $sign_separator |
110
|
|
|
*/ |
111
|
|
|
public function __construct( |
112
|
|
|
$code, |
113
|
|
|
Label $label, |
114
|
|
|
$sign, |
115
|
|
|
$sign_b4, |
116
|
|
|
$decimal_places, |
117
|
|
|
$decimal_mark, |
118
|
|
|
$thousands, |
119
|
|
|
$subunits, |
120
|
|
|
$sign_separator = '' |
121
|
|
|
) { |
122
|
|
|
$this->code = $code; |
123
|
|
|
$this->label = $label; |
124
|
|
|
$this->sign = $sign; |
125
|
|
|
$this->sign_b4 = $sign_b4; |
126
|
|
|
$this->decimal_places = $decimal_places; |
127
|
|
|
$this->decimal_mark = $decimal_mark; |
128
|
|
|
$this->thousands = $thousands; |
129
|
|
|
$this->subunits = $subunits; |
130
|
|
|
$this->sign_separator = $sign_separator; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* returns true if this currency is the same as the supplied currency |
137
|
|
|
* |
138
|
|
|
* @param Currency $other |
139
|
|
|
* @return bool |
140
|
|
|
*/ |
141
|
|
|
public function equals(Currency $other) |
142
|
|
|
{ |
143
|
|
|
return $this->code() === $other->code(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
|
|
public function code() |
152
|
|
|
{ |
153
|
|
|
return $this->code; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
|
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return string |
160
|
|
|
*/ |
161
|
|
|
public function name() |
162
|
|
|
{ |
163
|
|
|
return $this->label->singular(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
|
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return string |
170
|
|
|
*/ |
171
|
|
|
public function plural() |
172
|
|
|
{ |
173
|
|
|
return $this->label->plural(); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
|
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @return string |
180
|
|
|
*/ |
181
|
|
|
public function sign() |
182
|
|
|
{ |
183
|
|
|
return $this->sign; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return bool |
190
|
|
|
*/ |
191
|
|
|
public function signB4() |
192
|
|
|
{ |
193
|
|
|
return $this->sign_b4; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* @return string |
199
|
|
|
*/ |
200
|
|
|
public function signSeparator() |
201
|
|
|
{ |
202
|
|
|
return $this->sign_separator; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
|
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return int |
209
|
|
|
*/ |
210
|
|
|
public function decimalPlaces() |
211
|
|
|
{ |
212
|
|
|
return $this->decimal_places; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* @return string |
219
|
|
|
*/ |
220
|
|
|
public function decimalMark() |
221
|
|
|
{ |
222
|
|
|
return $this->decimal_mark; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
|
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return string |
229
|
|
|
*/ |
230
|
|
|
public function thousands() |
231
|
|
|
{ |
232
|
|
|
return $this->thousands; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* The number of divisions of the currency's main unit that comprises the smallest units |
238
|
|
|
* ex: 1 US Dollar has 100 Pennies, so USD subunits = 100 |
239
|
|
|
* **WARNING** |
240
|
|
|
* Some currencies, such as the Japanese Yen have no subunits, |
241
|
|
|
* ie: the main unit is the smallest division |
242
|
|
|
* so you need to always check that subunits is not zero |
243
|
|
|
* before performing multiplication or division with it |
244
|
|
|
* |
245
|
|
|
* @return int |
246
|
|
|
*/ |
247
|
|
|
public function subunits() |
248
|
|
|
{ |
249
|
|
|
return $this->subunits; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
|
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
public function __toString() |
258
|
|
|
{ |
259
|
|
|
return $this->code(); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
|
263
|
|
|
|
264
|
|
|
} |
265
|
|
|
// End of file Currency.php |
266
|
|
|
// Location: core/entities/money/Currency.php |
267
|
|
|
|