1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright Copyright (c) Flipbox Digital Limited |
5
|
|
|
* @license https://github.com/flipboxfactory/craft-ember/blob/master/LICENSE |
6
|
|
|
* @link https://github.com/flipboxfactory/craft-ember/ |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace flipbox\craft\ember\objects; |
10
|
|
|
|
11
|
|
|
use Craft; |
12
|
|
|
use craft\elements\User; |
13
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property int|null $userId |
17
|
|
|
* |
18
|
|
|
* @author Flipbox Factory <[email protected]> |
19
|
|
|
* @since 2.0.0 |
20
|
|
|
*/ |
21
|
|
|
trait UserMutatorTrait |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var User|null |
25
|
|
|
*/ |
26
|
|
|
private $user; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Set associated userId |
30
|
|
|
* |
31
|
|
|
* @param $id |
32
|
|
|
* @return $this |
33
|
|
|
*/ |
34
|
|
|
public function setUserId(int $id) |
35
|
|
|
{ |
36
|
|
|
$this->userId = $id; |
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Get associated userId |
42
|
|
|
* |
43
|
|
|
* @return int|null |
44
|
|
|
*/ |
45
|
|
|
public function getUserId() |
46
|
|
|
{ |
47
|
|
|
if (null === $this->userId && null !== $this->user) { |
48
|
|
|
$this->userId = $this->user->id; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->userId; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Associate a user |
56
|
|
|
* |
57
|
|
|
* @param mixed $user |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
|
|
public function setUser($user = null) |
61
|
|
|
{ |
62
|
|
|
$this->user = null; |
63
|
|
|
|
64
|
|
|
if (!$user = $this->internalResolveUser($user)) { |
65
|
|
|
$this->user = $this->userId = null; |
66
|
|
|
} else { |
67
|
|
|
$this->userId = $user->id; |
68
|
|
|
$this->user = $user; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return User|null |
76
|
|
|
*/ |
77
|
|
|
public function getUser() |
78
|
|
|
{ |
79
|
|
|
if ($this->user === null) { |
80
|
|
|
$user = $this->resolveUser(); |
81
|
|
|
$this->setUser($user); |
82
|
|
|
return $user; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$userId = $this->userId; |
86
|
|
|
if ($userId !== null && |
87
|
|
|
$userId !== $this->user->id |
88
|
|
|
) { |
89
|
|
|
$this->user = null; |
90
|
|
|
return $this->getUser(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->user; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return User|null |
98
|
|
|
*/ |
99
|
|
|
protected function resolveUser() |
100
|
|
|
{ |
101
|
|
|
if ($model = $this->resolveUserFromId()) { |
102
|
|
|
return $model; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @return User|null |
110
|
|
|
*/ |
111
|
|
|
private function resolveUserFromId() |
112
|
|
|
{ |
113
|
|
|
if (null === $this->userId) { |
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
return Craft::$app->getUsers()->getUserById($this->userId); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param $user |
122
|
|
|
* @return User|null |
123
|
|
|
*/ |
124
|
|
|
protected function internalResolveUser($user = null) |
125
|
|
|
{ |
126
|
|
|
if ($user === null) { |
127
|
|
|
return null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($user instanceof User) { |
131
|
|
|
return $user; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (is_numeric($user)) { |
135
|
|
|
return Craft::$app->getUsers()->getUserById($user); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (is_string($user)) { |
139
|
|
|
return Craft::$app->getUsers()->getUserByUsernameOrEmail($user); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
try { |
143
|
|
|
$object = Craft::createObject(User::class, [$user]); |
144
|
|
|
} catch (\Exception $e) { |
145
|
|
|
$object = new User(); |
146
|
|
|
ObjectHelper::populate( |
147
|
|
|
$object, |
148
|
|
|
$user |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @var User $object */ |
153
|
|
|
return $object; |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|