-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Currency.php
87 lines (75 loc) · 1.84 KB
/
Currency.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
declare(strict_types=1);
namespace SonsOfPHP\Component\Money;
use SonsOfPHP\Component\Money\Query\Currency\IsEqualToCurrencyQuery;
use SonsOfPHP\Contract\Money\CurrencyInterface;
use SonsOfPHP\Contract\Money\CurrencyQueryInterface;
use Stringable;
/**
* @author Joshua Estes <joshua@sonsofphp.com>
*/
final class Currency implements CurrencyInterface, Stringable
{
public function __construct(
private string $currencyCode,
private readonly ?int $numericCode = null,
private readonly ?int $minorUnit = null
) {
$this->currencyCode = strtoupper($currencyCode);
}
/**
* @see self::getCurrencyCode()
*/
public function __toString(): string
{
return $this->getCurrencyCode();
}
/**
* Makes it easy to create new currencies.
*
* Examples:
* Currency::USD();
* Currency::USD(840, 2);
*/
public static function __callStatic(string $currencyCode, array $args): CurrencyInterface
{
$numericCode = $args[0] ?? null;
$minorUnit = $args[1] ?? null;
return new self($currencyCode, $numericCode, $minorUnit);
}
/**
* {@inheritdoc}
*/
public function query(CurrencyQueryInterface $query)
{
return $query->queryFrom($this);
}
/**
* {@inheritdoc}
*/
public function getCurrencyCode(): string
{
return $this->currencyCode;
}
/**
* {@inheritdoc}
*/
public function getNumericCode(): ?int
{
return $this->numericCode;
}
/**
* {@inheritdoc}
*/
public function getMinorUnit(): ?int
{
return $this->minorUnit;
}
/**
* {@inheritdoc}
*/
public function isEqualTo(CurrencyInterface $currency): bool
{
return $this->query(new IsEqualToCurrencyQuery($currency));
}
}