Completed
Branch FET-10619-money-entity (c56734)
by
unknown
157:04 queued 143:03
created

Currency::thousands()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\entities\money;
4
5
use EE_Country;
6
use EE_Error;
7
use EEM_Country;
8
use EventEspresso\core\entities\Label;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
15
/**
16
 * Class Currency
17
 * DTO for data pertaining to a currency
18
 *
19
 * @package       Event Espresso
20
 * @author        Brent Christensen
21
 * @since         $VID:$
22
 */
23
class Currency
24
{
25
26
    /**
27
     * eg 'US'
28
     *
29
     * @var string $code
30
     */
31
    private $code;
32
33
    /**
34
     * @var Label $label
35
     */
36
    private $label;
37
38
    /**
39
     * currency sign
40
     *
41
     * @var string $sign
42
     * eg '$'
43
     */
44
    private $sign;
45
46
    /**
47
     * Whether the currency sign should come before the number or not
48
     *
49
     * @var boolean $sign_b4
50
     */
51
    private $sign_b4;
52
53
    /**
54
     * How many digits should come after the decimal place
55
     *
56
     * @var int $decimal_places
57
     */
58
    private $decimal_places;
59
60
    /**
61
     * Symbol to use for decimal mark
62
     *
63
     * @var string $decimal_mark
64
     * eg '.'
65
     */
66
    private $decimal_mark;
67
68
    /**
69
     * Symbol to use for thousands
70
     *
71
     * @var string $thousands
72
     * eg ','
73
     */
74
    private $thousands;
75
76
77
78
    /**
79
     * Currency constructor.
80
     *
81
     * @param string $code
82
     * @param Label  $label
83
     * @param string $sign
84
     * @param bool   $sign_b4
85
     * @param int    $decimal_places
86
     * @param string $decimal_mark
87
     * @param string $thousands
88
     */
89
    private function __construct($code, Label $label, $sign, $sign_b4, $decimal_places, $decimal_mark, $thousands)
90
    {
91
        $this->code = $code;
92
        $this->label = $label;
93
        $this->sign = $sign;
94
        $this->sign_b4 = $sign_b4;
95
        $this->decimal_places = $decimal_places;
96
        $this->decimal_mark = $decimal_mark;
97
        $this->thousands = $thousands;
98
    }
99
100
101
102
    /**
103
     * returns a Currency object for the supplied country code
104
     *
105
     * @param string $CNT_ISO
106
     * @return Currency
107
     * @throws InvalidArgumentException
108
     */
109 View Code Duplication
    public static function createFromCountryCode($CNT_ISO)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
    {
111
        /** @var EE_Country $country */
112
        $country = EEM_Country::instance()->get_one_by_ID($CNT_ISO);
113
        if (! $country instanceof EE_Country){
114
            throw new InvalidArgumentException(
115
                sprintf(
116
                    esc_html__(
117
                        'A valid country could not be found for the "%1$s" country code;',
118
                        'event_espresso'
119
                    ),
120
                    $CNT_ISO
121
                )
122
            );
123
        }
124
        return new Currency(
125
            $country->currency_code(),
126
            new Label(
127
                $country->currency_name_single(),
128
                $country->currency_name_plural()
129
            ),
130
            $country->currency_sign(),
131
            $country->currency_sign_before(),
132
            $country->currency_decimal_places(),
133
            $country->currency_decimal_mark(),
134
            $country->currency_thousands_separator()
135
        );
136
    }
137
138
139
140
    /**
141
     * returns a Currency object for the supplied currency code
142
     * PLZ NOTE: variations may exist between how different countries display the same currency,
143
     * so it may be necessary to use Currency::createFromCountryCode() to achieve the desired results
144
     *
145
     * @param string $code
146
     * @return Currency
147
     * @throws InvalidArgumentException
148
     * @throws EE_Error
149
     */
150 View Code Duplication
    public static function createFromCode($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
    {
152
        /** @var EE_Country $country */
153
        $country = EEM_Country::instance()->get_one(array(array('CNT_cur_code' => $code)));
154
        if (! $country instanceof EE_Country) {
155
            throw new InvalidArgumentException(
156
                sprintf(
157
                    esc_html__(
158
                        'A valid currency could not be found for the "%1$s" currency code;',
159
                        'event_espresso'
160
                    ),
161
                    $code
162
                )
163
            );
164
        }
165
        return new Currency(
166
            $country->currency_code(),
167
            new Label(
168
                $country->currency_name_single(),
169
                $country->currency_name_plural()
170
            ),
171
            $country->currency_sign(),
172
            $country->currency_sign_before(),
173
            $country->currency_decimal_places(),
174
            $country->currency_decimal_mark(),
175
            $country->currency_thousands_separator()
176
        );
177
    }
178
179
180
181
    /**
182
     * returns true if this currency is the same as the supplied currency
183
     *
184
     * @param Currency $other
185
     * @return bool
186
     */
187
    public function equals(Currency $other){
188
        return $this->code() === $other->code();
189
    }
190
191
192
193
    /**
194
     * @return string
195
     */
196
    public function code()
197
    {
198
        return $this->code;
199
    }
200
201
202
203
    /**
204
     * @return string
205
     */
206
    public function name()
207
    {
208
        return $this->label->singular();
209
    }
210
211
212
213
    /**
214
     * @return string
215
     */
216
    public function plural()
217
    {
218
        return $this->label->plural();
219
    }
220
221
222
223
    /**
224
     * @return string
225
     */
226
    public function sign()
227
    {
228
        return $this->sign;
229
    }
230
231
232
233
    /**
234
     * @return bool
235
     */
236
    public function signB4()
237
    {
238
        return $this->sign_b4;
239
    }
240
241
242
243
    /**
244
     * @return int
245
     */
246
    public function decimalPlaces()
247
    {
248
        return $this->decimal_places;
249
    }
250
251
252
253
    /**
254
     * @return string
255
     */
256
    public function decimalMark()
257
    {
258
        return $this->decimal_mark;
259
    }
260
261
262
263
    /**
264
     * @return string
265
     */
266
    public function thousands()
267
    {
268
        return $this->thousands;
269
    }
270
271
272
273
    /**
274
     * @return string
275
     */
276
    public function __toString()
277
    {
278
        return $this->code();
279
    }
280
281
282
283
}
284
// End of file Currency.php
285
// Location: core/entities/money/Currency.php