1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Adsmurai\Currency; |
6
|
|
|
|
7
|
|
|
use Adsmurai\Currency\Contracts\Currency as CurrencyInterface; |
8
|
|
|
use Adsmurai\Currency\Errors\InconsistentCurrenciesError; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
|
11
|
|
|
final class Currency implements CurrencyInterface |
12
|
|
|
{ |
13
|
|
|
private $ISOCode; |
14
|
|
|
|
15
|
|
|
private $name; |
16
|
|
|
|
17
|
|
|
private $symbol; |
18
|
|
|
|
19
|
|
|
private $numFractionalDigits; |
20
|
|
|
|
21
|
|
|
private $symbolPlacement; |
22
|
|
|
|
23
|
5 |
|
public function __construct( |
24
|
|
|
string $ISOCode, |
25
|
|
|
string $symbol, |
26
|
|
|
int $numFractionalDigits, |
27
|
|
|
int $symbolPlacement = self::AFTER_PLACEMENT, |
28
|
|
|
string $name = '' |
29
|
|
|
) { |
30
|
5 |
|
$symbol = \trim($symbol); |
31
|
5 |
|
$name = \trim($name); |
32
|
|
|
|
33
|
5 |
|
if (!in_array($symbolPlacement, [self::BEFORE_PLACEMENT, self::AFTER_PLACEMENT])) { |
34
|
1 |
|
throw new InvalidArgumentException('Invalid symbol placement'); |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
if ($numFractionalDigits < 0 || $numFractionalDigits > 8) { |
38
|
1 |
|
throw new InvalidArgumentException('Invalid number of fractional digits'); |
39
|
|
|
} |
40
|
|
|
|
41
|
3 |
|
if ('' === $symbol) { |
42
|
2 |
|
throw new InvalidArgumentException('Empty symbol'); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$this->ISOCode = $ISOCode; |
46
|
1 |
|
$this->symbol = $symbol; |
47
|
1 |
|
$this->numFractionalDigits = $numFractionalDigits; |
48
|
1 |
|
$this->symbolPlacement = $symbolPlacement; |
49
|
1 |
|
$this->name = $name; |
50
|
1 |
|
} |
51
|
|
|
|
52
|
1 |
|
public function getISOCode(): string |
53
|
|
|
{ |
54
|
1 |
|
return $this->ISOCode; |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
public function getName(): string |
58
|
|
|
{ |
59
|
1 |
|
return $this->name; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
public function getSymbol(): string |
63
|
|
|
{ |
64
|
1 |
|
return $this->symbol; |
65
|
|
|
} |
66
|
|
|
|
67
|
1 |
|
public function getNumFractionalDigits(): int |
68
|
|
|
{ |
69
|
1 |
|
return $this->numFractionalDigits; |
70
|
|
|
} |
71
|
|
|
|
72
|
1 |
|
public function getSymbolPlacement(): int |
73
|
|
|
{ |
74
|
1 |
|
return $this->symbolPlacement; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
public function equals(CurrencyInterface $currencyType): bool |
78
|
|
|
{ |
79
|
4 |
|
if ($this === $currencyType) { |
80
|
1 |
|
return true; |
81
|
|
|
} |
82
|
|
|
|
83
|
3 |
|
if ($currencyType->getISOCode() !== $this->ISOCode) { |
84
|
1 |
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if (!( |
88
|
2 |
|
$currencyType->getName() === $this->name && |
89
|
2 |
|
$currencyType->getSymbol() === $this->symbol && |
90
|
2 |
|
$currencyType->getNumFractionalDigits() === $this->numFractionalDigits && |
91
|
2 |
|
$currencyType->getSymbolPlacement() === $this->symbolPlacement |
92
|
|
|
)) { |
93
|
1 |
|
throw new InconsistentCurrenciesError($this, $currencyType); |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|