22
33namespace PostScripton \Money ;
44
5+ use Illuminate \Support \Collection ;
56use PostScripton \Money \Exceptions \CurrencyDoesNotExistException ;
7+ use PostScripton \Money \Exceptions \CurrencyHasWrongConstructorException ;
8+ use PostScripton \Money \Exceptions \CurrencyListConfigException ;
9+ use PostScripton \Money \Exceptions \NoSuchCurrencySymbolException ;
610use PostScripton \Money \Exceptions \ShouldPublishConfigFileException ;
711
812class Currency
@@ -12,38 +16,76 @@ class Currency
1216 public const POS_START = 0 ;
1317 public const POS_END = 1 ;
1418
19+ public const DISPLAY_SYMBOL = 10 ;
20+ public const DISPLAY_CODE = 11 ;
21+
22+ public const LIST_ALL = 'all ' ;
23+ public const LIST_POPULAR = 'popular ' ;
24+ private static string $ _list ;
25+
1526 public static function code (string $ code ): ?Currency
1627 {
17- $ code = strtoupper ($ code );
28+ $ currency = is_numeric ($ code )
29+ ? self ::currencies ()->firstWhere ('num_code ' , $ code )
30+ : self ::currencies ()->firstWhere ('iso_code ' , strtoupper ($ code ));
1831
19- if (! array_key_exists ( $ code , self :: all () )) {
20- throw new CurrencyDoesNotExistException (__METHOD__ , 1 , '$code ' , $ code );
32+ if (is_null ( $ currency )) {
33+ throw new CurrencyDoesNotExistException (__METHOD__ , 1 , '$code ' , implode ( ' , ' , [ $ code, self :: $ _list ]) );
2134 }
2235
23- $ currency = self ::all ()[$ code ];
36+ return new Currency ($ currency );
37+ }
2438
25- return new Currency ($ code , $ currency ['symbol ' ], $ currency ['position ' ]);
39+ protected static function currencies (): Collection
40+ {
41+ if (!in_array (config ('money.currency_list ' ), [self ::LIST_ALL , self ::LIST_POPULAR ])) {
42+ throw new CurrencyListConfigException (config ('money.currency_list ' ));
43+ }
44+
45+ if (!self ::$ currencies ) {
46+ self ::setCurrencyList (config ('money.currency_list ' ));
47+ }
48+
49+ return collect (self ::$ currencies );
2650 }
2751
28- protected static function all (): array
52+ public static function setCurrencyList ( string $ list = self :: LIST_POPULAR )
2953 {
30- if (! static :: $ currencies ) {
31- static :: $ currencies = require __DIR__ . ' /List/currencies.php ' ;
54+ if ($ list !== self :: LIST_ALL && $ list !== self :: LIST_POPULAR ) {
55+ $ list = self :: LIST_POPULAR ;
3256 }
33- return static ::$ currencies ;
57+
58+ self ::$ currencies = require __DIR__ . "/List/ {$ list }_currencies.php " ;
59+ self ::$ _list = $ list ;
3460 }
3561
3662 // ==================== OBJECT ==================== //
3763
38- private string $ code ;
39- private string $ symbol ;
40- private string $ position ;
64+ private string $ full_name ;
65+ private string $ name ;
66+ private string $ iso_code ;
67+ private string $ num_code ;
68+ private $ symbol ; // array or string
69+ private int $ position ;
70+ private int $ display ;
4171
42- public function __construct (string $ code , string $ symbol , ? int $ position = null )
72+ public function __construct (array $ currency )
4373 {
44- $ this ->code = $ code ;
45- $ this ->symbol = $ symbol ;
46- $ this ->position = $ position ?? self ::POS_START ;
74+ if (is_null ($ currency ['full_name ' ]) ||
75+ is_null ($ currency ['name ' ]) ||
76+ is_null ($ currency ['iso_code ' ]) ||
77+ is_null ($ currency ['num_code ' ]) ||
78+ is_null ($ currency ['symbol ' ])) {
79+ throw new CurrencyHasWrongConstructorException ();
80+ }
81+
82+ $ this ->full_name = $ currency ['full_name ' ];
83+ $ this ->name = $ currency ['name ' ];
84+ $ this ->iso_code = $ currency ['iso_code ' ];
85+ $ this ->num_code = $ currency ['num_code ' ];
86+ $ this ->symbol = $ currency ['symbol ' ];
87+ $ this ->position = $ currency ['position ' ] ?? self ::POS_END ;
88+ $ this ->display = self ::DISPLAY_SYMBOL ;
4789 }
4890
4991 /**
@@ -58,13 +100,45 @@ public static function getConfigCurrency(): string
58100 return config ('money.default_currency ' , 'USD ' );
59101 }
60102
103+ public function getFullName (): string
104+ {
105+ return $ this ->full_name ;
106+ }
107+
108+ public function getName (): string
109+ {
110+ return $ this ->name ;
111+ }
112+
61113 public function getCode (): string
62114 {
63- return $ this ->code ;
115+ return $ this ->iso_code ;
64116 }
65117
66- public function getSymbol (): string
118+ public function getNumCode (): string
67119 {
120+ return $ this ->num_code ;
121+ }
122+
123+ public function getSymbol (int $ index = 0 ): string
124+ {
125+ if ($ this ->display === self ::DISPLAY_CODE ) {
126+ return $ this ->iso_code ;
127+ }
128+
129+ if (is_array ($ this ->symbol )) {
130+ if (!array_key_exists ($ index , $ this ->symbol )) {
131+ throw new NoSuchCurrencySymbolException (
132+ __METHOD__ ,
133+ 1 ,
134+ '$index ' ,
135+ implode (', ' , [$ index , count ($ this ->symbol ) - 1 ])
136+ );
137+ }
138+
139+ return $ this ->symbol [$ index ];
140+ }
141+
68142 return $ this ->symbol ;
69143 }
70144
@@ -73,13 +147,28 @@ public function getPosition(): int
73147 return $ this ->position ;
74148 }
75149
76- public function setPosition (string $ position ): Currency
150+ public function getDisplay (): int
151+ {
152+ return $ this ->display ;
153+ }
154+
155+ public function setPosition (int $ position = self ::POS_START ): self
77156 {
78- if ($ position !== self ::POS_START || $ position !== self ::POS_END ) {
157+ if ($ position !== self ::POS_START && $ position !== self ::POS_END ) {
79158 $ position = self ::POS_START ;
80159 }
81160
82161 $ this ->position = $ position ;
83162 return $ this ;
84163 }
164+
165+ public function setDisplay (int $ display = self ::DISPLAY_SYMBOL ): self
166+ {
167+ if ($ display !== self ::DISPLAY_SYMBOL && $ display !== self ::DISPLAY_CODE ) {
168+ $ display = self ::DISPLAY_SYMBOL ;
169+ }
170+
171+ $ this ->display = $ display ;
172+ return $ this ;
173+ }
85174}
0 commit comments