1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Talentify\ValueObject\Geography\Address\Physical; |
6
|
|
|
|
7
|
|
|
use Talentify\ValueObject\Geography\Address\City; |
8
|
|
|
use Talentify\ValueObject\Geography\Address\Country; |
9
|
|
|
use Talentify\ValueObject\Geography\Address\PostalCode; |
10
|
|
|
use Talentify\ValueObject\Geography\Address\Region; |
11
|
|
|
use Talentify\ValueObject\Geography\Address\Street; |
12
|
|
|
use Talentify\ValueObject\StringUtils; |
13
|
|
|
use Talentify\ValueObject\ValueObject; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* A generic structure for a physical address. |
17
|
|
|
*/ |
18
|
|
|
class GenericPhysicalAddress implements PhysicalAddress |
19
|
|
|
{ |
20
|
|
|
/** @var \Talentify\ValueObject\Geography\Address\Street|null */ |
21
|
|
|
protected $street; |
22
|
|
|
/** @var \Talentify\ValueObject\Geography\Address\City|null */ |
23
|
|
|
protected $city; |
24
|
|
|
/** @var \Talentify\ValueObject\Geography\Address\Region|null */ |
25
|
|
|
protected $region; |
26
|
|
|
/** @var \Talentify\ValueObject\Geography\Address\PostalCode|null */ |
27
|
|
|
protected $postalCode; |
28
|
|
|
/** @var \Talentify\ValueObject\Geography\Address\Country|null */ |
29
|
|
|
protected $country; |
30
|
|
|
/** @var string */ |
31
|
|
|
protected $formattedAddress; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
?Street $street = null, |
38
|
|
|
?City $city = null, |
39
|
|
|
?Region $region = null, |
40
|
|
|
?PostalCode $postalCode = null, |
41
|
|
|
?Country $country = null, |
42
|
|
|
?string $formattedAddress = null |
43
|
|
|
) { |
44
|
|
|
$this->street = $street; |
45
|
|
|
$this->city = $city; |
46
|
|
|
$this->region = $region; |
47
|
|
|
$this->postalCode = $postalCode; |
48
|
|
|
$this->country = $country; |
49
|
|
|
$this->formattedAddress = $formattedAddress; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getStreet() : ?Street |
53
|
|
|
{ |
54
|
|
|
return $this->street; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getCity() : ?City |
58
|
|
|
{ |
59
|
|
|
return $this->city; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function getRegion() : ?Region |
63
|
|
|
{ |
64
|
|
|
return $this->region; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function getCountry() : ?Country |
68
|
|
|
{ |
69
|
|
|
return $this->country; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function getPostalCode() : ?PostalCode |
73
|
|
|
{ |
74
|
|
|
return $this->postalCode; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function equals(?ValueObject $object) : bool |
78
|
|
|
{ |
79
|
|
|
if (!$object instanceof self) { |
80
|
|
|
return false; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return ( |
84
|
|
|
( |
85
|
|
|
(null === $this->getStreet() && null === $object->getStreet()) || |
86
|
|
|
(\is_object($this->getStreet()) && $this->getStreet()->equals($object->getStreet())) || |
87
|
|
|
(\is_object($object->getStreet()) && $object->getStreet()->equals($this->getStreet())) |
88
|
|
|
) && |
89
|
|
|
( |
90
|
|
|
(null === $this->getCity() && null === $object->getCity()) || |
91
|
|
|
(\is_object($this->getCity()) && $this->getCity()->equals($object->getCity())) || |
92
|
|
|
(\is_object($object->getCity()) && $object->getCity()->equals($this->getCity())) |
93
|
|
|
) && |
94
|
|
|
( |
95
|
|
|
(null === $this->getRegion() && null === $object->getRegion()) || |
96
|
|
|
(\is_object($this->getRegion()) && $this->getRegion()->equals($object->getRegion())) || |
97
|
|
|
(\is_object($object->getRegion()) && $object->getRegion()->equals($this->getRegion())) |
98
|
|
|
) && |
99
|
|
|
( |
100
|
|
|
(null === $this->getPostalCode() && null === $object->getPostalCode()) || |
101
|
|
|
(\is_object($this->getPostalCode()) && $this->getPostalCode()->equals($object->getPostalCode())) || |
102
|
|
|
(\is_object($object->getPostalCode()) && $object->getPostalCode()->equals($this->getPostalCode())) |
103
|
|
|
) && |
104
|
|
|
( |
105
|
|
|
(null === $this->getCountry() && null === $object->getCountry()) || |
106
|
|
|
(\is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) || |
107
|
|
|
(\is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry())) |
108
|
|
|
) |
109
|
|
|
); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function getAddress() : string |
113
|
|
|
{ |
114
|
|
|
if ($this->formattedAddress !== null) { |
115
|
|
|
return $this->formattedAddress; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$street = $this->getStreet() ? $this->getStreet()->getFormatted() : ''; |
119
|
|
|
$city = $this->getCity() ? $this->getCity()->getFormatted() : ''; |
120
|
|
|
$region = $this->getRegion() ? $this->getRegion()->__toString() : ''; |
121
|
|
|
$postalCode = $this->getPostalCode() ? $this->getPostalCode()->getFormatted() : ''; |
122
|
|
|
$country = $this->getCountry() ? $this->getCountry()->__toString() : ''; |
123
|
|
|
|
124
|
|
|
$values = [$street, $city, $region, $postalCode, $country]; |
125
|
|
|
$nonEmptyValues = []; |
126
|
|
|
foreach ($values as $value) { |
127
|
|
|
if($value !== ''){ |
128
|
|
|
$nonEmptyValues[] = $value; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
$formatted = implode(', ', $nonEmptyValues); |
133
|
|
|
|
134
|
|
|
return StringUtils::trimSpacesWisely($formatted) ?? ''; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function __toString() : string |
138
|
|
|
{ |
139
|
|
|
return $this->getAddress(); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|