Skip to content

Commit 92b250a

Browse files
authored
Merge v2.2.1 (#13)
v2.2.1
2 parents c9a2543 + 37d5119 commit 92b250a

15 files changed

+545
-174
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,14 @@ $money->settings->getCurrency()->getSymbol(); // "₽"
232232
$money->toString(); // "123.4 ₽"
233233
```
234234

235+
There is a shortcut for getting a currency:
236+
```php
237+
use PostScripton\Money\Money;
238+
239+
$money = new Money(1234);
240+
$money->getCurrency(); // the same as: $money->settings->getCurrency()
241+
```
242+
235243
---
236244

237245
#### Origin number
@@ -380,6 +388,14 @@ Currency::code('USD');
380388
Currency::setCurrencyList(Currency::LIST_ALL);
381389
Currency::code('EGP');
382390
```
391+
Following constants are provided:
392+
```php
393+
use PostScripton\Money\Currency;
394+
395+
Currency::LIST_ALL;
396+
Currency::LIST_POPULAR;
397+
Currency::LIST_CONFIG; // returns back to list what you've written in the config
398+
```
383399

384400
---
385401

@@ -516,6 +532,18 @@ $money = new Money(1000); // "$ 100"
516532
$money->add(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 150"
517533
```
518534

535+
```php
536+
use PostScripton\Money\Money;
537+
use PostScripton\Money\Currency;
538+
539+
$m1 = new Money(1000); // "$ 100"
540+
$m2 = new Money(500); // "$ 50"
541+
$m3 = new Money(500, Currency::code('RUB')); // "50 ₽"
542+
543+
$m1->add($m2); // "$ 150"
544+
$m1->add($m3); // MoneyHasDifferentCurrenciesException
545+
```
546+
519547
---
520548

521549
##### `subtract()`
@@ -537,6 +565,18 @@ $money = new Money(1500); // "$ 150"
537565
$money->subtract(50.0, MoneySettings::ORIGIN_FLOAT); // "$ 100"
538566
```
539567

568+
```php
569+
use PostScripton\Money\Money;
570+
use PostScripton\Money\Currency;
571+
572+
$m1 = new Money(1500); // "$ 150"
573+
$m2 = new Money(500); // "$ 50"
574+
$m3 = new Money(500, Currency::code('RUB')); // "50 ₽"
575+
576+
$m1->subtract($m2); // "$ 100"
577+
$m1->subtract($m3); // MoneyHasDifferentCurrenciesException
578+
```
579+
540580
---
541581

542582
##### `rebase()`
@@ -558,6 +598,18 @@ $money = new Money(1500); // "$ 150"
558598
$money->rebase(10.0, MoneySettings::ORIGIN_FLOAT); // "$ 10"
559599
```
560600

601+
```php
602+
use PostScripton\Money\Money;
603+
use PostScripton\Money\Currency;
604+
605+
$m1 = new Money(1000); // "$ 100"
606+
$m2 = new Money(750); // "$ 75"
607+
$m3 = new Money(750, Currency::code('RUB')); // "75 ₽"
608+
609+
$m1->rebase($m2); // "$ 75"
610+
$m1->rebase($m3); // MoneyHasDifferentCurrenciesException
611+
```
612+
561613
---
562614

563615
##### `convertOfflineInto()`

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "postscripton/laravel-money",
33
"description": "A convenient way to convert numbers from DB or inputs into money strings for humans",
4-
"version": "2.2.0",
5-
"type": "library",
4+
"version": "2.2.1",
5+
"type": "library",
66
"license": "MIT",
77
"authors": [
88
{

config/money.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
| For now following lists are provided:
2727
| 1. all - all the currencies in the world.
2828
| 2. popular - only the most popular ones (35) are used. (default)
29+
| 3. ['840', 'EUR', 'RUB'] - array of currency codes you need
2930
|
3031
| Segregation of currencies is assumed for performance purposes so that
3132
| unnecessary ones won't be used.

src/Currency.php

Lines changed: 100 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace PostScripton\Money\Exceptions;
4+
5+
class MoneyHasDifferentCurrenciesException extends ValueErrorException
6+
{
7+
public function __construct(
8+
string $method,
9+
int $arg_num,
10+
string $arg_name = null,
11+
string $message = null,
12+
$code = 0,
13+
BaseException $previous = null
14+
) {
15+
parent::__construct(
16+
$method,
17+
$arg_num,
18+
$arg_name,
19+
"must be the same currency as the main money",
20+
$message,
21+
$code,
22+
$previous
23+
);
24+
}
25+
}

0 commit comments

Comments
 (0)