1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Currency |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2023 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Money |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Money; |
12
|
|
|
|
13
|
|
|
use JsonSerializable; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Currency |
17
|
|
|
* |
18
|
|
|
* @author Remco Tolsma |
19
|
|
|
* @version 2.0.0 |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
class Currency implements JsonSerializable { |
23
|
|
|
/** |
24
|
|
|
* Alphabetic code. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $alphabetic_code; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Numeric code. |
32
|
|
|
* |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
private $numeric_code; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Symbol. |
39
|
|
|
* |
40
|
|
|
* @var string|null |
41
|
|
|
*/ |
42
|
|
|
private $symbol; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Name. |
46
|
|
|
* |
47
|
|
|
* @var string|null |
48
|
|
|
*/ |
49
|
|
|
private $name; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Number decimals. |
53
|
|
|
* |
54
|
|
|
* @var int |
55
|
|
|
*/ |
56
|
|
|
private $number_decimals; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Construct and initialize currency object. |
60
|
|
|
* |
61
|
|
|
* @param string $alphabetic_code Alphabetic currency code. |
62
|
|
|
* @param string|null $numeric_code Numeric code. |
63
|
|
|
* @param string|null $name Name. |
64
|
|
|
* @param string|null $symbol Symbol. |
65
|
|
|
* @param int $number_decimals Number decimals. |
66
|
|
|
*/ |
67
|
2 |
|
public function __construct( $alphabetic_code, $numeric_code = null, $name = null, $symbol = null, $number_decimals = 2 ) { |
68
|
2 |
|
$this->set_alphabetic_code( $alphabetic_code ); |
69
|
1 |
|
$this->set_numeric_code( $numeric_code ); |
70
|
1 |
|
$this->set_name( $name ); |
71
|
1 |
|
$this->set_symbol( $symbol ); |
72
|
1 |
|
$this->set_number_decimals( $number_decimals ); |
73
|
1 |
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get alphabetic code. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
16 |
|
public function get_alphabetic_code() { |
81
|
16 |
|
return $this->alphabetic_code; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set alphabetic code. |
86
|
|
|
* |
87
|
|
|
* @param string $alphabetic_code Alphabetic code. |
88
|
|
|
* @return void |
89
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception if code is not 3 characters. |
90
|
|
|
*/ |
91
|
2 |
|
final public function set_alphabetic_code( $alphabetic_code ) { |
92
|
2 |
|
if ( 3 !== \strlen( $alphabetic_code ) ) { |
93
|
1 |
|
throw new \InvalidArgumentException( |
94
|
1 |
|
\sprintf( |
95
|
1 |
|
'The alphabetical code of a currency must consist of 3 characters: %s.', |
96
|
1 |
|
$alphabetic_code |
97
|
|
|
) |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
$this->alphabetic_code = $alphabetic_code; |
102
|
1 |
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Get numeric code. |
106
|
|
|
* |
107
|
|
|
* @return string|null |
108
|
|
|
*/ |
109
|
1 |
|
public function get_numeric_code() { |
110
|
1 |
|
return $this->numeric_code; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Set numeric code. |
115
|
|
|
* |
116
|
|
|
* @param string|null $numeric_code Numeric code. |
117
|
|
|
* @return void |
118
|
|
|
*/ |
119
|
1 |
|
public function set_numeric_code( $numeric_code ) { |
120
|
1 |
|
$this->numeric_code = $numeric_code; |
121
|
1 |
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get symbol. |
125
|
|
|
* |
126
|
|
|
* @return string|null |
127
|
|
|
*/ |
128
|
14 |
|
public function get_symbol() { |
129
|
14 |
|
return $this->symbol; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Set symbol. |
134
|
|
|
* |
135
|
|
|
* @param string|null $symbol Symbol. |
136
|
|
|
* @return void |
137
|
|
|
*/ |
138
|
1 |
|
public function set_symbol( $symbol ) { |
139
|
1 |
|
$this->symbol = $symbol; |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get number decimals. |
144
|
|
|
* |
145
|
|
|
* @return int |
146
|
|
|
*/ |
147
|
39 |
|
public function get_number_decimals() { |
148
|
39 |
|
return $this->number_decimals; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Set number decimals. |
153
|
|
|
* |
154
|
|
|
* @param int $number_decimals Number of decimals. |
155
|
|
|
* @return void |
156
|
|
|
*/ |
157
|
1 |
|
final public function set_number_decimals( $number_decimals ) { |
158
|
1 |
|
$this->number_decimals = intval( $number_decimals ); |
159
|
1 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get instance. |
163
|
|
|
* |
164
|
|
|
* @param string $alphabetic_code Alphabetic code. |
165
|
|
|
* |
166
|
|
|
* @return Currency |
167
|
|
|
*/ |
168
|
101 |
|
public static function get_instance( $alphabetic_code ) { |
169
|
101 |
|
return Currencies::get_currency( $alphabetic_code ); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* Get name. |
174
|
|
|
* |
175
|
|
|
* @return string|null |
176
|
|
|
*/ |
177
|
1 |
|
public function get_name() { |
178
|
1 |
|
return $this->name; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Set name. |
183
|
|
|
* |
184
|
|
|
* @param string|null $name Currency name. |
185
|
|
|
* @return void |
186
|
|
|
*/ |
187
|
1 |
|
public function set_name( $name ) { |
188
|
1 |
|
$this->name = $name; |
189
|
1 |
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* JSON serialize. |
193
|
|
|
* |
194
|
|
|
* @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php |
195
|
|
|
* @return string |
196
|
|
|
*/ |
197
|
3 |
|
public function jsonSerialize(): string { |
198
|
3 |
|
return $this->alphabetic_code; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|