1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* PWWEB\Localisation\Models\Address Model. |
5
|
|
|
* |
6
|
|
|
* Standard Address Model. |
7
|
|
|
* |
8
|
|
|
* @author Frank Pillukeit <[email protected]> |
9
|
|
|
* @copyright 2020 pw-websolutions.com |
10
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace PWWeb\Localisation\Models; |
14
|
|
|
|
15
|
|
|
use Illuminate\Database\Eloquent\Collection; |
16
|
|
|
use Illuminate\Database\Eloquent\Model; |
17
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo; |
18
|
|
|
use PWWEB\Localisation\Contracts\Address as AddressContract; |
19
|
|
|
use PWWEB\Localisation\Exceptions\AddressDoesNotExist; |
20
|
|
|
use PWWEB\Localisation\LocalisationRegistrar; |
21
|
|
|
use PWWEB\Localisation\Models\Address\Type; |
22
|
|
|
|
23
|
|
|
class Address extends Model implements AddressContract |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The attributes that are mass assignable. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $fillable = [ |
31
|
|
|
'street', 'street2', 'city', 'state', 'postcode', 'country_id', 'type_id', 'lat', 'lng', |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Constructor. |
36
|
|
|
* |
37
|
|
|
* @param array $attributes additional attributes for model initialisation |
38
|
|
|
* |
39
|
|
|
* @return void |
40
|
|
|
*/ |
41
|
|
|
public function __construct(array $attributes = []) |
42
|
|
|
{ |
43
|
|
|
parent::__construct($attributes); |
44
|
|
|
|
45
|
|
|
$this->setTable(config('pwweb.localisation.table_names.addresses')); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* An address can be of only one type (e.g. private, work, etc). |
50
|
|
|
* |
51
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
52
|
|
|
*/ |
53
|
|
|
public function type(): BelongsTo |
54
|
|
|
{ |
55
|
|
|
return $this->belongsTo(Address\Type::class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* An address belongs to a country. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
62
|
|
|
*/ |
63
|
|
|
public function country(): BelongsTo |
64
|
|
|
{ |
65
|
|
|
return $this->belongsTo(Country::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Find an address by its id. |
70
|
|
|
* |
71
|
|
|
* @param int $id ID to be used to retrieve the address |
72
|
|
|
* |
73
|
|
|
* @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist |
74
|
|
|
* |
75
|
|
|
* @return \PWWEB\Localisation\Contracts\Address |
76
|
|
|
*/ |
77
|
|
|
public static function findById(int $id): AddressContract |
78
|
|
|
{ |
79
|
|
|
$address = static::getAddresses(['id' => $id])->first(); |
80
|
|
|
|
81
|
|
|
if (null === $address) { |
82
|
|
|
throw AddressDoesNotExist::withId($id); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return $address; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Find an address by its type. |
90
|
|
|
* |
91
|
|
|
* @param string $type Address type to be used to retrieve the address |
92
|
|
|
* |
93
|
|
|
* @throws \PWWEB\Localisation\Exceptions\AddressDoesNotExist |
94
|
|
|
* |
95
|
|
|
* @return \PWWEB\Localisation\Contracts\Address |
96
|
|
|
*/ |
97
|
|
|
public static function findByType(string $type): AddressContract |
98
|
|
|
{ |
99
|
|
|
$address = static::getAddresses(['type' => $type])->first(); |
100
|
|
|
|
101
|
|
|
if (null === $address) { |
102
|
|
|
throw AddressDoesNotExist::withType($type); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $address; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the current cached addresses. |
110
|
|
|
* |
111
|
|
|
* @param array $params additional parameters for the database query |
112
|
|
|
* |
113
|
|
|
* @return Collection collection of addresses |
114
|
|
|
*/ |
115
|
|
|
protected static function getAddresses(array $params = []): Collection |
116
|
|
|
{ |
117
|
|
|
return app(LocalisationRegistrar::class) |
118
|
|
|
->setAddressClass(static::class) |
119
|
|
|
->getAddresses($params); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|