1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PWWeb\Localisation\Traits; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* PWWeb\Localisation\Traits HasAddresses |
7
|
|
|
* |
8
|
|
|
* HasAddresses trait for use with other models. |
9
|
|
|
* |
10
|
|
|
* @package PWWeb\Localisation |
11
|
|
|
* @author Frank Pillukeit <[email protected]> |
12
|
|
|
* @copyright 2020 pw-websolutions.com |
13
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
17
|
|
|
use PWWeb\Localisation\LocalisationRegistrar; |
18
|
|
|
use PWWeb\Localisation\Models\Address; |
19
|
|
|
|
20
|
|
|
trait HasAddresses |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Address class. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
private $addressClass; |
28
|
|
|
|
29
|
|
|
public function getAddressClass() |
30
|
|
|
{ |
31
|
|
|
if (! isset($this->addressClass)) { |
32
|
|
|
$this->addressClass = app(LocalisationRegistrar::class)->getAddressClass(); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $this->addressClass; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A model may have multiple addresses. |
40
|
|
|
*/ |
41
|
|
|
public function addresses(): MorphToMany |
42
|
|
|
{ |
43
|
|
|
return $this->morphToMany( |
|
|
|
|
44
|
|
|
config('localisation.models.address'), |
45
|
|
|
'model', |
46
|
|
|
config('localisation.table_names.model_has_address'), |
47
|
|
|
config('localisation.column_names.model_morph_key'), |
48
|
|
|
'address_id' |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Assign the given address(es) to the model. |
54
|
|
|
* |
55
|
|
|
* @param array|string|\PWWeb\Localisation\Contracts\Address ...$addresses |
56
|
|
|
* |
57
|
|
|
* @return $this |
58
|
|
|
*/ |
59
|
|
|
public function assignAddress(...$addresses) |
60
|
|
|
{ |
61
|
|
|
$addresses = collect($addresses) |
62
|
|
|
->flatten() |
63
|
|
|
->map(function ($address) { |
64
|
|
|
if (empty($address)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $this->getStoredAddress($address); |
69
|
|
|
}) |
70
|
|
|
->filter(function ($address) { |
71
|
|
|
return $address instanceof Address; |
72
|
|
|
}) |
73
|
|
|
->map->id |
74
|
|
|
->all(); |
75
|
|
|
|
76
|
|
|
$model = $this->getModel(); |
|
|
|
|
77
|
|
|
|
78
|
|
|
if ($model->exists) { |
79
|
|
|
$this->addresses()->sync($addresses, false); |
80
|
|
|
$model->load('addresses'); |
81
|
|
|
} else { |
82
|
|
|
$class = \get_class($model); |
83
|
|
|
|
84
|
|
|
$class::saved( |
85
|
|
|
function ($object) use ($addresses, $model) { |
86
|
|
|
static $modelLastFiredOn; |
87
|
|
|
if (null !== $modelLastFiredOn && $modelLastFiredOn === $model) { |
88
|
|
|
return; |
89
|
|
|
} |
90
|
|
|
$object->addresses()->sync($addresses, false); |
91
|
|
|
$object->load('addresses'); |
92
|
|
|
$modelLastFiredOn = $object; |
93
|
|
|
}); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
//$this->forgetCachedAddresses(); |
97
|
|
|
|
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function getStoredAddress($address): Address |
102
|
|
|
{ |
103
|
|
|
$addressClass = $this->getAddressClass(); |
104
|
|
|
|
105
|
|
|
if (is_numeric($address)) { |
106
|
|
|
return $addressClass->findById($address, $this->getDefaultGuardName()); |
|
|
|
|
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (is_string($address)) { |
110
|
|
|
return $addressClass->findByName($address, $this->getDefaultGuardName()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $address; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..