-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCountry.php
More file actions
72 lines (59 loc) · 1.79 KB
/
Country.php
File metadata and controls
72 lines (59 loc) · 1.79 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?php
namespace MatchBot\Domain;
use MatchBot\Application\Assertion;
use PrinsFrank\Standards\Country\CountryAlpha2;
use PrinsFrank\Standards\Country\Groups\EU;
/**
* A nation state, such as the UK. Currently used in our card fee calculation logic, may be used elsewhere in future.
*/
readonly class Country
{
private function __construct(
public readonly CountryAlpha2 $alpha2
) {
}
/**
* @param string $countryCode ISO 3166 two letter code
*/
public static function fromAlpha2(string $countryCode): self
{
Assertion::regex($countryCode, '/^[A-Za-z]{2}$/');
$alpha2 = CountryAlpha2::tryFrom(strtoupper($countryCode));
if (! $alpha2) {
throw new \DomainException("Unrecognised Country code: '$countryCode'");
}
return new self($alpha2);
}
public static function fromEnum(CountryAlpha2 $alpha2): self
{
return new self($alpha2);
}
public static function fromAlpha2OrNull(?string $countryCode): ?self
{
if ($countryCode === null) {
return null;
}
return self::fromAlpha2($countryCode);
}
/** United Kingdom */
public static function GB(): self
{
return self::fromAlpha2('GB');
}
/**
* @return bool True if is either the UK or any EU member
*/
public function isEUOrUK(): bool
{
// We may want to use EEA later, but need to clarify our Stripe fee schedule first.
return $this->alpha2->isMemberOf(EU::class) || $this->alpha2 === CountryAlpha2::United_Kingdom;
}
public function __toString(): string
{
return "{$this->alpha2->name} (code {$this->alpha2->value})";
}
public function equals(self $that): bool
{
return $this->alpha2 === $that->alpha2;
}
}