@@ -21,7 +21,9 @@ class Currency
2121
2222 public const LIST_ALL = 'all ' ;
2323 public const LIST_POPULAR = 'popular ' ;
24- private static string $ _list ;
24+ public const LIST_CONFIG = 'config ' ;
25+ private const CONFIG_LIST = 'money.currency_list ' ;
26+ private static ?string $ _list ;
2527
2628 public static function code (string $ code ): ?Currency
2729 {
@@ -33,30 +35,88 @@ public static function code(string $code): ?Currency
3335 throw new CurrencyDoesNotExistException (__METHOD__ , 1 , '$code ' , implode (', ' , [$ code , self ::$ _list ]));
3436 }
3537
36- return new Currency ($ currency );
38+ return new self ($ currency );
3739 }
3840
3941 protected static function currencies (): Collection
4042 {
41- if (!in_array (config ('money.currency_list ' ), [self ::LIST_ALL , self ::LIST_POPULAR ])) {
42- throw new CurrencyListConfigException (config ('money.currency_list ' ));
43+ $ list = is_array (config (self ::CONFIG_LIST ))
44+ ? self ::LIST_CONFIG
45+ : config (self ::CONFIG_LIST );
46+
47+ if (self ::isIncorrectList ($ list )) {
48+ throw new CurrencyListConfigException ($ list );
4349 }
4450
4551 if (!self ::$ currencies ) {
46- self ::setCurrencyList (config ( ' money.currency_list ' ) );
52+ self ::setCurrencyList ($ list );
4753 }
4854
4955 return collect (self ::$ currencies );
5056 }
5157
52- public static function setCurrencyList (string $ list = self :: LIST_POPULAR )
58+ public static function isIncorrectList (string $ list): bool
5359 {
54- if ($ list !== self ::LIST_ALL && $ list !== self ::LIST_POPULAR ) {
60+ return !in_array (
61+ $ list ,
62+ [
63+ self ::LIST_ALL ,
64+ self ::LIST_POPULAR ,
65+ self ::LIST_CONFIG ,
66+ ]
67+ );
68+ }
69+
70+ public static function setCurrencyList (string $ list = self ::LIST_POPULAR ): void
71+ {
72+ if (self ::isIncorrectList ($ list )) {
5573 $ list = self ::LIST_POPULAR ;
5674 }
57-
58- self ::$ currencies = require __DIR__ . "/List/ {$ list }_currencies.php " ;
5975 self ::$ _list = $ list ;
76+
77+ if ($ list !== self ::LIST_CONFIG ) {
78+ self ::$ currencies = self ::getList ($ list );
79+ return ;
80+ }
81+
82+ // Config list below...
83+
84+ if (!is_array (config (self ::CONFIG_LIST ))) {
85+ self ::$ currencies = self ::getList (config (self ::CONFIG_LIST ));
86+ return ;
87+ }
88+
89+ // Custom currency list
90+ $ custom_list = config (self ::CONFIG_LIST );
91+ self ::$ currencies = self ::getList (self ::LIST_ALL ); // todo смёржить с кастомными валютами
92+
93+ self ::$ currencies = array_filter (
94+ self ::$ currencies ,
95+ function ($ currency ) use (&$ custom_list ) {
96+ if (empty ($ custom_list )) {
97+ return false ;
98+ }
99+
100+ foreach ($ custom_list as $ item ) {
101+ if ($ currency ['iso_code ' ] === $ item || $ currency ['num_code ' ] === $ item ) {
102+ $ custom_list = array_diff ($ custom_list , [$ item ]);
103+ return true ;
104+ }
105+ }
106+
107+ return false ;
108+ }
109+ );
110+ }
111+
112+ public static function currentList (): string
113+ {
114+ return self ::$ _list ?? self ::CONFIG_LIST ;
115+ }
116+
117+ private static function getList (string $ list )
118+ {
119+ return require __DIR__ . "/List/ " . $ list . "_currencies.php " ;
60120 }
61121
62122 // ==================== OBJECT ==================== //
@@ -68,6 +128,7 @@ public static function setCurrencyList(string $list = self::LIST_POPULAR)
68128 private $ symbol ; // array or string
69129 private int $ position ;
70130 private int $ display ;
131+ private ?int $ preferred_symbol = null ;
71132
72133 public function __construct (array $ currency )
73134 {
@@ -86,6 +147,7 @@ public function __construct(array $currency)
86147 $ this ->symbol = $ currency ['symbol ' ];
87148 $ this ->position = $ currency ['position ' ] ?? self ::POS_END ;
88149 $ this ->display = self ::DISPLAY_SYMBOL ;
150+ $ preferred_symbol = null ;
89151 }
90152
91153 /**
@@ -120,22 +182,30 @@ public function getNumCode(): string
120182 return $ this ->num_code ;
121183 }
122184
123- public function getSymbol (int $ index = 0 ): string
185+ public function getSymbol (? int $ index = null ): string
124186 {
125187 if ($ this ->display === self ::DISPLAY_CODE ) {
126188 return $ this ->iso_code ;
127189 }
128190
129191 if (is_array ($ this ->symbol )) {
130- if (!array_key_exists ($ index , $ this ->symbol )) {
192+ if (!array_key_exists ($ index ?? 0 , $ this ->symbol )) {
131193 throw new NoSuchCurrencySymbolException (
132194 __METHOD__ ,
133195 1 ,
134196 '$index ' ,
135- implode (', ' , [$ index , count ($ this ->symbol ) - 1 ])
197+ implode (', ' , [$ index ?? 0 , count ($ this ->symbol ) - 1 ])
136198 );
137199 }
138200
201+ if (is_null ($ index )) {
202+ if (!is_null ($ this ->preferred_symbol )) {
203+ return $ this ->symbol [$ this ->preferred_symbol ];
204+ }
205+
206+ $ index = 0 ;
207+ }
208+
139209 return $ this ->symbol [$ index ];
140210 }
141211
@@ -171,4 +241,22 @@ public function setDisplay(int $display = self::DISPLAY_SYMBOL): self
171241 $ this ->display = $ display ;
172242 return $ this ;
173243 }
244+
245+ public function setPreferredSymbol (int $ index = 0 ): self
246+ {
247+ if (is_array ($ this ->symbol )) {
248+ if (!array_key_exists ($ index , $ this ->symbol )) {
249+ throw new NoSuchCurrencySymbolException (
250+ __METHOD__ ,
251+ 1 ,
252+ '$index ' ,
253+ implode (', ' , [$ index , count ($ this ->symbol ) - 1 ])
254+ );
255+ }
256+
257+ $ this ->preferred_symbol = $ index ;
258+ }
259+
260+ return $ this ;
261+ }
174262}
0 commit comments