Completed
Branch FET-10619-money-entity (ff52f2)
by
unknown
155:11 queued 144:22
created
core/entities/money/Money.php 1 patch
Indentation   +413 added lines, -413 removed lines patch added patch discarded remove patch
@@ -30,419 +30,419 @@
 block discarded – undo
30 30
 class Money
31 31
 {
32 32
 
33
-    /**
34
-     * number of decimal places to be added to currencies for internal computations,
35
-     * but removed before any output or formatting is applied.
36
-     * This allows us to avoid rounding errors during calculations.
37
-     */
38
-    const EXTRA_PRECISION = 3;
39
-
40
-    /**
41
-     * @var int $amount
42
-     */
43
-    private $amount;
44
-
45
-    /**
46
-     * @var Currency $currency
47
-     */
48
-    private $currency;
49
-
50
-    /**
51
-     * @var Calculator $calculator
52
-     */
53
-    protected static $calculator;
54
-
55
-    /**
56
-     * @var MoneyFormatter[] $formatters
57
-     */
58
-    protected static $formatters;
59
-
60
-
61
-
62
-    /**
63
-     * Money constructor.
64
-     *
65
-     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
66
-     *                                 example: $12.5 USD would equate to a value amount of 12.50
67
-     * @param Currency         $currency
68
-     * @throws InvalidDataTypeException
69
-     */
70
-    public function __construct($amount, Currency $currency)
71
-    {
72
-        $this->currency = $currency;
73
-        $this->amount = (string)$this->parseAmount($amount);
74
-    }
75
-
76
-
77
-
78
-    /**
79
-     * factory method that returns a Money object using the currency corresponding to the site's country
80
-     * example: Money::forSite(12.5)
81
-     *
82
-     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
83
-     *                                 example: $12.5 USD would equate to a value amount of 12.50
84
-     * @return Money
85
-     * @throws InvalidArgumentException
86
-     */
87
-    public static function forSite($amount)
88
-    {
89
-        $CNT_ISO = isset(EE_Registry::instance()->CFG->organization)
90
-                   && EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config
91
-            ? EE_Registry::instance()->CFG->organization->CNT_ISO
92
-            : 'US';
93
-        return new self($amount, Currency::createFromCountryCode($CNT_ISO));
94
-    }
95
-
96
-
97
-
98
-    /**
99
-     * factory method that returns a Money object using the currency as specified by the supplied ISO country code
100
-     * example: Money::forCountry(12.5,'US')
101
-     *
102
-     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
103
-     *                                 example: $12.5 USD would equate to a value amount of 12.50
104
-     * @param string           $CNT_ISO
105
-     * @return Money
106
-     * @throws InvalidArgumentException
107
-     */
108
-    public static function forCountry($amount, $CNT_ISO)
109
-    {
110
-        return new self($amount, Currency::createFromCountryCode($CNT_ISO));
111
-    }
112
-
113
-
114
-
115
-    /**
116
-     * factory method that returns a Money object using the currency as specified by the supplied currency code
117
-     * example: Money::forCurrency(12.5, 'USD')
118
-     *
119
-     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
120
-     *                                 example: $12.5 USD would equate to a value amount of 12.50
121
-     * @param string           $currency_code
122
-     * @return Money
123
-     * @throws InvalidDataTypeException
124
-     * @throws EE_Error
125
-     * @throws InvalidArgumentException
126
-     */
127
-    public static function forCurrency($amount, $currency_code)
128
-    {
129
-        return new self($amount, Currency::createFromCode($currency_code));
130
-    }
131
-
132
-
133
-
134
-    /**
135
-     * factory method that returns a Money object for the currency specified as if it were a class method
136
-     * example: Money::USD(12.5);
137
-     * money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
138
-     * example: $12.5 USD would equate to a value amount of 12.50
139
-     *
140
-     * @param string $currency_code
141
-     * @param array  $arguments
142
-     * @return Money
143
-     * @throws InvalidDataTypeException
144
-     * @throws EE_Error
145
-     * @throws InvalidArgumentException
146
-     */
147
-    public static function __callStatic($currency_code, $arguments)
148
-    {
149
-        return new self($arguments[0], Currency::createFromCode($currency_code));
150
-    }
151
-
152
-
153
-
154
-    /**
155
-     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
156
-     *                                 example: $12.5 USD would equate to a value amount of 12.50
157
-     * @return float|int|number|string
158
-     * @throws InvalidDataTypeException
159
-     */
160
-    private function parseAmount($amount)
161
-    {
162
-        $type = gettype($amount);
163
-        switch ($type) {
164
-            case 'integer' :
165
-            case 'double' :
166
-            case 'string' :
167
-                // shift the decimal position by the number of decimal places used internally
168
-                // ex: 12.5 for a currency using 2 decimal places, would become 1250
169
-                // then if our extra internal precision was 3, it would become 1250000
170
-                $amount *= pow(10, $this->precision());
171
-                // then round up the remaining value if there is still a fractional amount left
172
-                $amount = round($amount, 0, PHP_ROUND_HALF_UP);
173
-            break;
174
-            default  :
175
-                throw new InvalidDataTypeException(
176
-                    '$amount',
177
-                    $amount,
178
-                    'integer (or float or string)'
179
-                );
180
-        }
181
-        return $amount;
182
-    }
183
-
184
-
185
-
186
-    /**
187
-     * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant
188
-     *
189
-     * @param bool $positive
190
-     * @return int
191
-     */
192
-    private function precision($positive = true)
193
-    {
194
-        $sign = $positive ? 1 : -1;
195
-        return ((int)$this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign;
196
-    }
197
-
198
-
199
-
200
-    /**
201
-     * Returns the money amount as an unformatted string
202
-     * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format()
203
-     *
204
-     * @return string
205
-     */
206
-    public function amount()
207
-    {
208
-        // shift the decimal position BACK by the number of decimal places used internally
209
-        // ex: 1250 for a currency using 2 decimal places, would become 12.5
210
-        $amount = (string)$this->amount * pow(10, $this->precision(false));
211
-        // then shave off our extra internal precision using the number of decimal places for the currency
212
-        $amount = round($amount, $this->currency->decimalPlaces());
213
-        return $amount;
214
-    }
215
-
216
-
217
-
218
-    /**
219
-     * applies formatting based on the specified formatting level
220
-     * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter
221
-     *
222
-     * @param int $formatting_level
223
-     * @return string
224
-     */
225
-    public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS)
226
-    {
227
-        $formatted_amount = $this->amount();
228
-        $formatters = $this->formatters();
229
-        // if we are applying thousands formatting...
230
-        if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) {
231
-            // then let's remove decimal formatting since it's included in thousands formatting
232
-            unset($formatters[MoneyFormatter::DECIMAL_ONLY]);
233
-        }
234
-        for ($x = 1; $x <= $formatting_level; $x++) {
235
-            if (isset($formatters[$x]) && $formatters[$x] instanceof MoneyFormatter) {
236
-                $formatted_amount = $formatters[$x]->format($formatted_amount, $this->currency);
237
-            }
238
-        }
239
-        return $formatted_amount;
240
-    }
241
-
242
-
243
-
244
-    /**
245
-     * Returns the Currency object for this money
246
-     *
247
-     * @return Currency
248
-     */
249
-    public function currency()
250
-    {
251
-        return $this->currency;
252
-    }
253
-
254
-
255
-
256
-    /**
257
-     * adds the supplied Money amount to this Money amount
258
-     * and returns a new Money object
259
-     *
260
-     * @param Money $other
261
-     * @return Money
262
-     * @throws InvalidArgumentException
263
-     */
264
-    public function add(Money $other)
265
-    {
266
-        $this->verifySameCurrency($other->currency());
267
-        return new Money(
268
-            $this->calculator()->add(
269
-                $this->amount(),
270
-                $other->amount()
271
-            ),
272
-            $this->currency()
273
-        );
274
-    }
275
-
276
-
277
-
278
-    /**
279
-     * subtracts the supplied Money amount from this Money amount
280
-     * and returns a new Money object
281
-     *
282
-     * @param Money $other
283
-     * @return Money
284
-     * @throws InvalidArgumentException
285
-     */
286
-    public function subtract(Money $other)
287
-    {
288
-        $this->verifySameCurrency($other->currency());
289
-        return new Money(
290
-            $this->calculator()->subtract(
291
-                $this->amount(),
292
-                $other->amount()
293
-            ),
294
-            $this->currency()
295
-        );
296
-    }
297
-
298
-
299
-
300
-    /**
301
-     * multiplies this Money amount by the supplied $multiplier
302
-     * and returns a new Money object
303
-     *
304
-     * @param float|int|string $multiplier
305
-     * @param int              $rounding_mode
306
-     * @return Money
307
-     * @throws InvalidDataTypeException
308
-     */
309
-    public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP)
310
-    {
311
-        return new Money(
312
-            $this->calculator()->multiply(
313
-                $this->amount(),
314
-                $multiplier,
315
-                $this->precision(),
316
-                $rounding_mode
317
-            ),
318
-            $this->currency()
319
-        );
320
-    }
321
-
322
-
323
-
324
-    /**
325
-     * divides this Money amount by the supplied $divisor
326
-     * and returns a new Money object
327
-     *
328
-     * @param float|int|string $divisor
329
-     * @param int              $rounding_mode
330
-     * @return Money
331
-     * @throws InvalidDataTypeException
332
-     */
333
-    public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP)
334
-    {
335
-        return new Money(
336
-            $this->calculator()->divide(
337
-                $this->amount(),
338
-                $divisor,
339
-                $this->precision(),
340
-                $rounding_mode
341
-            ),
342
-            $this->currency()
343
-        );
344
-    }
345
-
346
-
347
-
348
-    /**
349
-     * @param Currency $other_currency
350
-     * @throws InvalidArgumentException
351
-     */
352
-    public function verifySameCurrency(Currency $other_currency)
353
-    {
354
-        if ($this->currency()->equals($other_currency) !== true) {
355
-            throw new InvalidArgumentException(
356
-                esc_html__(
357
-                    'Currencies must be the same in order to add or subtract their values.',
358
-                    'event_espresso'
359
-                )
360
-            );
361
-        }
362
-    }
363
-
364
-
365
-
366
-    /**
367
-     * @return Calculator
368
-     */
369
-    protected function calculator()
370
-    {
371
-        $this->initializeCalculators();
372
-        return self::$calculator;
373
-    }
374
-
375
-
376
-
377
-    /**
378
-     * loops through a filterable array of Calculator services
379
-     * and selects the first one that is supported by the current server
380
-     */
381
-    protected function initializeCalculators()
382
-    {
383
-        if (self::$calculator instanceof Calculator) {
384
-            return;
385
-        }
386
-        $calculators = apply_filters(
387
-            'FHEE__EventEspresso\core\entities\money\Money__initializeCalculators__Calculator_array',
388
-            array(
389
-                '\EventEspresso\core\services\currency\DefaultCalculator',
390
-            )
391
-        );
392
-        foreach ($calculators as $calculator) {
393
-            if (! class_exists($calculator)) {
394
-                continue;
395
-            }
396
-            $calculator = new $calculator();
397
-            if ($calculator instanceof Calculator && $calculator->isSupported()) {
398
-                self::$calculator = $calculator;
399
-                break;
400
-            }
401
-        }
402
-    }
403
-
404
-
405
-
406
-    /**
407
-     * @return MoneyFormatter[]
408
-     */
409
-    protected function formatters()
410
-    {
411
-        $this->initializeFormatters();
412
-        return self::$formatters;
413
-    }
414
-
415
-
416
-
417
-    /**
418
-     * initializes a filterable array of MoneyFormatter services
419
-     */
420
-    protected function initializeFormatters()
421
-    {
422
-        if (! empty(self::$formatters)) {
423
-            return;
424
-        }
425
-        self::$formatters = apply_filters(
426
-            'FHEE__EventEspresso\core\entities\money\Money__initializeFormatters__MoneyFormatters_array',
427
-            array(
428
-                1 => new DecimalMoneyFormatter(),
429
-                2 => new ThousandsMoneyFormatter(),
430
-                3 => new CurrencySignMoneyFormatter(),
431
-                4 => new CurrencyCodeMoneyFormatter(),
432
-                5 => new InternationalMoneyFormatter(),
433
-            )
434
-        );
435
-    }
436
-
437
-
438
-
439
-    /**
440
-     * @return string
441
-     */
442
-    public function __toString()
443
-    {
444
-        return $this->format(MoneyFormatter::DECIMAL_ONLY);
445
-    }
33
+	/**
34
+	 * number of decimal places to be added to currencies for internal computations,
35
+	 * but removed before any output or formatting is applied.
36
+	 * This allows us to avoid rounding errors during calculations.
37
+	 */
38
+	const EXTRA_PRECISION = 3;
39
+
40
+	/**
41
+	 * @var int $amount
42
+	 */
43
+	private $amount;
44
+
45
+	/**
46
+	 * @var Currency $currency
47
+	 */
48
+	private $currency;
49
+
50
+	/**
51
+	 * @var Calculator $calculator
52
+	 */
53
+	protected static $calculator;
54
+
55
+	/**
56
+	 * @var MoneyFormatter[] $formatters
57
+	 */
58
+	protected static $formatters;
59
+
60
+
61
+
62
+	/**
63
+	 * Money constructor.
64
+	 *
65
+	 * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
66
+	 *                                 example: $12.5 USD would equate to a value amount of 12.50
67
+	 * @param Currency         $currency
68
+	 * @throws InvalidDataTypeException
69
+	 */
70
+	public function __construct($amount, Currency $currency)
71
+	{
72
+		$this->currency = $currency;
73
+		$this->amount = (string)$this->parseAmount($amount);
74
+	}
75
+
76
+
77
+
78
+	/**
79
+	 * factory method that returns a Money object using the currency corresponding to the site's country
80
+	 * example: Money::forSite(12.5)
81
+	 *
82
+	 * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
83
+	 *                                 example: $12.5 USD would equate to a value amount of 12.50
84
+	 * @return Money
85
+	 * @throws InvalidArgumentException
86
+	 */
87
+	public static function forSite($amount)
88
+	{
89
+		$CNT_ISO = isset(EE_Registry::instance()->CFG->organization)
90
+				   && EE_Registry::instance()->CFG->organization instanceof EE_Organization_Config
91
+			? EE_Registry::instance()->CFG->organization->CNT_ISO
92
+			: 'US';
93
+		return new self($amount, Currency::createFromCountryCode($CNT_ISO));
94
+	}
95
+
96
+
97
+
98
+	/**
99
+	 * factory method that returns a Money object using the currency as specified by the supplied ISO country code
100
+	 * example: Money::forCountry(12.5,'US')
101
+	 *
102
+	 * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
103
+	 *                                 example: $12.5 USD would equate to a value amount of 12.50
104
+	 * @param string           $CNT_ISO
105
+	 * @return Money
106
+	 * @throws InvalidArgumentException
107
+	 */
108
+	public static function forCountry($amount, $CNT_ISO)
109
+	{
110
+		return new self($amount, Currency::createFromCountryCode($CNT_ISO));
111
+	}
112
+
113
+
114
+
115
+	/**
116
+	 * factory method that returns a Money object using the currency as specified by the supplied currency code
117
+	 * example: Money::forCurrency(12.5, 'USD')
118
+	 *
119
+	 * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
120
+	 *                                 example: $12.5 USD would equate to a value amount of 12.50
121
+	 * @param string           $currency_code
122
+	 * @return Money
123
+	 * @throws InvalidDataTypeException
124
+	 * @throws EE_Error
125
+	 * @throws InvalidArgumentException
126
+	 */
127
+	public static function forCurrency($amount, $currency_code)
128
+	{
129
+		return new self($amount, Currency::createFromCode($currency_code));
130
+	}
131
+
132
+
133
+
134
+	/**
135
+	 * factory method that returns a Money object for the currency specified as if it were a class method
136
+	 * example: Money::USD(12.5);
137
+	 * money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
138
+	 * example: $12.5 USD would equate to a value amount of 12.50
139
+	 *
140
+	 * @param string $currency_code
141
+	 * @param array  $arguments
142
+	 * @return Money
143
+	 * @throws InvalidDataTypeException
144
+	 * @throws EE_Error
145
+	 * @throws InvalidArgumentException
146
+	 */
147
+	public static function __callStatic($currency_code, $arguments)
148
+	{
149
+		return new self($arguments[0], Currency::createFromCode($currency_code));
150
+	}
151
+
152
+
153
+
154
+	/**
155
+	 * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
156
+	 *                                 example: $12.5 USD would equate to a value amount of 12.50
157
+	 * @return float|int|number|string
158
+	 * @throws InvalidDataTypeException
159
+	 */
160
+	private function parseAmount($amount)
161
+	{
162
+		$type = gettype($amount);
163
+		switch ($type) {
164
+			case 'integer' :
165
+			case 'double' :
166
+			case 'string' :
167
+				// shift the decimal position by the number of decimal places used internally
168
+				// ex: 12.5 for a currency using 2 decimal places, would become 1250
169
+				// then if our extra internal precision was 3, it would become 1250000
170
+				$amount *= pow(10, $this->precision());
171
+				// then round up the remaining value if there is still a fractional amount left
172
+				$amount = round($amount, 0, PHP_ROUND_HALF_UP);
173
+			break;
174
+			default  :
175
+				throw new InvalidDataTypeException(
176
+					'$amount',
177
+					$amount,
178
+					'integer (or float or string)'
179
+				);
180
+		}
181
+		return $amount;
182
+	}
183
+
184
+
185
+
186
+	/**
187
+	 * adds or subtracts additional decimal places based on the value of the Money::EXTRA_PRECISION constant
188
+	 *
189
+	 * @param bool $positive
190
+	 * @return int
191
+	 */
192
+	private function precision($positive = true)
193
+	{
194
+		$sign = $positive ? 1 : -1;
195
+		return ((int)$this->currency->decimalPlaces() + Money::EXTRA_PRECISION) * $sign;
196
+	}
197
+
198
+
199
+
200
+	/**
201
+	 * Returns the money amount as an unformatted string
202
+	 * IF YOU REQUIRE A FORMATTED STRING, THEN USE Money::format()
203
+	 *
204
+	 * @return string
205
+	 */
206
+	public function amount()
207
+	{
208
+		// shift the decimal position BACK by the number of decimal places used internally
209
+		// ex: 1250 for a currency using 2 decimal places, would become 12.5
210
+		$amount = (string)$this->amount * pow(10, $this->precision(false));
211
+		// then shave off our extra internal precision using the number of decimal places for the currency
212
+		$amount = round($amount, $this->currency->decimalPlaces());
213
+		return $amount;
214
+	}
215
+
216
+
217
+
218
+	/**
219
+	 * applies formatting based on the specified formatting level
220
+	 * corresponding to one of the constants on \EventEspresso\core\services\currency\MoneyFormatter
221
+	 *
222
+	 * @param int $formatting_level
223
+	 * @return string
224
+	 */
225
+	public function format($formatting_level = MoneyFormatter::ADD_THOUSANDS)
226
+	{
227
+		$formatted_amount = $this->amount();
228
+		$formatters = $this->formatters();
229
+		// if we are applying thousands formatting...
230
+		if ($formatting_level >= MoneyFormatter::ADD_THOUSANDS) {
231
+			// then let's remove decimal formatting since it's included in thousands formatting
232
+			unset($formatters[MoneyFormatter::DECIMAL_ONLY]);
233
+		}
234
+		for ($x = 1; $x <= $formatting_level; $x++) {
235
+			if (isset($formatters[$x]) && $formatters[$x] instanceof MoneyFormatter) {
236
+				$formatted_amount = $formatters[$x]->format($formatted_amount, $this->currency);
237
+			}
238
+		}
239
+		return $formatted_amount;
240
+	}
241
+
242
+
243
+
244
+	/**
245
+	 * Returns the Currency object for this money
246
+	 *
247
+	 * @return Currency
248
+	 */
249
+	public function currency()
250
+	{
251
+		return $this->currency;
252
+	}
253
+
254
+
255
+
256
+	/**
257
+	 * adds the supplied Money amount to this Money amount
258
+	 * and returns a new Money object
259
+	 *
260
+	 * @param Money $other
261
+	 * @return Money
262
+	 * @throws InvalidArgumentException
263
+	 */
264
+	public function add(Money $other)
265
+	{
266
+		$this->verifySameCurrency($other->currency());
267
+		return new Money(
268
+			$this->calculator()->add(
269
+				$this->amount(),
270
+				$other->amount()
271
+			),
272
+			$this->currency()
273
+		);
274
+	}
275
+
276
+
277
+
278
+	/**
279
+	 * subtracts the supplied Money amount from this Money amount
280
+	 * and returns a new Money object
281
+	 *
282
+	 * @param Money $other
283
+	 * @return Money
284
+	 * @throws InvalidArgumentException
285
+	 */
286
+	public function subtract(Money $other)
287
+	{
288
+		$this->verifySameCurrency($other->currency());
289
+		return new Money(
290
+			$this->calculator()->subtract(
291
+				$this->amount(),
292
+				$other->amount()
293
+			),
294
+			$this->currency()
295
+		);
296
+	}
297
+
298
+
299
+
300
+	/**
301
+	 * multiplies this Money amount by the supplied $multiplier
302
+	 * and returns a new Money object
303
+	 *
304
+	 * @param float|int|string $multiplier
305
+	 * @param int              $rounding_mode
306
+	 * @return Money
307
+	 * @throws InvalidDataTypeException
308
+	 */
309
+	public function multiply($multiplier, $rounding_mode = Calculator::ROUND_HALF_UP)
310
+	{
311
+		return new Money(
312
+			$this->calculator()->multiply(
313
+				$this->amount(),
314
+				$multiplier,
315
+				$this->precision(),
316
+				$rounding_mode
317
+			),
318
+			$this->currency()
319
+		);
320
+	}
321
+
322
+
323
+
324
+	/**
325
+	 * divides this Money amount by the supplied $divisor
326
+	 * and returns a new Money object
327
+	 *
328
+	 * @param float|int|string $divisor
329
+	 * @param int              $rounding_mode
330
+	 * @return Money
331
+	 * @throws InvalidDataTypeException
332
+	 */
333
+	public function divide($divisor, $rounding_mode = Calculator::ROUND_HALF_UP)
334
+	{
335
+		return new Money(
336
+			$this->calculator()->divide(
337
+				$this->amount(),
338
+				$divisor,
339
+				$this->precision(),
340
+				$rounding_mode
341
+			),
342
+			$this->currency()
343
+		);
344
+	}
345
+
346
+
347
+
348
+	/**
349
+	 * @param Currency $other_currency
350
+	 * @throws InvalidArgumentException
351
+	 */
352
+	public function verifySameCurrency(Currency $other_currency)
353
+	{
354
+		if ($this->currency()->equals($other_currency) !== true) {
355
+			throw new InvalidArgumentException(
356
+				esc_html__(
357
+					'Currencies must be the same in order to add or subtract their values.',
358
+					'event_espresso'
359
+				)
360
+			);
361
+		}
362
+	}
363
+
364
+
365
+
366
+	/**
367
+	 * @return Calculator
368
+	 */
369
+	protected function calculator()
370
+	{
371
+		$this->initializeCalculators();
372
+		return self::$calculator;
373
+	}
374
+
375
+
376
+
377
+	/**
378
+	 * loops through a filterable array of Calculator services
379
+	 * and selects the first one that is supported by the current server
380
+	 */
381
+	protected function initializeCalculators()
382
+	{
383
+		if (self::$calculator instanceof Calculator) {
384
+			return;
385
+		}
386
+		$calculators = apply_filters(
387
+			'FHEE__EventEspresso\core\entities\money\Money__initializeCalculators__Calculator_array',
388
+			array(
389
+				'\EventEspresso\core\services\currency\DefaultCalculator',
390
+			)
391
+		);
392
+		foreach ($calculators as $calculator) {
393
+			if (! class_exists($calculator)) {
394
+				continue;
395
+			}
396
+			$calculator = new $calculator();
397
+			if ($calculator instanceof Calculator && $calculator->isSupported()) {
398
+				self::$calculator = $calculator;
399
+				break;
400
+			}
401
+		}
402
+	}
403
+
404
+
405
+
406
+	/**
407
+	 * @return MoneyFormatter[]
408
+	 */
409
+	protected function formatters()
410
+	{
411
+		$this->initializeFormatters();
412
+		return self::$formatters;
413
+	}
414
+
415
+
416
+
417
+	/**
418
+	 * initializes a filterable array of MoneyFormatter services
419
+	 */
420
+	protected function initializeFormatters()
421
+	{
422
+		if (! empty(self::$formatters)) {
423
+			return;
424
+		}
425
+		self::$formatters = apply_filters(
426
+			'FHEE__EventEspresso\core\entities\money\Money__initializeFormatters__MoneyFormatters_array',
427
+			array(
428
+				1 => new DecimalMoneyFormatter(),
429
+				2 => new ThousandsMoneyFormatter(),
430
+				3 => new CurrencySignMoneyFormatter(),
431
+				4 => new CurrencyCodeMoneyFormatter(),
432
+				5 => new InternationalMoneyFormatter(),
433
+			)
434
+		);
435
+	}
436
+
437
+
438
+
439
+	/**
440
+	 * @return string
441
+	 */
442
+	public function __toString()
443
+	{
444
+		return $this->format(MoneyFormatter::DECIMAL_ONLY);
445
+	}
446 446
 
447 447
 
448 448
 
Please login to merge, or discard this patch.
core/services/currency/InternationalMoneyFormatter.php 2 patches
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -21,18 +21,18 @@
 block discarded – undo
21 21
 
22 22
 
23 23
 
24
-    /**
25
-     * @param string   $money_amount
26
-     * @param Currency $currency
27
-     * @return string
28
-     */
29
-    public function format($money_amount, Currency $currency)
30
-    {
31
-        $currency_code = $currency->code();
32
-        // remove currency code if already added
33
-        $money_amount = str_replace($currency_code, '', $money_amount);
34
-        return trim($money_amount) . ' <span class="currency-code">(' . $currency_code . ')</span>';
35
-    }
24
+	/**
25
+	 * @param string   $money_amount
26
+	 * @param Currency $currency
27
+	 * @return string
28
+	 */
29
+	public function format($money_amount, Currency $currency)
30
+	{
31
+		$currency_code = $currency->code();
32
+		// remove currency code if already added
33
+		$money_amount = str_replace($currency_code, '', $money_amount);
34
+		return trim($money_amount) . ' <span class="currency-code">(' . $currency_code . ')</span>';
35
+	}
36 36
 
37 37
 
38 38
 
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -31,7 +31,7 @@
 block discarded – undo
31 31
         $currency_code = $currency->code();
32 32
         // remove currency code if already added
33 33
         $money_amount = str_replace($currency_code, '', $money_amount);
34
-        return trim($money_amount) . ' <span class="currency-code">(' . $currency_code . ')</span>';
34
+        return trim($money_amount).' <span class="currency-code">('.$currency_code.')</span>';
35 35
     }
36 36
 
37 37
 
Please login to merge, or discard this patch.
core/services/currency/MoneyFormatter.php 1 patch
Indentation   +45 added lines, -45 removed lines patch added patch discarded remove patch
@@ -19,51 +19,51 @@
 block discarded – undo
19 19
 interface MoneyFormatter
20 20
 {
21 21
 
22
-    /**
23
-     * do NOT apply any formatting
24
-     * eg: 123456
25
-     */
26
-    const RAW = 0;
27
-
28
-    /**
29
-     * only format money amount by adding the decimal mark
30
-     * eg: 1234.56
31
-     */
32
-    const DECIMAL_ONLY = 1;
33
-
34
-    /**
35
-     * format money amount by adding decimal mark and thousands separator
36
-     * eg: 1,234.56
37
-     */
38
-    const ADD_THOUSANDS = 2;
39
-
40
-    /**
41
-     * format money amount by adding decimal mark, thousands separator, and currency sign
42
-     * eg: $1,234.56
43
-     */
44
-    const ADD_CURRENCY_SIGN = 3;
45
-
46
-    /**
47
-     * format money amount by adding decimal mark, thousands separator, currency sign, and currency code
48
-     * eg: $1,234.56 USD
49
-     */
50
-    const ADD_CURRENCY_CODE = 4;
51
-
52
-    /**
53
-     * format money amount by adding decimal mark, thousands separator, currency sign,
54
-     * and currency code wrapped in HTML span tag with HTML class
55
-     * eg: $1,234.56 <span class="currency-code">(USD)</span>
56
-     */
57
-    const INTERNATIONAL = 5;
58
-
59
-
60
-
61
-    /**
62
-     * @param string   $money_amount
63
-     * @param Currency $currency
64
-     * @return string
65
-     */
66
-    public function format($money_amount, Currency $currency);
22
+	/**
23
+	 * do NOT apply any formatting
24
+	 * eg: 123456
25
+	 */
26
+	const RAW = 0;
27
+
28
+	/**
29
+	 * only format money amount by adding the decimal mark
30
+	 * eg: 1234.56
31
+	 */
32
+	const DECIMAL_ONLY = 1;
33
+
34
+	/**
35
+	 * format money amount by adding decimal mark and thousands separator
36
+	 * eg: 1,234.56
37
+	 */
38
+	const ADD_THOUSANDS = 2;
39
+
40
+	/**
41
+	 * format money amount by adding decimal mark, thousands separator, and currency sign
42
+	 * eg: $1,234.56
43
+	 */
44
+	const ADD_CURRENCY_SIGN = 3;
45
+
46
+	/**
47
+	 * format money amount by adding decimal mark, thousands separator, currency sign, and currency code
48
+	 * eg: $1,234.56 USD
49
+	 */
50
+	const ADD_CURRENCY_CODE = 4;
51
+
52
+	/**
53
+	 * format money amount by adding decimal mark, thousands separator, currency sign,
54
+	 * and currency code wrapped in HTML span tag with HTML class
55
+	 * eg: $1,234.56 <span class="currency-code">(USD)</span>
56
+	 */
57
+	const INTERNATIONAL = 5;
58
+
59
+
60
+
61
+	/**
62
+	 * @param string   $money_amount
63
+	 * @param Currency $currency
64
+	 * @return string
65
+	 */
66
+	public function format($money_amount, Currency $currency);
67 67
 
68 68
 
69 69
 }
Please login to merge, or discard this patch.
core/services/currency/CurrencyCodeMoneyFormatter.php 2 patches
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -19,15 +19,15 @@
 block discarded – undo
19 19
 class CurrencyCodeMoneyFormatter implements MoneyFormatter
20 20
 {
21 21
 
22
-    /**
23
-     * @param string   $money_amount
24
-     * @param Currency $currency
25
-     * @return string
26
-     */
27
-    public function format($money_amount, Currency $currency)
28
-    {
29
-        return $money_amount . ' ' . $currency->code();
30
-    }
22
+	/**
23
+	 * @param string   $money_amount
24
+	 * @param Currency $currency
25
+	 * @return string
26
+	 */
27
+	public function format($money_amount, Currency $currency)
28
+	{
29
+		return $money_amount . ' ' . $currency->code();
30
+	}
31 31
 
32 32
 }
33 33
 // End of file CurrencyCodeMoneyFormatter.php
Please login to merge, or discard this patch.
Spacing   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -26,7 +26,7 @@
 block discarded – undo
26 26
      */
27 27
     public function format($money_amount, Currency $currency)
28 28
     {
29
-        return $money_amount . ' ' . $currency->code();
29
+        return $money_amount.' '.$currency->code();
30 30
     }
31 31
 
32 32
 }
Please login to merge, or discard this patch.