Skip to content

Commit 0f957a0

Browse files
committed
fix: do not include the same data in MessyPhysicalAddress
Fixes #15
1 parent cf07669 commit 0f957a0

File tree

2 files changed

+94
-28
lines changed

2 files changed

+94
-28
lines changed

src/Geography/Address/Physical/MessyPhysicalAddress.php

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
use Talentify\ValueObject\Geography\Address\Street;
1313
use Talentify\ValueObject\StringUtils;
1414
use Talentify\ValueObject\ValueObject;
15+
use function count;
16+
use function is_object;
1517

1618
/**
1719
* A confuse physical address.
@@ -30,6 +32,8 @@ class MessyPhysicalAddress implements PhysicalAddress
3032
private $region;
3133
/** @var \Talentify\ValueObject\Geography\Address\Country|null */
3234
private $country;
35+
/** @var string */
36+
private $formattedAddress;
3337

3438
/**
3539
* @throws \InvalidArgumentException
@@ -40,67 +44,99 @@ public function __construct(
4044
?Region $region = null,
4145
?Country $country = null
4246
) {
43-
$this->setMessyAddress($messyAddress);
4447
$this->city = $city;
4548
$this->region = $region;
4649
$this->country = $country;
50+
$this->setMessyAddress($messyAddress);
51+
$this->setFormattedAddress();
4752
}
4853

4954
protected function setMessyAddress(string $messyAddress) : void
5055
{
5156
$normalized = StringUtils::trimSpacesWisely($messyAddress);
52-
if (empty($normalized)) {
53-
throw new InvalidArgumentException(sprintf('The value "%s" is invalid.', $messyAddress));
57+
if ($normalized === null || $normalized === '') {
58+
throw new InvalidArgumentException(sprintf('The given value "%s" is an empty string.', $messyAddress));
5459
}
5560

5661
$this->messyAddress = StringUtils::convertCaseToTitle($normalized);
5762
}
5863

64+
protected function setFormattedAddress() : void
65+
{
66+
$notNull = array_filter([$this->city, $this->region, $this->country], function ($element) {
67+
return $element !== null;
68+
});
69+
70+
if (count($notNull) === 0) {
71+
$this->formattedAddress = $this->messyAddress;
72+
return;
73+
}
74+
75+
$secondSegment = implode(', ', $notNull);
76+
if (stripos($this->messyAddress, $secondSegment) !== false) {
77+
$this->formattedAddress = $this->messyAddress;
78+
return;
79+
}
80+
81+
$this->formattedAddress = sprintf('%s, %s', $this->messyAddress, $secondSegment);
82+
}
83+
5984
public function getMessyAddress() : string
6085
{
6186
return $this->messyAddress;
6287
}
6388

89+
/**
90+
* {@inheritDoc}
91+
*/
6492
public function getStreet() : ?Street
6593
{
6694
return null;
6795
}
6896

97+
/**
98+
* {@inheritDoc}
99+
*/
69100
public function getCity() : ?City
70101
{
71102
return $this->city;
72103
}
73104

105+
/**
106+
* {@inheritDoc}
107+
*/
74108
public function getRegion() : ?Region
75109
{
76110
return $this->region;
77111
}
78112

113+
/**
114+
* {@inheritDoc}
115+
*/
79116
public function getPostalCode() : ?PostalCode
80117
{
81118
return null;
82119
}
83120

121+
/**
122+
* {@inheritDoc}
123+
*/
84124
public function getCountry() : ?Country
85125
{
86126
return $this->country;
87127
}
88128

129+
/**
130+
* {@inheritDoc}
131+
*/
89132
public function getAddress() : string
90133
{
91-
$messyAddress = $this->messyAddress;
92-
93-
$notNull = array_filter([$this->city, $this->region, $this->country], function ($element) {
94-
return $element !== null;
95-
});
96-
97-
if (\count($notNull) === 0) {
98-
return $this->messyAddress;
99-
}
100-
101-
return sprintf('%s, %s', $messyAddress, implode(', ', $notNull));
134+
return $this->formattedAddress;
102135
}
103136

137+
/**
138+
* {@inheritDoc}
139+
*/
104140
public function equals(?ValueObject $object) : bool
105141
{
106142
if (!$object instanceof self) {
@@ -113,22 +149,25 @@ public function equals(?ValueObject $object) : bool
113149
) &&
114150
(
115151
(null === $this->getCity() && null === $object->getCity()) ||
116-
(\is_object($this->getCity()) && $this->getCity()->equals($object->getCity())) ||
117-
(\is_object($object->getCity()) && $object->getCity()->equals($this->getCity()))
152+
(is_object($this->getCity()) && $this->getCity()->equals($object->getCity())) ||
153+
(is_object($object->getCity()) && $object->getCity()->equals($this->getCity()))
118154
) &&
119155
(
120156
(null === $this->getRegion() && null === $object->getRegion()) ||
121-
(\is_object($this->getRegion()) && $this->getRegion()->equals($object->getRegion())) ||
122-
(\is_object($object->getRegion()) && $object->getRegion()->equals($this->getRegion()))
157+
(is_object($this->getRegion()) && $this->getRegion()->equals($object->getRegion())) ||
158+
(is_object($object->getRegion()) && $object->getRegion()->equals($this->getRegion()))
123159
) &&
124160
(
125161
(null === $this->getCountry() && null === $object->getCountry()) ||
126-
(\is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) ||
127-
(\is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry()))
162+
(is_object($this->getCountry()) && $this->getCountry()->equals($object->getCountry())) ||
163+
(is_object($object->getCountry()) && $object->getCountry()->equals($this->getCountry()))
128164
)
129165
);
130166
}
131167

168+
/**
169+
* {@inheritDoc}
170+
*/
132171
public function __toString() : string
133172
{
134173
return $this->getAddress();

tests/Geography/Address/Physical/MessyPhysicalAddressTest.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,43 @@
1111

1212
class MessyPhysicalAddressTest extends ValueObjectTestCase
1313
{
14-
public function testWillGetFormattedAddress() : void
14+
/**
15+
* @dataProvider formattedAddressDataProvider
16+
*/
17+
public function testWillGetFormattedAddress(MessyPhysicalAddress $messyAddress, string $expected) : void
1518
{
16-
$messyAddress = new MessyPhysicalAddress('400 broad St, seattle');
17-
$this->assertEquals('400 Broad St, Seattle', $messyAddress->getAddress());
18-
19-
$messyAddress = new MessyPhysicalAddress('washington', null, new Region('king county'));
20-
$this->assertEquals('Washington, King County', $messyAddress->getAddress());
19+
$this->assertEquals($expected, $messyAddress->getAddress());
20+
}
2121

22-
$messyAddress = new MessyPhysicalAddress('400 broad st', new City('Seattle'), new Region('Washington'), CountryList::US());
23-
$this->assertEquals('400 Broad St, Seattle, Washington, US', $messyAddress->getAddress());
22+
public function formattedAddressDataProvider() : array
23+
{
24+
return [
25+
[
26+
new MessyPhysicalAddress('400 broad St, seattle'),
27+
'400 Broad St, Seattle'
28+
],
29+
[
30+
new MessyPhysicalAddress('washington', null, new Region('king county')),
31+
'Washington, King County'
32+
],
33+
[
34+
new MessyPhysicalAddress(
35+
'400 broad st',
36+
new City('Seattle'),
37+
new Region('Washington'),
38+
CountryList::US()
39+
),
40+
'400 Broad St, Seattle, Washington, US'
41+
],
42+
[
43+
new MessyPhysicalAddress('São Paulo, SP', new City('São Paulo'), new Region('SP')),
44+
'São Paulo, Sp',
45+
],
46+
[
47+
new MessyPhysicalAddress('Morristown, Nj', new City('Morristown'), new Region('NJ')),
48+
'Morristown, Nj',
49+
]
50+
];
2451
}
2552

2653
/**

0 commit comments

Comments
 (0)