|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Infinitypaul\NairaExchangeRates\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Infinitypaul\NairaExchangeRates\Exceptions\Exceptions; |
|
6
|
|
|
|
|
7
|
|
|
trait CurrencyTraits |
|
8
|
|
|
{ |
|
9
|
|
|
// Supported currencies: |
|
10
|
|
|
private $_currencies = [ |
|
11
|
|
|
'USD', 'GBP', 'EUR', 'JPY', 'XAF', 'CNY', 'QAR', 'ZAR', 'SEK', |
|
12
|
|
|
]; |
|
13
|
|
|
|
|
14
|
|
|
// The base currency (default is USD): |
|
15
|
|
|
private $baseCurrency; |
|
16
|
|
|
|
|
17
|
|
|
// Regular Expression for the currency: |
|
18
|
|
|
private $currencyRegExp = '/^[A-Z]{3}$/'; |
|
19
|
|
|
|
|
20
|
|
|
// Sanitize a currency code: |
|
21
|
|
|
private function sanitizeCurrencyCode(string $code) |
|
22
|
|
|
{ |
|
23
|
|
|
return trim( |
|
24
|
|
|
strtoupper($code) |
|
25
|
|
|
); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
// Set the base currency: |
|
29
|
|
|
public function setBaseCurrency(string $currency) |
|
30
|
|
|
{ |
|
31
|
|
|
// Sanitize the code: |
|
32
|
|
|
$currencyCode = $this->sanitizeCurrencyCode($currency); |
|
33
|
|
|
|
|
34
|
|
|
// Is it valid? |
|
35
|
|
|
$this->verifyCurrencyCode($currencyCode); |
|
36
|
|
|
|
|
37
|
|
|
$this->baseCurrency = $currencyCode; |
|
38
|
|
|
|
|
39
|
|
|
// Return object to preserve method-chaining: |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Runs tests to verify a currency code: |
|
44
|
|
|
private function verifyCurrencyCode(string $code) |
|
45
|
|
|
{ |
|
46
|
|
|
$currencyCode = $this->sanitizeCurrencyCode($code); |
|
47
|
|
|
|
|
48
|
|
|
// Is the currency code in ISO 4217 format? |
|
49
|
|
|
if (! $this->validateCurrencyCodeFormat($currencyCode)) { |
|
50
|
|
|
throw Exceptions::create('format.invalid_currency_code'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
// Is it a supported currency? |
|
54
|
|
|
if (! $this->currencyIsSupported($currencyCode)) { |
|
55
|
|
|
throw Exceptions('format.unsupported_currency'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
// Validate a currency code is in the correct format: |
|
60
|
|
|
private function validateCurrencyCodeFormat(string $code = null) |
|
61
|
|
|
{ |
|
62
|
|
|
if (! is_null($code)) { |
|
63
|
|
|
// Is the string longer than 3 characters? |
|
64
|
|
|
if (strlen($code) != 3) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Does it contain non-alphabetical characters? |
|
69
|
|
|
if (! preg_match($this->currencyRegExp, $code)) { |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// Check if a currency code is in the supported range: |
|
80
|
|
|
public function currencyIsSupported(string $code) |
|
81
|
|
|
{ |
|
82
|
|
|
$currencyCode = $this->sanitizeCurrencyCode($code); |
|
83
|
|
|
|
|
84
|
|
|
if (! $this->validateCurrencyCodeFormat($currencyCode)) { |
|
85
|
|
|
throw Exceptions::create('format.invalid_currency_code'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return in_array($currencyCode, $this->_currencies); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function getBaseCurrency() |
|
92
|
|
|
{ |
|
93
|
|
|
return (is_null($this->baseCurrency)) ? null : $this->baseCurrency; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
// Get the supported currencies: |
|
97
|
|
|
public function getSupportedCurrencies(string $concat = null) |
|
98
|
|
|
{ |
|
99
|
|
|
return (is_null($concat)) ? $this->_currencies : implode($concat, $this->_currencies); |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|