|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
namespace Thunder\Currenz; |
|
4
|
|
|
|
|
5
|
|
|
use Prophecy\Exception\InvalidArgumentException; |
|
6
|
|
|
|
|
7
|
|
|
abstract class AbstractCurrency implements CurrencyInterface |
|
8
|
|
|
{ |
|
9
|
|
|
private $name; |
|
10
|
|
|
private $code; |
|
11
|
|
|
private $digits; |
|
12
|
|
|
private $countries; |
|
13
|
|
|
private $number; |
|
14
|
|
|
|
|
15
|
542 |
|
public function __construct(string $name, string $code, ?int $digits, array $countries, string $number) |
|
16
|
|
|
{ |
|
17
|
542 |
|
if(empty($name)) { |
|
18
|
1 |
|
throw new \InvalidArgumentException('Currency name must not be empty!'); |
|
19
|
|
|
} |
|
20
|
541 |
|
if(!preg_match('~^[A-Z]{3}$~', $code)) { |
|
21
|
1 |
|
throw new \InvalidArgumentException(sprintf('ISO4217 currency code must be an uppercase three-letter string, `%s` given!', $code)); |
|
22
|
|
|
} |
|
23
|
540 |
|
if($digits !== null && $digits < 0) { |
|
24
|
1 |
|
throw new \InvalidArgumentException(sprintf('Currency digits must be a non-negative integer or null, `%s` given!', $digits)); |
|
25
|
|
|
} |
|
26
|
539 |
|
foreach($countries as $country) { |
|
27
|
539 |
|
if(false === is_string($country)) { |
|
28
|
1 |
|
throw new InvalidArgumentException('Currency country name must be a string!'); |
|
29
|
|
|
} |
|
30
|
538 |
|
if(empty($country)) { |
|
31
|
538 |
|
throw new \InvalidArgumentException('Currency country name must not be empty!'); |
|
32
|
|
|
} |
|
33
|
|
|
} |
|
34
|
537 |
|
if(!preg_match('~^[0-9]{3}$~', $number)) { |
|
35
|
1 |
|
throw new \InvalidArgumentException(sprintf('ISO4217 currency number must be a three-digit string, `%s` given!', $number)); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
536 |
|
$this->name = $name; |
|
39
|
536 |
|
$this->code = $code; |
|
40
|
536 |
|
$this->digits = $digits; |
|
41
|
536 |
|
$this->countries = $countries; |
|
42
|
536 |
|
$this->number = $number; |
|
43
|
536 |
|
} |
|
44
|
|
|
|
|
45
|
536 |
|
public function getName(): string |
|
46
|
|
|
{ |
|
47
|
536 |
|
return $this->name; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
536 |
|
public function getCode(): string |
|
51
|
|
|
{ |
|
52
|
536 |
|
return $this->code; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
536 |
|
public function getDigits(): ?int |
|
56
|
|
|
{ |
|
57
|
536 |
|
return $this->digits; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
536 |
|
public function getUnits(): int |
|
61
|
|
|
{ |
|
62
|
536 |
|
return null === $this->digits || 0 === $this->digits ? 0 : 10 ** $this->digits; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
536 |
|
public function getCountries(): array |
|
66
|
|
|
{ |
|
67
|
536 |
|
return $this->countries; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
536 |
|
public function getNumber(): string |
|
71
|
|
|
{ |
|
72
|
536 |
|
return $this->number; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
536 |
|
public function hasName(string $name): bool |
|
76
|
|
|
{ |
|
77
|
536 |
|
return $this->name === $name; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
536 |
|
public function hasCode(string $code): bool |
|
81
|
|
|
{ |
|
82
|
536 |
|
return $this->code === $code; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
536 |
|
public function hasCountry(string $country): bool |
|
86
|
|
|
{ |
|
87
|
536 |
|
return in_array($country, $this->countries, true); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
536 |
|
public function hasNumber(string $number): bool |
|
91
|
|
|
{ |
|
92
|
536 |
|
return $this->number === $number; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|