-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCurrency.php
More file actions
53 lines (45 loc) · 1.42 KB
/
Currency.php
File metadata and controls
53 lines (45 loc) · 1.42 KB
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
<?php
namespace MatchBot\Domain;
use MatchBot\Application\Assertion;
/**
* @psalm-immutable
*/
enum Currency: string
{
case GBP = 'GBP';
case USD = 'USD';
case SEK = 'SEK';
case EUR = 'EUR';
public static function fromIsoCode(string $isoCode): self
{
Assertion::length($isoCode, 3);
Assertion::alnum($isoCode);
$isoCode = strtoupper($isoCode);
// other currencies have some tests but are not fully supported. USD is also not fully tested but we have considered
// USA a little more and have data relating to that in prod.
if (! defined('RUNNING_UNIT_TESTS') && ! \in_array($isoCode, ['GBP', 'USD'], true)) {
throw new \UnexpectedValueException("Unexpected Currency ISO Code " . $isoCode);
}
return self::tryFrom($isoCode) ??
throw new \UnexpectedValueException("Unexpected Currency ISO Code " . $isoCode);
}
/**
* E.g. '£', '$' or '€'
*/
public function symbol(): string
{
return match ($this) {
self::GBP => '£',
self::USD => '$',
default => throw new \Exception("Unexpected currency " . $this->isoCode()),
};
}
/**
* @param 'upper'|'lower' $case
* @return string
*/
public function isoCode(string $case = 'upper'): string
{
return $case === 'upper' ? strtoupper($this->name) : strtolower($this->name);
}
}