Skip to content

Commit

Permalink
Merge v2.2.1 (#13)
Browse files Browse the repository at this point in the history
v2.2.1
  • Loading branch information
PostScripton authored May 6, 2021
2 parents c9a2543 + 37d5119 commit 92b250a
Show file tree
Hide file tree
Showing 15 changed files with 545 additions and 174 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,14 @@ $money->settings->getCurrency()->getSymbol(); // "₽"
$money->toString(); // "123.4 ₽"
```

There is a shortcut for getting a currency:
```php
use PostScripton\Money\Money;

$money = new Money(1234);
$money->getCurrency(); // the same as: $money->settings->getCurrency()
```

---

#### Origin number
Expand Down Expand Up @@ -380,6 +388,14 @@ Currency::code('USD');
Currency::setCurrencyList(Currency::LIST_ALL);
Currency::code('EGP');
```
Following constants are provided:
```php
use PostScripton\Money\Currency;

Currency::LIST_ALL;
Currency::LIST_POPULAR;
Currency::LIST_CONFIG; // returns back to list what you've written in the config
```

---

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

```php
use PostScripton\Money\Money;
use PostScripton\Money\Currency;

$m1 = new Money(1000); // "$ 100"
$m2 = new Money(500); // "$ 50"
$m3 = new Money(500, Currency::code('RUB')); // "50 ₽"

$m1->add($m2); // "$ 150"
$m1->add($m3); // MoneyHasDifferentCurrenciesException
```

---

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

```php
use PostScripton\Money\Money;
use PostScripton\Money\Currency;

$m1 = new Money(1500); // "$ 150"
$m2 = new Money(500); // "$ 50"
$m3 = new Money(500, Currency::code('RUB')); // "50 ₽"

$m1->subtract($m2); // "$ 100"
$m1->subtract($m3); // MoneyHasDifferentCurrenciesException
```

---

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

```php
use PostScripton\Money\Money;
use PostScripton\Money\Currency;

$m1 = new Money(1000); // "$ 100"
$m2 = new Money(750); // "$ 75"
$m3 = new Money(750, Currency::code('RUB')); // "75 ₽"

$m1->rebase($m2); // "$ 75"
$m1->rebase($m3); // MoneyHasDifferentCurrenciesException
```

---

##### `convertOfflineInto()`
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "postscripton/laravel-money",
"description": "A convenient way to convert numbers from DB or inputs into money strings for humans",
"version": "2.2.0",
"type": "library",
"version": "2.2.1",
"type": "library",
"license": "MIT",
"authors": [
{
Expand Down
1 change: 1 addition & 0 deletions config/money.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
| For now following lists are provided:
| 1. all - all the currencies in the world.
| 2. popular - only the most popular ones (35) are used. (default)
| 3. ['840', 'EUR', 'RUB'] - array of currency codes you need
|
| Segregation of currencies is assumed for performance purposes so that
| unnecessary ones won't be used.
Expand Down
112 changes: 100 additions & 12 deletions src/Currency.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Currency

public const LIST_ALL = 'all';
public const LIST_POPULAR = 'popular';
private static string $_list;
public const LIST_CONFIG = 'config';
private const CONFIG_LIST = 'money.currency_list';
private static ?string $_list;

public static function code(string $code): ?Currency
{
Expand All @@ -33,30 +35,88 @@ public static function code(string $code): ?Currency
throw new CurrencyDoesNotExistException(__METHOD__, 1, '$code', implode(',', [$code, self::$_list]));
}

return new Currency($currency);
return new self($currency);
}

protected static function currencies(): Collection
{
if (!in_array(config('money.currency_list'), [self::LIST_ALL, self::LIST_POPULAR])) {
throw new CurrencyListConfigException(config('money.currency_list'));
$list = is_array(config(self::CONFIG_LIST))
? self::LIST_CONFIG
: config(self::CONFIG_LIST);

if (self::isIncorrectList($list)) {
throw new CurrencyListConfigException($list);
}

if (!self::$currencies) {
self::setCurrencyList(config('money.currency_list'));
self::setCurrencyList($list);
}

return collect(self::$currencies);
}

public static function setCurrencyList(string $list = self::LIST_POPULAR)
public static function isIncorrectList(string $list): bool
{
if ($list !== self::LIST_ALL && $list !== self::LIST_POPULAR) {
return !in_array(
$list,
[
self::LIST_ALL,
self::LIST_POPULAR,
self::LIST_CONFIG,
]
);
}

public static function setCurrencyList(string $list = self::LIST_POPULAR): void
{
if (self::isIncorrectList($list)) {
$list = self::LIST_POPULAR;
}

self::$currencies = require __DIR__ . "/List/{$list}_currencies.php";
self::$_list = $list;

if ($list !== self::LIST_CONFIG) {
self::$currencies = self::getList($list);
return;
}

// Config list below...

if (!is_array(config(self::CONFIG_LIST))) {
self::$currencies = self::getList(config(self::CONFIG_LIST));
return;
}

// Custom currency list
$custom_list = config(self::CONFIG_LIST);
self::$currencies = self::getList(self::LIST_ALL); // todo смёржить с кастомными валютами

self::$currencies = array_filter(
self::$currencies,
function ($currency) use (&$custom_list) {
if (empty($custom_list)) {
return false;
}

foreach ($custom_list as $item) {
if ($currency['iso_code'] === $item || $currency['num_code'] === $item) {
$custom_list = array_diff($custom_list, [$item]);
return true;
}
}

return false;
}
);
}

public static function currentList(): string
{
return self::$_list ?? self::CONFIG_LIST;
}

private static function getList(string $list)
{
return require __DIR__ . "/List/" . $list . "_currencies.php";
}

// ==================== OBJECT ==================== //
Expand All @@ -68,6 +128,7 @@ public static function setCurrencyList(string $list = self::LIST_POPULAR)
private $symbol; // array or string
private int $position;
private int $display;
private ?int $preferred_symbol = null;

public function __construct(array $currency)
{
Expand All @@ -86,6 +147,7 @@ public function __construct(array $currency)
$this->symbol = $currency['symbol'];
$this->position = $currency['position'] ?? self::POS_END;
$this->display = self::DISPLAY_SYMBOL;
$preferred_symbol = null;
}

/**
Expand Down Expand Up @@ -120,22 +182,30 @@ public function getNumCode(): string
return $this->num_code;
}

public function getSymbol(int $index = 0): string
public function getSymbol(?int $index = null): string
{
if ($this->display === self::DISPLAY_CODE) {
return $this->iso_code;
}

if (is_array($this->symbol)) {
if (!array_key_exists($index, $this->symbol)) {
if (!array_key_exists($index ?? 0, $this->symbol)) {
throw new NoSuchCurrencySymbolException(
__METHOD__,
1,
'$index',
implode(',', [$index, count($this->symbol) - 1])
implode(',', [$index ?? 0, count($this->symbol) - 1])
);
}

if (is_null($index)) {
if (!is_null($this->preferred_symbol)) {
return $this->symbol[$this->preferred_symbol];
}

$index = 0;
}

return $this->symbol[$index];
}

Expand Down Expand Up @@ -171,4 +241,22 @@ public function setDisplay(int $display = self::DISPLAY_SYMBOL): self
$this->display = $display;
return $this;
}

public function setPreferredSymbol(int $index = 0): self
{
if (is_array($this->symbol)) {
if (!array_key_exists($index, $this->symbol)) {
throw new NoSuchCurrencySymbolException(
__METHOD__,
1,
'$index',
implode(',', [$index, count($this->symbol) - 1])
);
}

$this->preferred_symbol = $index;
}

return $this;
}
}
25 changes: 25 additions & 0 deletions src/Exceptions/MoneyHasDifferentCurrenciesException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace PostScripton\Money\Exceptions;

class MoneyHasDifferentCurrenciesException extends ValueErrorException
{
public function __construct(
string $method,
int $arg_num,
string $arg_name = null,
string $message = null,
$code = 0,
BaseException $previous = null
) {
parent::__construct(
$method,
$arg_num,
$arg_name,
"must be the same currency as the main money",
$message,
$code,
$previous
);
}
}
Loading

0 comments on commit 92b250a

Please sign in to comment.