12
12
use Talentify \ValueObject \Geography \Address \Street ;
13
13
use Talentify \ValueObject \StringUtils ;
14
14
use Talentify \ValueObject \ValueObject ;
15
+ use function count ;
16
+ use function is_object ;
15
17
16
18
/**
17
19
* A confuse physical address.
@@ -30,6 +32,8 @@ class MessyPhysicalAddress implements PhysicalAddress
30
32
private $ region ;
31
33
/** @var \Talentify\ValueObject\Geography\Address\Country|null */
32
34
private $ country ;
35
+ /** @var string */
36
+ private $ formattedAddress ;
33
37
34
38
/**
35
39
* @throws \InvalidArgumentException
@@ -40,67 +44,99 @@ public function __construct(
40
44
?Region $ region = null ,
41
45
?Country $ country = null
42
46
) {
43
- $ this ->setMessyAddress ($ messyAddress );
44
47
$ this ->city = $ city ;
45
48
$ this ->region = $ region ;
46
49
$ this ->country = $ country ;
50
+ $ this ->setMessyAddress ($ messyAddress );
51
+ $ this ->setFormattedAddress ();
47
52
}
48
53
49
54
protected function setMessyAddress (string $ messyAddress ) : void
50
55
{
51
56
$ 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 ));
54
59
}
55
60
56
61
$ this ->messyAddress = StringUtils::convertCaseToTitle ($ normalized );
57
62
}
58
63
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
+
59
84
public function getMessyAddress () : string
60
85
{
61
86
return $ this ->messyAddress ;
62
87
}
63
88
89
+ /**
90
+ * {@inheritDoc}
91
+ */
64
92
public function getStreet () : ?Street
65
93
{
66
94
return null ;
67
95
}
68
96
97
+ /**
98
+ * {@inheritDoc}
99
+ */
69
100
public function getCity () : ?City
70
101
{
71
102
return $ this ->city ;
72
103
}
73
104
105
+ /**
106
+ * {@inheritDoc}
107
+ */
74
108
public function getRegion () : ?Region
75
109
{
76
110
return $ this ->region ;
77
111
}
78
112
113
+ /**
114
+ * {@inheritDoc}
115
+ */
79
116
public function getPostalCode () : ?PostalCode
80
117
{
81
118
return null ;
82
119
}
83
120
121
+ /**
122
+ * {@inheritDoc}
123
+ */
84
124
public function getCountry () : ?Country
85
125
{
86
126
return $ this ->country ;
87
127
}
88
128
129
+ /**
130
+ * {@inheritDoc}
131
+ */
89
132
public function getAddress () : string
90
133
{
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 ;
102
135
}
103
136
137
+ /**
138
+ * {@inheritDoc}
139
+ */
104
140
public function equals (?ValueObject $ object ) : bool
105
141
{
106
142
if (!$ object instanceof self) {
@@ -113,22 +149,25 @@ public function equals(?ValueObject $object) : bool
113
149
) &&
114
150
(
115
151
(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 ()))
118
154
) &&
119
155
(
120
156
(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 ()))
123
159
) &&
124
160
(
125
161
(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 ()))
128
164
)
129
165
);
130
166
}
131
167
168
+ /**
169
+ * {@inheritDoc}
170
+ */
132
171
public function __toString () : string
133
172
{
134
173
return $ this ->getAddress ();
0 commit comments