|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chuckcms\Addresses\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Chuckcms\Addresses\Contracts\Address as AddressContract; |
|
6
|
|
|
use Chuckcms\Addresses\Exceptions\AddressDoesNotExist; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
9
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
10
|
|
|
|
|
11
|
|
|
class Address extends Model implements AddressContract |
|
12
|
|
|
{ |
|
13
|
|
|
use SoftDeletes; |
|
14
|
|
|
|
|
15
|
|
|
protected $guarded = ['id']; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* The attributes that are fillable on this model. |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $fillable = ['addressable_id', 'addressable_type']; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* The default rules that the model will validate against. |
|
26
|
|
|
* |
|
27
|
|
|
* @var array |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $rules = []; |
|
30
|
|
|
|
|
31
|
|
|
public function __construct(array $attributes = []) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->setTable(config('addresses.table_names.addresses')); |
|
|
|
|
|
|
34
|
|
|
$this->mergeFillables(); |
|
35
|
|
|
|
|
36
|
|
|
parent::__construct($attributes); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Merge fillable fields. |
|
41
|
|
|
* |
|
42
|
|
|
* @return void. |
|
|
|
|
|
|
43
|
|
|
*/ |
|
44
|
|
|
private function mergeFillables() |
|
45
|
|
|
{ |
|
46
|
|
|
$fillable = $this->fillable; |
|
47
|
|
|
$columns = array_keys(config('addresses.fields.addresses')); |
|
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
$this->fillable(array_merge($fillable, $columns)); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Get the related model. |
|
54
|
|
|
* |
|
55
|
|
|
* @return MorphTo |
|
56
|
|
|
*/ |
|
57
|
|
|
public function addressable(): MorphTo |
|
|
|
|
|
|
58
|
|
|
{ |
|
59
|
|
|
return $this->morphTo(); |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Get the validation rules. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getValidationRules(): array |
|
68
|
|
|
{ |
|
69
|
|
|
$rules = config('addresses.fields.addresses'); |
|
|
|
|
|
|
70
|
|
|
|
|
71
|
|
|
return $rules; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Scope public addresses. |
|
76
|
|
|
* |
|
77
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
78
|
|
|
* |
|
79
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
80
|
|
|
*/ |
|
81
|
|
|
public function scopeIsPublic(Builder $builder): Builder |
|
82
|
|
|
{ |
|
83
|
|
|
return $builder->where('is_public', true); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Scope primary addresses. |
|
88
|
|
|
* |
|
89
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
90
|
|
|
* |
|
91
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
92
|
|
|
*/ |
|
93
|
|
|
public function scopeIsPrimary(Builder $builder): Builder |
|
94
|
|
|
{ |
|
95
|
|
|
return $builder->where('is_primary', true); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Scope billing addresses. |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
102
|
|
|
* |
|
103
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
104
|
|
|
*/ |
|
105
|
|
|
public function scopeIsBilling(Builder $builder): Builder |
|
106
|
|
|
{ |
|
107
|
|
|
return $builder->where('is_billing', true); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Scope shipping addresses. |
|
112
|
|
|
* |
|
113
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
114
|
|
|
* |
|
115
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
116
|
|
|
*/ |
|
117
|
|
|
public function scopeIsShipping(Builder $builder): Builder |
|
118
|
|
|
{ |
|
119
|
|
|
return $builder->where('is_shipping', true); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Scope addresses by the given country. |
|
124
|
|
|
* |
|
125
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $builder |
|
126
|
|
|
* @param string $countryCode |
|
127
|
|
|
* |
|
128
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
|
129
|
|
|
*/ |
|
130
|
|
|
public function scopeInCountry(Builder $builder, string $country): Builder |
|
131
|
|
|
{ |
|
132
|
|
|
return $builder->where('country', $country); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
public static function findById(int $id): AddressContract |
|
136
|
|
|
{ |
|
137
|
|
|
$address = static::where('id', $id)->first(); |
|
138
|
|
|
|
|
139
|
|
|
if (!$address) { |
|
140
|
|
|
throw AddressDoesNotExist::withId($id); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return $address; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|