|
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
|
|
|
/** |
|
12
|
|
|
* An area that are broadly divided by human impact characteristics such as |
|
13
|
|
|
* provinces, states, counties, townships, territories, etc. |
|
14
|
|
|
* |
|
15
|
|
|
* @see https://en.wikipedia.org/wiki/Region#Political_regions |
|
16
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-2 |
|
17
|
|
|
*/ |
|
18
|
|
|
class Region implements AddressElement |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $name; |
|
22
|
|
|
/** @var string|null */ |
|
23
|
|
|
private $isoAlpha2; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @param string|null $isoAlpha2 https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
|
27
|
|
|
* |
|
28
|
|
|
* @throws \InvalidArgumentException if supplied value is invalid. |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(string $name, ?string $isoAlpha2 = null) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->setName($name); |
|
33
|
|
|
$this->setIsoAlpha2($isoAlpha2); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
private function setName(string $name) : void |
|
37
|
|
|
{ |
|
38
|
|
|
$normalized = StringUtils::trimSpacesWisely($name); |
|
39
|
|
|
if (empty($normalized)) { |
|
40
|
|
|
throw new \InvalidArgumentException(sprintf('The value "%s" is not a valid region name.', $name)); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$this->name = StringUtils::convertCaseToTitle($normalized); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
public function getName() : string |
|
47
|
|
|
{ |
|
48
|
|
|
return $this->name; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @throws \InvalidArgumentException |
|
53
|
|
|
*/ |
|
54
|
|
|
private function setIsoAlpha2(?string $value) : void |
|
55
|
|
|
{ |
|
56
|
|
|
if ($value === null) { |
|
57
|
|
|
return; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
$normalized = StringUtils::removeSpaces($value); |
|
61
|
|
|
if (empty($normalized) || strlen($normalized) !== 2) { |
|
62
|
|
|
throw new InvalidArgumentException( |
|
63
|
|
|
sprintf('The value "%s" (%s) is not a valid ISO 3166-1 alpha 2.', $value, $normalized) |
|
64
|
|
|
); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->isoAlpha2 = StringUtils::convertCaseToUpper($normalized); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Returns the alpha-2 code for the ISO 3166-1. |
|
72
|
|
|
* |
|
73
|
|
|
* @see https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2 |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getIsoAlpha2() : ?string |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->isoAlpha2; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function equals(?ValueObject $object) : bool |
|
81
|
|
|
{ |
|
82
|
|
|
if (!$object instanceof self) { |
|
83
|
|
|
return false; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $object->getName() === $this->getName() && |
|
87
|
|
|
$object->getIsoAlpha2() === $this->getIsoAlpha2(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
public function getFormatted() : string |
|
91
|
|
|
{ |
|
92
|
|
|
return sprintf('%s %s', $this->getName(), $this->getIsoAlpha2()); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function __toString() : string |
|
96
|
|
|
{ |
|
97
|
|
|
if ($this->isoAlpha2 !== null) { |
|
98
|
|
|
return strtoupper($this->isoAlpha2); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return $this->name; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function jsonSerialize() |
|
105
|
|
|
{ |
|
106
|
|
|
return [ |
|
107
|
|
|
'name' => $this->name, |
|
108
|
|
|
'isoAlpha2' => $this->isoAlpha2, |
|
109
|
|
|
]; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|