1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ShippoClient\Http\Request\Addresses; |
4
|
|
|
|
5
|
|
|
use ShippoClient\Http\Request\CommonParameter; |
6
|
|
|
use TurmericSpice\Container\InvalidAttributeException; |
7
|
|
|
use TurmericSpice\ReadWriteAttributes; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Address objects are used for creating Shipment, obtaining Rates and printing Labels, |
11
|
|
|
* and thus are one of the fundamental building blocks of the Shippo API. |
12
|
|
|
*/ |
13
|
|
|
class CreateObject extends CommonParameter |
14
|
|
|
{ |
15
|
|
|
use ReadWriteAttributes { |
16
|
|
|
toArray as public __toArray; |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
const OBJECT_PURPOSE_QUOTE = 'QUOTE'; |
20
|
|
|
const OBJECT_PURPOSE_PURCHASE = 'PURCHASE'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Quote Address can be generated by only passing selected address information. |
24
|
|
|
* Quote Address can only be used for obtaining shipping Rate quotes |
25
|
|
|
* and cannot be used to purchase Labels. |
26
|
|
|
* Address that should be used for purchases must be fully entered, |
27
|
|
|
* i.e., a complete street address with all required fields must be passed. |
28
|
|
|
* |
29
|
|
|
* Required |
30
|
|
|
* |
31
|
|
|
* @return string |
32
|
|
|
* @throws InvalidAttributeException |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function getObjectPurpose() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$allowed = [static::OBJECT_PURPOSE_QUOTE, static::OBJECT_PURPOSE_PURCHASE]; |
37
|
|
|
|
38
|
|
|
return $this->attributes->mustHave('object_purpose')->asString(function ($value) use ($allowed) { |
39
|
|
|
return in_array($value, $allowed, true); |
40
|
|
|
}); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* First and Last Name of the addressee |
45
|
|
|
* |
46
|
|
|
* Required for purchase |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
* @throws InvalidAttributeException |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function getName() |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
if ($this->getObjectPurpose() === static::OBJECT_PURPOSE_PURCHASE) { |
54
|
|
|
return $this->attributes->mustHave('name')->asString(); |
55
|
|
|
} |
56
|
|
|
return $this->attributes->mayHave('name')->asString(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Company Name |
61
|
|
|
* |
62
|
|
|
* Optional |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getCompany() |
67
|
|
|
{ |
68
|
|
|
return $this->attributes->mayHave('company')->asString(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* First street line, which is usually the street name. |
73
|
|
|
* Note that a building's street number should be passed separately (see below). |
74
|
|
|
* |
75
|
|
|
* Required for purchase |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
* @throws InvalidAttributeException |
79
|
|
|
*/ |
80
|
|
View Code Duplication |
public function getStreet1() |
|
|
|
|
81
|
|
|
{ |
82
|
|
|
if ($this->getObjectPurpose() === static::OBJECT_PURPOSE_PURCHASE) { |
83
|
|
|
return $this->attributes->mustHave('street1')->asString(); |
84
|
|
|
} |
85
|
|
|
return $this->attributes->mayHave('street1')->asString(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Street number of the addressed building. |
90
|
|
|
* This field can be included in street1 for all carriers except for DHL Paket (Germany). |
91
|
|
|
* |
92
|
|
|
* Optional |
93
|
|
|
* |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
|
|
public function getStreetNo() |
97
|
|
|
{ |
98
|
|
|
return $this->attributes->mayHave('street_no')->asString(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Second street line. |
103
|
|
|
* |
104
|
|
|
* Optional |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
public function getStreet2() |
109
|
|
|
{ |
110
|
|
|
return $this->attributes->mayHave('street2')->asString(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Name of a city. When creating a Quote Address, sending a city is optional but will yield more accurate Rates. |
115
|
|
|
* Please bear in mind that city names may be ambiguous (there are 34 Springfields in the US). |
116
|
|
|
* Passing a state or a ZIP code (see below), if known, will yield more accurate results. |
117
|
|
|
* |
118
|
|
|
* Required for purchase |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
* @throws InvalidAttributeException |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function getCity() |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
if ($this->getObjectPurpose() === static::OBJECT_PURPOSE_PURCHASE) { |
126
|
|
|
return $this->attributes->mustHave('city')->asString(); |
127
|
|
|
} |
128
|
|
|
return $this->attributes->mayHave('city')->asString(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Postal code of an Address. When creating a Quote Address, |
133
|
|
|
* sending a ZIP is optional but will yield more accurate Rates. |
134
|
|
|
* |
135
|
|
|
* Required for purchase |
136
|
|
|
* |
137
|
|
|
* @return string |
138
|
|
|
* @throws InvalidAttributeException |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function getZip() |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if ($this->getObjectPurpose() === static::OBJECT_PURPOSE_PURCHASE) { |
143
|
|
|
return $this->attributes->mustHave('zip')->asString(); |
144
|
|
|
} |
145
|
|
|
return $this->attributes->mayHave('zip')->asString(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* State values are only required for shipments from |
150
|
|
|
* the United States and Canada(most carriers only accept two-character state abbreviations). |
151
|
|
|
* However, to receive more accurate quotes, passing it is generally recommended. |
152
|
|
|
* |
153
|
|
|
* Required for purchase for some countries |
154
|
|
|
* |
155
|
|
|
* @return string |
156
|
|
|
* @throws InvalidAttributeException |
157
|
|
|
*/ |
158
|
|
|
public function getState() |
159
|
|
|
{ |
160
|
|
|
if (in_array($this->getCountry(), ['US', 'CA'], true)) { |
161
|
|
|
return $this->attributes->mustHave('state')->asString(); |
162
|
|
|
} |
163
|
|
|
return $this->attributes->mayHave('state')->asString(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Example: 'US' or 'DE'. |
168
|
|
|
* All accepted values can be found on the Official ISO Website(http://www.iso.org/). |
169
|
|
|
* Sending a country is always required. |
170
|
|
|
* |
171
|
|
|
* Required |
172
|
|
|
* |
173
|
|
|
* @return string |
174
|
|
|
* @throws InvalidAttributeException |
175
|
|
|
*/ |
176
|
|
|
public function getCountry() |
177
|
|
|
{ |
178
|
|
|
return $this->attributes->mustHave('country')->asString(function ($country) { |
179
|
|
|
return (bool)preg_match('/^[A-Z]{2}$/', $country); |
180
|
|
|
}); |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Address containing a phone number allow carriers to call the recipient when delivering the Parcel. |
185
|
|
|
* This increases the probability of delivery and helps to avoid accessorial charges after a Parcel has been shipped. |
|
|
|
|
186
|
|
|
* |
187
|
|
|
* Optional for domestic, required for international |
188
|
|
|
* |
189
|
|
|
* @return string |
190
|
|
|
* @throws InvalidAttributeException |
191
|
|
|
*/ |
192
|
|
|
public function getPhone() |
193
|
|
|
{ |
194
|
|
|
return $this->attributes->mayHave('phone')->asString(); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* E-mail address of the contact person, RFC3696/5321-compliant. |
199
|
|
|
* |
200
|
|
|
* Required for purchase |
201
|
|
|
* |
202
|
|
|
* @return string |
203
|
|
|
* @throws InvalidAttributeException |
204
|
|
|
*/ |
205
|
|
|
public function getEmail() |
206
|
|
|
{ |
207
|
|
|
$validation = function ($email) { |
208
|
|
|
return filter_var($email, FILTER_VALIDATE_EMAIL); |
209
|
|
|
}; |
210
|
|
|
if ($this->getObjectPurpose() === static::OBJECT_PURPOSE_PURCHASE) { |
211
|
|
|
return $this->attributes->mustHave('email')->asString($validation); |
212
|
|
|
} |
213
|
|
|
return $this->attributes->mayHave('email')->asString($validation); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* Indicates whether the address provided is a residential address or not |
218
|
|
|
* |
219
|
|
|
* Optional |
220
|
|
|
* |
221
|
|
|
* @return null|bool |
222
|
|
|
*/ |
223
|
|
View Code Duplication |
public function getIsResidential() |
|
|
|
|
224
|
|
|
{ |
225
|
|
|
$is_residential = $this->attributes->mayHave('is_residential')->value(); |
226
|
|
|
if ($is_residential === null) { |
227
|
|
|
return null; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return (bool)$is_residential; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
public function toArray() |
234
|
|
|
{ |
235
|
|
|
return array_filter($this->__toArray()); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.