1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipbox/spark/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipbox/spark |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\spark\models\traits; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User as UserElement; |
13
|
|
|
use flipbox\spark\helpers\ModelHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Flipbox Factory <[email protected]> |
17
|
|
|
* @since 1.0.0 |
18
|
|
|
*/ |
19
|
|
|
trait UserTrait |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var int|null |
24
|
|
|
*/ |
25
|
|
|
private $_userId; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var UserElement|null |
29
|
|
|
*/ |
30
|
|
|
private $_user; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Set associated userId |
34
|
|
|
* |
35
|
|
|
* @param $id |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function setUserId(int $id) |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
// Has the id changed? |
42
|
|
|
if ($id !== $this->_userId) { |
43
|
|
|
|
44
|
|
|
// Invalidate existing user |
45
|
|
|
if ($this->_user !== null && $this->_user->getId() !== $id) { |
46
|
|
|
$this->_user = null; |
47
|
|
|
}; |
48
|
|
|
|
49
|
|
|
$this->_userId = $id; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Get associated userId |
58
|
|
|
* |
59
|
|
|
* @return int|null |
60
|
|
|
*/ |
61
|
|
|
public function getUserId() |
62
|
|
|
{ |
63
|
|
|
return $this->_userId; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Associate a user |
69
|
|
|
* |
70
|
|
|
* @param $user |
71
|
|
|
* @return $this |
72
|
|
|
*/ |
73
|
|
|
public function setUser($user) |
74
|
|
|
{ |
75
|
|
|
|
76
|
|
|
// Clear cache |
77
|
|
|
$this->_user = null; |
78
|
|
|
|
79
|
|
|
// Find element |
80
|
|
|
if (!$user = $this->findUserElement($user)) { |
81
|
|
|
|
82
|
|
|
// Clear property / cache |
83
|
|
|
$this->_userId = $this->_user = null; |
84
|
|
|
|
85
|
|
|
} else { |
86
|
|
|
|
87
|
|
|
// Set property |
88
|
|
|
$this->_userId = $user->getId(); |
89
|
|
|
|
90
|
|
|
// Set cache |
91
|
|
|
$this->_user = $user; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $this; |
96
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return UserElement|null |
101
|
|
|
*/ |
102
|
|
|
public function getUser() |
103
|
|
|
{ |
104
|
|
|
|
105
|
|
|
// Check cache |
106
|
|
|
if (is_null($this->_user)) { |
107
|
|
|
|
108
|
|
|
// Check property |
109
|
|
|
if (!empty($this->_userId)) { |
110
|
|
|
|
111
|
|
|
// Find element |
112
|
|
|
if ($userElement = Craft::$app->getUsers()->getUserById($this->_userId)) { |
113
|
|
|
|
114
|
|
|
// Set |
115
|
|
|
$this->setUser($userElement); |
116
|
|
|
|
117
|
|
|
} else { |
118
|
|
|
|
119
|
|
|
// Clear property (it's invalid) |
120
|
|
|
$this->_userId = null; |
121
|
|
|
|
122
|
|
|
// Prevent subsequent look-ups |
123
|
|
|
$this->_user = false; |
|
|
|
|
124
|
|
|
|
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
} else { |
128
|
|
|
|
129
|
|
|
// Prevent subsequent look-ups |
130
|
|
|
$this->_user = false; |
131
|
|
|
|
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return !$this->_user ? null : $this->_user; |
137
|
|
|
|
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param $user |
142
|
|
|
* @return UserElement|null |
143
|
|
|
*/ |
144
|
|
|
private function findUserElement($user) |
145
|
|
|
{ |
146
|
|
|
|
147
|
|
|
// Element |
148
|
|
|
if ($user instanceof UserElement) { |
149
|
|
|
|
150
|
|
|
return $user; |
151
|
|
|
|
152
|
|
|
// Id |
153
|
|
|
} elseif (is_numeric($user)) { |
154
|
|
|
|
155
|
|
|
return Craft::$app->getUsers()->getUserById($user); |
156
|
|
|
|
157
|
|
|
// Username / Email |
158
|
|
|
} elseif (!is_null($user)) { |
159
|
|
|
|
160
|
|
|
return Craft::$app->getUsers()->getUserByUsernameOrEmail($user); |
161
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return null; |
165
|
|
|
|
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* @return array |
170
|
|
|
*/ |
171
|
|
View Code Duplication |
protected function userRules(): array |
|
|
|
|
172
|
|
|
{ |
173
|
|
|
|
174
|
|
|
return [ |
175
|
|
|
[ |
176
|
|
|
[ |
177
|
|
|
'userId' |
178
|
|
|
], |
179
|
|
|
'number', |
180
|
|
|
'integerOnly' => true |
181
|
|
|
], |
182
|
|
|
[ |
183
|
|
|
[ |
184
|
|
|
'userId', |
185
|
|
|
'user' |
186
|
|
|
], |
187
|
|
|
'safe', |
188
|
|
|
'on' => [ |
189
|
|
|
ModelHelper::SCENARIO_DEFAULT |
190
|
|
|
] |
191
|
|
|
] |
192
|
|
|
]; |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return array |
198
|
|
|
*/ |
199
|
|
|
protected function userFields(): array |
200
|
|
|
{ |
201
|
|
|
|
202
|
|
|
return [ |
203
|
|
|
'userId' |
204
|
|
|
]; |
205
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @return array |
210
|
|
|
*/ |
211
|
|
|
protected function userAttributes(): array |
212
|
|
|
{ |
213
|
|
|
|
214
|
|
|
return [ |
215
|
|
|
'userId' |
216
|
|
|
]; |
217
|
|
|
|
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* @return array |
222
|
|
|
*/ |
223
|
|
|
protected function userAttributeLabels(): array |
224
|
|
|
{ |
225
|
|
|
|
226
|
|
|
return [ |
227
|
|
|
'userId' => Craft::t('app', 'User Id') |
228
|
|
|
]; |
229
|
|
|
|
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
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..