1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PWWEB\Localisation\Models; |
4
|
|
|
|
5
|
|
|
use Eloquent as Model; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* PWWEB\Localisation\Models\Address Model. |
10
|
|
|
* |
11
|
|
|
* Standard Address Model. |
12
|
|
|
* |
13
|
|
|
* @package pwweb/localisation |
14
|
|
|
* @author Frank Pillukeit <[email protected]> |
15
|
|
|
* @author Richard Browne <[email protected] |
16
|
|
|
* @copyright 2020 pw-websolutions.com |
17
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
18
|
|
|
* @property \PWWEB\Localisation\Models\SystemLocalisationCountry country |
19
|
|
|
* @property \PWWEB\Localisation\Models\SystemAddressType type |
20
|
|
|
* @property \PWWEB\Localisation\Models\SystemModelHasAddress systemModelHasAddress |
21
|
|
|
* @property integer country_id |
22
|
|
|
* @property integer type_id |
23
|
|
|
* @property string street |
24
|
|
|
* @property string street2 |
25
|
|
|
* @property string city |
26
|
|
|
* @property string state |
27
|
|
|
* @property string postcode |
28
|
|
|
* @property number lat |
29
|
|
|
* @property number lng |
30
|
|
|
* @property boolean primary |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
class Address extends Model |
34
|
|
|
{ |
35
|
|
|
use SoftDeletes; |
36
|
|
|
|
37
|
|
|
public $table = 'system_addresses'; |
38
|
|
|
|
39
|
|
|
const CREATED_AT = 'created_at'; |
40
|
|
|
const UPDATED_AT = 'updated_at'; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
protected $dates = ['deleted_at']; |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
public $fillable = [ |
48
|
|
|
'country_id', |
49
|
|
|
'type_id', |
50
|
|
|
'street', |
51
|
|
|
'street2', |
52
|
|
|
'city', |
53
|
|
|
'state', |
54
|
|
|
'postcode', |
55
|
|
|
'lat', |
56
|
|
|
'lng', |
57
|
|
|
'primary' |
58
|
|
|
]; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* The attributes that should be casted to native types. |
62
|
|
|
* |
63
|
|
|
* @var array |
64
|
|
|
*/ |
65
|
|
|
protected $casts = [ |
66
|
|
|
'id' => 'integer', |
67
|
|
|
'country_id' => 'integer', |
68
|
|
|
'type_id' => 'integer', |
69
|
|
|
'street' => 'string', |
70
|
|
|
'street2' => 'string', |
71
|
|
|
'city' => 'string', |
72
|
|
|
'state' => 'string', |
73
|
|
|
'postcode' => 'string', |
74
|
|
|
'lat' => 'float', |
75
|
|
|
'lng' => 'float', |
76
|
|
|
'primary' => 'boolean' |
77
|
|
|
]; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Validation rules. |
81
|
|
|
* |
82
|
|
|
* @var array |
83
|
|
|
*/ |
84
|
|
|
public static $rules = [ |
85
|
|
|
'country_id' => 'required', |
86
|
|
|
'type_id' => 'required', |
87
|
|
|
'primary' => 'required' |
88
|
|
|
]; |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Constructor. |
92
|
|
|
* |
93
|
|
|
* @param array $attributes additional attributes for model initialisation |
94
|
|
|
* |
95
|
|
|
* @return void |
96
|
|
|
*/ |
97
|
|
|
public function __construct(array $attributes = []) |
98
|
|
|
{ |
99
|
|
|
parent::__construct($attributes); |
100
|
|
|
|
101
|
|
|
$this->setTable(config('pwweb.localisation.table_names.addresses')); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
106
|
|
|
**/ |
107
|
|
|
public function country() |
108
|
|
|
{ |
109
|
|
|
return $this->belongsTo(\App\Models\Pwweb\Localisation\Models\SystemLocalisationCountry::class, 'country_id'); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
114
|
|
|
**/ |
115
|
|
|
public function type() |
116
|
|
|
{ |
117
|
|
|
return $this->belongsTo(\App\Models\Pwweb\Localisation\Models\SystemAddressType::class, 'type_id'); |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne |
122
|
|
|
**/ |
123
|
|
|
public function systemModelHasAddress() |
124
|
|
|
{ |
125
|
|
|
return $this->hasOne(\App\Models\Pwweb\Localisation\Models\SystemModelHasAddress::class); |
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Find an address by its id. |
130
|
|
|
* |
131
|
|
|
* @param int $id ID to be used to retrieve the address |
132
|
|
|
* |
133
|
|
|
* @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist |
134
|
|
|
* |
135
|
|
|
* @return \PWWEB\Localisation\Contracts\Address |
136
|
|
|
*/ |
137
|
|
|
public static function findById(int $id): AddressContract |
|
|
|
|
138
|
|
|
{ |
139
|
|
|
$address = static::getAddresses(['id' => $id])->first(); |
140
|
|
|
|
141
|
|
|
if (null === $address) { |
142
|
|
|
throw AddressDoesNotExist::withId($id); |
|
|
|
|
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $address; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Find an address by its type. |
150
|
|
|
* |
151
|
|
|
* @param string $type Address type to be used to retrieve the address |
152
|
|
|
* |
153
|
|
|
* @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist |
154
|
|
|
* |
155
|
|
|
* @return \PWWEB\Localisation\Contracts\Address |
156
|
|
|
*/ |
157
|
|
|
public static function findByType(string $type): AddressContract |
158
|
|
|
{ |
159
|
|
|
$address = static::getAddresses(['type' => $type])->first(); |
160
|
|
|
|
161
|
|
|
if (null === $address) { |
162
|
|
|
throw AddressDoesNotExist::withType($type); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return $address; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Get the current cached addresses. |
170
|
|
|
* |
171
|
|
|
* @param array $params additional parameters for the database query |
172
|
|
|
* |
173
|
|
|
* @return Collection collection of addresses |
174
|
|
|
*/ |
175
|
|
|
protected static function getAddresses(array $params = []): Collection |
|
|
|
|
176
|
|
|
{ |
177
|
|
|
return app(LocalisationRegistrar::class) |
|
|
|
|
178
|
|
|
->setAddressClass(static::class) |
|
|
|
|
179
|
|
|
->getAddresses($params); |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths