1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://flipboxfactory.com/software/patron/license |
6
|
|
|
* @link https://www.flipboxfactory.com/software/patron/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\patron\records; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
13
|
|
|
use flipbox\craft\ember\records\ActiveRecordTrait; |
14
|
|
|
use yii\db\ActiveQueryInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @property int|null $providerId |
18
|
|
|
* @property Provider|null $provider |
19
|
|
|
* @property ActiveQueryInterface $providerRecord |
20
|
|
|
* |
21
|
|
|
* @author Flipbox Factory <[email protected]> |
22
|
|
|
* @since 1.0.0 |
23
|
|
|
*/ |
24
|
|
|
trait ProviderMutatorTrait |
25
|
|
|
{ |
26
|
|
|
use ActiveRecordTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Provider|null |
30
|
|
|
*/ |
31
|
|
|
private $provider; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Set associated providerId |
35
|
|
|
* |
36
|
|
|
* @param $id |
37
|
|
|
* @return $this |
38
|
|
|
*/ |
39
|
|
|
public function setProviderId(int $id) |
40
|
|
|
{ |
41
|
|
|
$this->providerId = $id; |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get associated providerId |
47
|
|
|
* |
48
|
|
|
* @return int|null |
49
|
|
|
*/ |
50
|
|
|
public function getProviderId() |
51
|
|
|
{ |
52
|
|
|
$id = $this->getAttribute('providerId'); |
53
|
|
|
if (null === $id && null !== $this->provider) { |
54
|
|
|
$id = $this->provider->id; |
55
|
|
|
$this->setAttribute('providerId', $id); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $id; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Associate a provider |
63
|
|
|
* |
64
|
|
|
* @param mixed $provider |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setProvider($provider = null) |
68
|
|
|
{ |
69
|
|
|
$this->provider = null; |
70
|
|
|
|
71
|
|
|
if (null === ($provider = $this->internalResolveProvider($provider))) { |
72
|
|
|
$this->provider = $this->providerId = null; |
73
|
|
|
} else { |
74
|
|
|
$this->providerId = $provider->id; |
75
|
|
|
$this->provider = $provider; |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @return Provider|null |
83
|
|
|
*/ |
84
|
|
|
public function getProvider() |
85
|
|
|
{ |
86
|
|
|
if ($this->provider === null) { |
87
|
|
|
$provider = $this->resolveProvider(); |
88
|
|
|
$this->setProvider($provider); |
89
|
|
|
return $provider; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
$providerId = $this->providerId; |
93
|
|
|
if ($providerId !== null && |
94
|
|
|
$providerId !== $this->provider->id |
95
|
|
|
) { |
96
|
|
|
$this->provider = null; |
97
|
|
|
return $this->getProvider(); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $this->provider; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return Provider|null |
105
|
|
|
*/ |
106
|
|
|
protected function resolveProvider() |
107
|
|
|
{ |
108
|
|
|
if (null !== ($model = $this->resolveProviderFromRelation())) { |
109
|
|
|
return $model; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (null !== ($model = $this->resolveProviderFromId())) { |
113
|
|
|
return $model; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return Provider|null |
121
|
|
|
*/ |
122
|
|
|
private function resolveProviderFromRelation() |
123
|
|
|
{ |
124
|
|
|
if (false === $this->isRelationPopulated('providerRecord')) { |
125
|
|
|
return null; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** @var Provider $record */ |
129
|
|
|
if (null === ($record = $this->getRelation('providerRecord'))) { |
130
|
|
|
return null; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return $record; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* @return Provider|null |
138
|
|
|
*/ |
139
|
|
|
private function resolveProviderFromId() |
140
|
|
|
{ |
141
|
|
|
if (null === $this->providerId) { |
142
|
|
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return Provider::findOne(['id' => $this->providerId]); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param $provider |
150
|
|
|
* @return Provider|null |
151
|
|
|
*/ |
152
|
|
|
protected function internalResolveProvider($provider = null) |
153
|
|
|
{ |
154
|
|
|
if ($provider instanceof Provider) { |
155
|
|
|
return $provider; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
if (is_numeric($provider) || is_string($provider)) { |
159
|
|
|
return Provider::findOne([ |
160
|
|
|
'enabled' => null, |
161
|
|
|
is_numeric($provider) ? 'id' : 'handle' => $provider |
162
|
|
|
]); |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
try { |
166
|
|
|
$object = Craft::createObject(Provider::class, [$provider]); |
167
|
|
|
} catch (\Exception $e) { |
168
|
|
|
$object = new Provider(); |
169
|
|
|
ObjectHelper::populate( |
170
|
|
|
$object, |
171
|
|
|
$provider |
172
|
|
|
); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** @var Provider $object */ |
176
|
|
|
return $object; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Returns the associated provider record. |
181
|
|
|
* |
182
|
|
|
* @return ActiveQueryInterface |
183
|
|
|
*/ |
184
|
|
|
protected function getProviderRecord(): ActiveQueryInterface |
185
|
|
|
{ |
186
|
|
|
return $this->hasOne( |
187
|
|
|
Provider::class, |
188
|
|
|
['id' => 'providerId'] |
189
|
|
|
); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
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..