1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Rinvex\Addresses\Models; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use Rinvex\Cacheable\CacheableEloquent; |
9
|
|
|
use Illuminate\Database\Eloquent\Builder; |
10
|
|
|
use Jackpopp\GeoDistance\GeoDistanceTrait; |
11
|
|
|
use Rinvex\Support\Traits\ValidatingTrait; |
12
|
|
|
use Rinvex\Addresses\Contracts\AddressContract; |
13
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Rinvex\Addresses\Models\Address. |
17
|
|
|
* |
18
|
|
|
* @property int $id |
19
|
|
|
* @property int $addressable_id |
20
|
|
|
* @property string $addressable_type |
21
|
|
|
* @property string $label |
22
|
|
|
* @property string $name_prefix |
23
|
|
|
* @property string $first_name |
24
|
|
|
* @property string $middle_name |
25
|
|
|
* @property string $last_name |
26
|
|
|
* @property string $name_suffix |
27
|
|
|
* @property string $organization |
28
|
|
|
* @property string $country_code |
29
|
|
|
* @property string $street |
30
|
|
|
* @property string $state |
31
|
|
|
* @property string $city |
32
|
|
|
* @property string $postal_code |
33
|
|
|
* @property float $lat |
34
|
|
|
* @property float $lng |
35
|
|
|
* @property bool $is_primary |
36
|
|
|
* @property bool $is_billing |
37
|
|
|
* @property bool $is_shipping |
38
|
|
|
* @property \Carbon\Carbon|null $created_at |
39
|
|
|
* @property \Carbon\Carbon|null $updated_at |
40
|
|
|
* @property-read \Illuminate\Database\Eloquent\Model|\Eloquent $addressable |
41
|
|
|
* |
42
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address inCountry($countryCode) |
43
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address inLanguage($languageCode) |
44
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isBilling() |
45
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isPrimary() |
46
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address isShipping() |
47
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address outside($distance, $measurement = null, $lat = null, $lng = null) |
|
|
|
|
48
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereAddressableId($value) |
49
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereAddressableType($value) |
50
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCity($value) |
51
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCountryCode($value) |
52
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereCreatedAt($value) |
53
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereFirstName($value) |
54
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereId($value) |
55
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsBilling($value) |
56
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsPrimary($value) |
57
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereIsShipping($value) |
58
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLabel($value) |
59
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLastName($value) |
60
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLat($value) |
61
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereLng($value) |
62
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereMiddleName($value) |
63
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereNamePrefix($value) |
64
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereNameSuffix($value) |
65
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereOrganization($value) |
66
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address wherePostalCode($value) |
67
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereState($value) |
68
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereStreet($value) |
69
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address whereUpdatedAt($value) |
70
|
|
|
* @method static \Illuminate\Database\Eloquent\Builder|\Rinvex\Addresses\Models\Address within($distance, $measurement = null, $lat = null, $lng = null) |
|
|
|
|
71
|
|
|
* @mixin \Eloquent |
72
|
|
|
*/ |
73
|
|
|
class Address extends Model implements AddressContract |
74
|
|
|
{ |
75
|
|
|
use ValidatingTrait; |
76
|
|
|
use GeoDistanceTrait; |
77
|
|
|
use CacheableEloquent; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected $fillable = [ |
83
|
|
|
'addressable_id', |
84
|
|
|
'addressable_type', |
85
|
|
|
'label', |
86
|
|
|
'name_prefix', |
87
|
|
|
'first_name', |
88
|
|
|
'middle_name', |
89
|
|
|
'last_name', |
90
|
|
|
'name_suffix', |
91
|
|
|
'organization', |
92
|
|
|
'country_code', |
93
|
|
|
'street', |
94
|
|
|
'state', |
95
|
|
|
'city', |
96
|
|
|
'postal_code', |
97
|
|
|
'lat', |
98
|
|
|
'lng', |
99
|
|
|
'is_primary', |
100
|
|
|
'is_billing', |
101
|
|
|
'is_shipping', |
102
|
|
|
]; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
protected $casts = [ |
108
|
|
|
'addressable_id' => 'integer', |
109
|
|
|
'addressable_type' => 'string', |
110
|
|
|
'label' => 'string', |
111
|
|
|
'name_prefix' => 'string', |
112
|
|
|
'first_name' => 'string', |
113
|
|
|
'middle_name' => 'string', |
114
|
|
|
'last_name' => 'string', |
115
|
|
|
'name_suffix' => 'string', |
116
|
|
|
'organization' => 'string', |
117
|
|
|
'country_code' => 'string', |
118
|
|
|
'street' => 'string', |
119
|
|
|
'state' => 'string', |
120
|
|
|
'city' => 'string', |
121
|
|
|
'postal_code' => 'string', |
122
|
|
|
'lat' => 'float', |
123
|
|
|
'lng' => 'float', |
124
|
|
|
'is_primary' => 'boolean', |
125
|
|
|
'is_billing' => 'boolean', |
126
|
|
|
'is_shipping' => 'boolean', |
127
|
|
|
'deleted_at' => 'datetime', |
128
|
|
|
]; |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* {@inheritdoc} |
132
|
|
|
*/ |
133
|
|
|
protected $observables = [ |
134
|
|
|
'validating', |
135
|
|
|
'validated', |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* The default rules that the model will validate against. |
140
|
|
|
* |
141
|
|
|
* @var array |
142
|
|
|
*/ |
143
|
|
|
protected $rules = [ |
144
|
|
|
'addressable_id' => 'required|integer', |
145
|
|
|
'addressable_type' => 'required|string|max:150', |
146
|
|
|
'label' => 'nullable|string|max:150', |
147
|
|
|
'name_prefix' => 'nullable|string|max:150', |
148
|
|
|
'first_name' => 'nullable|string|max:150', |
149
|
|
|
'middle_name' => 'nullable|string|max:150', |
150
|
|
|
'last_name' => 'nullable|string|max:150', |
151
|
|
|
'name_suffix' => 'nullable|string|max:150', |
152
|
|
|
'organization' => 'nullable|string|max:150', |
153
|
|
|
'country_code' => 'nullable|alpha|size:2|country', |
154
|
|
|
'street' => 'nullable|string|max:150', |
155
|
|
|
'state' => 'nullable|string|max:150', |
156
|
|
|
'city' => 'nullable|string|max:150', |
157
|
|
|
'postal_code' => 'nullable|string|max:150', |
158
|
|
|
'lat' => 'nullable|numeric', |
159
|
|
|
'lng' => 'nullable|numeric', |
160
|
|
|
'is_primary' => 'sometimes|boolean', |
161
|
|
|
'is_billing' => 'sometimes|boolean', |
162
|
|
|
'is_shipping' => 'sometimes|boolean', |
163
|
|
|
]; |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Whether the model should throw a |
167
|
|
|
* ValidationException if it fails validation. |
168
|
|
|
* |
169
|
|
|
* @var bool |
170
|
|
|
*/ |
171
|
|
|
protected $throwValidationExceptions = true; |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Create a new Eloquent model instance. |
175
|
|
|
* |
176
|
|
|
* @param array $attributes |
177
|
|
|
*/ |
178
|
|
|
public function __construct(array $attributes = []) |
179
|
|
|
{ |
180
|
|
|
parent::__construct($attributes); |
181
|
|
|
|
182
|
|
|
$this->setTable(config('rinvex.addresses.tables.addresses')); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get the owner model of the address. |
187
|
|
|
* |
188
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\MorphTo |
189
|
|
|
*/ |
190
|
|
|
public function addressable(): MorphTo |
191
|
|
|
{ |
192
|
|
|
return $this->morphTo(); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Scope primary addresses. |
197
|
|
|
* |
198
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
199
|
|
|
* |
200
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
201
|
|
|
*/ |
202
|
|
|
public function scopeIsPrimary(Builder $builder): Builder |
203
|
|
|
{ |
204
|
|
|
return $builder->where('is_primary', true); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Scope billing addresses. |
209
|
|
|
* |
210
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
211
|
|
|
* |
212
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
213
|
|
|
*/ |
214
|
|
|
public function scopeIsBilling(Builder $builder): Builder |
215
|
|
|
{ |
216
|
|
|
return $builder->where('is_billing', true); |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Scope shipping addresses. |
221
|
|
|
* |
222
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
223
|
|
|
* |
224
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
225
|
|
|
*/ |
226
|
|
|
public function scopeIsShipping(Builder $builder): Builder |
227
|
|
|
{ |
228
|
|
|
return $builder->where('is_shipping', true); |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Scope addresses by the given country. |
233
|
|
|
* |
234
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
235
|
|
|
* @param string $countryCode |
236
|
|
|
* |
237
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
238
|
|
|
*/ |
239
|
|
|
public function scopeInCountry(Builder $builder, string $countryCode): Builder |
240
|
|
|
{ |
241
|
|
|
return $builder->where('country_code', $countryCode); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
/** |
245
|
|
|
* Scope addresses by the given language. |
246
|
|
|
* |
247
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
248
|
|
|
* @param string $languageCode |
249
|
|
|
* |
250
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
251
|
|
|
*/ |
252
|
|
|
public function scopeInLanguage(Builder $builder, string $languageCode): Builder |
253
|
|
|
{ |
254
|
|
|
return $builder->where('language_code', $languageCode); |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* {@inheritdoc} |
259
|
|
|
*/ |
260
|
|
|
protected static function boot() |
261
|
|
|
{ |
262
|
|
|
parent::boot(); |
263
|
|
|
|
264
|
|
|
static::saving(function (self $address) { |
265
|
|
|
if (config('rinvex.addresses.geocoding')) { |
266
|
|
|
$segments[] = $address->street; |
|
|
|
|
267
|
|
|
$segments[] = sprintf('%s, %s %s', $address->city, $address->state, $address->postal_code); |
268
|
|
|
$segments[] = country($address->country_code)->getName(); |
269
|
|
|
|
270
|
|
|
$query = str_replace(' ', '+', implode(', ', $segments)); |
271
|
|
|
$geocode = json_decode(file_get_contents("https://maps.google.com/maps/api/geocode/json?address={$query}&sensor=false")); |
|
|
|
|
272
|
|
|
|
273
|
|
|
if (count($geocode->results)) { |
274
|
|
|
$address->lat = $geocode->results[0]->geometry->location->lat; |
275
|
|
|
$address->lng = $geocode->results[0]->geometry->location->lng; |
276
|
|
|
} |
277
|
|
|
} |
278
|
|
|
}); |
279
|
|
|
} |
280
|
|
|
} |
281
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.