|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Talentify\ValueObject\Geography\Address; |
|
6
|
|
|
|
|
7
|
|
|
use InvalidArgumentException; |
|
8
|
|
|
use Talentify\ValueObject\StringUtils; |
|
9
|
|
|
use Talentify\ValueObject\ValueObject; |
|
10
|
|
|
|
|
11
|
|
|
use function strlen; |
|
12
|
|
|
|
|
13
|
|
|
class Country implements AddressElement |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $name; |
|
17
|
|
|
/** @var string|null */ |
|
18
|
|
|
private $isoAlpha2; |
|
19
|
|
|
/** @var string|null */ |
|
20
|
|
|
private $isoAlpha3; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string|null $isoAlpha2 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
|
24
|
|
|
* @param string|null $isoAlpha3 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 |
|
25
|
|
|
* |
|
26
|
|
|
* @throws \InvalidArgumentException if supplied value is invalid. |
|
27
|
|
|
* |
|
28
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1 |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
string $name, |
|
32
|
|
|
?string $isoAlpha2 = null, |
|
33
|
|
|
?string $isoAlpha3 = null |
|
34
|
|
|
) { |
|
35
|
|
|
$this->setName($name); |
|
36
|
|
|
$this->setIsoAlpha2($isoAlpha2); |
|
37
|
|
|
$this->setIsoAlpha3($isoAlpha3); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
private function setName(string $name) : void |
|
41
|
|
|
{ |
|
42
|
|
|
$normalized = StringUtils::trimSpacesWisely($name); |
|
43
|
|
|
if (empty($normalized)) { |
|
44
|
|
|
throw new InvalidArgumentException(sprintf('The value "%s" is not a valid country name.', $name)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if (strlen($normalized) === 2) { |
|
48
|
|
|
$this->setIsoAlpha2($normalized); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (strlen($normalized) === 3) { |
|
52
|
|
|
$this->setIsoAlpha3($normalized); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$this->name = StringUtils::convertCaseToTitle($normalized); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getName() : string |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->name; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
private function setIsoAlpha2(?string $value) : void |
|
64
|
|
|
{ |
|
65
|
|
|
if ($value === null) { |
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$normalized = StringUtils::removeSpaces($value); |
|
70
|
|
|
if (empty($normalized) || strlen($normalized) !== 2) { |
|
71
|
|
|
throw new InvalidArgumentException( |
|
72
|
|
|
sprintf('The value "%s" (%s) is not a valid ISO 3166-1 alpha 2.', $value, $normalized) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->isoAlpha2 = StringUtils::convertCaseToUpper($normalized); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Returns the alpha-2 code for the ISO 3166-1. |
|
81
|
|
|
* |
|
82
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getIsoAlpha2() : ?string |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->isoAlpha2; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
private function setIsoAlpha3(?string $value = null) : void |
|
90
|
|
|
{ |
|
91
|
|
|
if ($value === null) { |
|
92
|
|
|
return; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
$normalized = StringUtils::removeSpaces($value); |
|
96
|
|
|
if (empty($normalized) || strlen($normalized) !== 3) { |
|
97
|
|
|
throw new InvalidArgumentException( |
|
98
|
|
|
sprintf('The value "%s" (%s) is not a valid ISO 3166-1 alpha 3.', $value, $normalized) |
|
99
|
|
|
); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$this->isoAlpha3 = StringUtils::convertCaseToUpper($normalized); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Returns the alpha-3 code for the ISO 3166-1. |
|
107
|
|
|
* |
|
108
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-3 |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getIsoAlpha3() : ?string |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->isoAlpha3; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function equals(?ValueObject $object) : bool |
|
116
|
|
|
{ |
|
117
|
|
|
if (!$object instanceof self) { |
|
118
|
|
|
return false; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $object->getName() === $this->getName() && |
|
122
|
|
|
$object->getIsoAlpha2() === $this->getIsoAlpha2() && |
|
123
|
|
|
$object->getIsoAlpha3() === $this->getIsoAlpha3(); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
public function getFormatted() : string |
|
127
|
|
|
{ |
|
128
|
|
|
return sprintf('%s %s', $this->getName(), $this->getIsoAlpha2()); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
public function __toString() : string |
|
132
|
|
|
{ |
|
133
|
|
|
if ($this->isoAlpha2 !== null) { |
|
134
|
|
|
return strtoupper($this->isoAlpha2); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
if ($this->isoAlpha3 !== null) { |
|
138
|
|
|
return strtoupper($this->isoAlpha3); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
return $this->name; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function jsonSerialize() |
|
145
|
|
|
{ |
|
146
|
|
|
return [ |
|
147
|
|
|
'name' => $this->name, |
|
148
|
|
|
'isoAlpha2' => $this->isoAlpha2, |
|
149
|
|
|
'isoAlpha3' => $this->isoAlpha3, |
|
150
|
|
|
]; |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
|