1 | <?php |
||
12 | final class CurrencyFactory implements CurrencyFactoryContract |
||
13 | { |
||
14 | const DEFAULT_DATA_PATH = __DIR__.'/Data/CurrencyTypes.php'; |
||
15 | |||
16 | /** @var array */ |
||
17 | private $data; |
||
18 | |||
19 | /** @var Currency[] */ |
||
20 | private $currencies; |
||
21 | |||
22 | 112 | private function __construct(array $data) |
|
27 | |||
28 | 112 | public static function fromDataPath(string $dataPath = self::DEFAULT_DATA_PATH): CurrencyFactory |
|
35 | |||
36 | 122 | public static function fromDataArray(array $data): CurrencyFactory |
|
42 | |||
43 | /** |
||
44 | * @param array $currenciesData |
||
45 | * |
||
46 | * @throws InvalidCurrenciesDataError |
||
47 | */ |
||
48 | 122 | private static function validateCurrenciesData(array $currenciesData) |
|
65 | |||
66 | 112 | private static function hasValidISOCode($ISOCode): bool |
|
70 | |||
71 | 112 | private static function hasValidSymbol(array $currencyData): bool |
|
78 | |||
79 | 112 | private static function hasValidSymbolPlacement(array $currencyData): bool |
|
80 | { |
||
81 | return |
||
82 | 112 | isset($currencyData['symbolPlacement']) |
|
83 | 112 | && \is_int($currencyData['symbolPlacement']) |
|
84 | 112 | && \in_array( |
|
85 | 112 | $currencyData['symbolPlacement'], |
|
86 | [ |
||
87 | 112 | CurrencyContract::BEFORE_PLACEMENT, |
|
88 | CurrencyContract::AFTER_PLACEMENT, |
||
89 | ] |
||
90 | ); |
||
91 | } |
||
92 | |||
93 | 112 | private static function hasValidNumFractionalDigits(array $currencyData): bool |
|
97 | |||
98 | 113 | public function buildFromISOCode(string $ISOCode): CurrencyContract |
|
99 | { |
||
100 | 113 | if (!isset($this->data[$ISOCode])) { |
|
101 | 1 | throw new UnsupportedCurrencyISOCodeError($ISOCode); |
|
102 | } |
||
103 | |||
104 | 112 | if (!isset($this->currencies[$ISOCode])) { |
|
105 | 112 | $this->currencies[$ISOCode] = new Currency( |
|
106 | 112 | $ISOCode, |
|
107 | 112 | $this->data[$ISOCode]['symbol'], |
|
108 | 112 | $this->data[$ISOCode]['numFractionalDigits'], |
|
109 | 112 | $this->data[$ISOCode]['symbolPlacement'], |
|
110 | 112 | (isset($this->data[$ISOCode]['name']) && !empty($this->data[$ISOCode]['name'])) |
|
111 | ? $this->data[$ISOCode]['name'] |
||
112 | 112 | : '' |
|
113 | ); |
||
114 | } |
||
115 | |||
116 | 112 | return $this->currencies[$ISOCode]; |
|
117 | } |
||
118 | |||
119 | /** @return string[] */ |
||
120 | 1 | public function getSupportedCurrencyISOCodes(): array |
|
124 | } |
||
125 |