|
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\organizations\objects; |
|
10
|
|
|
|
|
11
|
|
|
use Craft; |
|
12
|
|
|
use flipbox\craft\ember\helpers\ObjectHelper; |
|
13
|
|
|
use flipbox\organizations\elements\Organization; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* This trait accepts both an Organization or and Organization Id and ensures that the both |
|
17
|
|
|
* the Organization and the Id are in sync; If one changes (and does not match the other) it |
|
18
|
|
|
* resolves (removes / updates) the other. |
|
19
|
|
|
* |
|
20
|
|
|
* In addition, this trait is primarily useful when a new Organization is set and saved; the Organization |
|
21
|
|
|
* Id can be retrieved without needing to explicitly set the newly created Id. |
|
22
|
|
|
* |
|
23
|
|
|
* @property Organization|null $organization |
|
24
|
|
|
* |
|
25
|
|
|
* @author Flipbox Factory <[email protected]> |
|
26
|
|
|
* @since 1.0.0 |
|
27
|
|
|
*/ |
|
28
|
|
|
trait OrganizationMutatorTrait |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @var Organization|null |
|
32
|
|
|
*/ |
|
33
|
|
|
private $organization; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Internally set the Organization Id. This can be overridden. A record for example |
|
37
|
|
|
* should use `setAttribute`. |
|
38
|
|
|
* |
|
39
|
|
|
* @param int|null $id |
|
40
|
|
|
* @return $this |
|
41
|
|
|
*/ |
|
42
|
|
|
abstract protected function internalSetOrganizationId(int $id = null); |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Internally get the Organization Id. This can be overridden. A record for example |
|
46
|
|
|
* should use `getAttribute`. |
|
47
|
|
|
* |
|
48
|
|
|
* @return int|null |
|
49
|
|
|
*/ |
|
50
|
|
|
abstract protected function internalGetOrganizationId(); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
|
|
public function isOrganizationSet(): bool |
|
56
|
|
|
{ |
|
57
|
|
|
return null !== $this->organization; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set associated organizationId |
|
62
|
|
|
* |
|
63
|
|
|
* @param $id |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setOrganizationId(int $id = null) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->internalSetOrganizationId($id); |
|
69
|
|
|
|
|
70
|
|
|
if (null !== $this->organization && $id !== $this->organization->id) { |
|
71
|
|
|
$this->organization = null; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return $this; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get associated organizationId |
|
79
|
|
|
* |
|
80
|
|
|
* @return int|null |
|
81
|
|
|
*/ |
|
82
|
|
|
public function getOrganizationId() |
|
83
|
|
|
{ |
|
84
|
|
|
if (null === $this->internalGetOrganizationId() && null !== $this->organization) { |
|
85
|
|
|
$this->setOrganizationId($this->organization->id); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return $this->internalGetOrganizationId(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* AssociateUserToOrganization a organization |
|
93
|
|
|
* |
|
94
|
|
|
* @param mixed $organization |
|
95
|
|
|
* @return $this |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setOrganization($organization = null) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->organization = null; |
|
100
|
|
|
$this->internalSetOrganizationId(null); |
|
101
|
|
|
|
|
102
|
|
|
if (null !== ($organization = $this->verifyOrganization($organization))) { |
|
103
|
|
|
$this->organization = $organization; |
|
|
|
|
|
|
104
|
|
|
$this->internalSetOrganizationId($organization->id); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return $this; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @return Organization|null |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getOrganization() |
|
114
|
|
|
{ |
|
115
|
|
|
if ($this->organization === null) { |
|
116
|
|
|
$organization = $this->resolveOrganization(); |
|
117
|
|
|
$this->setOrganization($organization); |
|
118
|
|
|
return $organization; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
$organizationId = $this->internalGetOrganizationId(); |
|
122
|
|
|
if ($organizationId !== null && $organizationId !== $this->organization->id) { |
|
123
|
|
|
$this->organization = null; |
|
124
|
|
|
return $this->getOrganization(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $this->organization; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @return Organization|null |
|
132
|
|
|
*/ |
|
133
|
|
|
protected function resolveOrganization() |
|
134
|
|
|
{ |
|
135
|
|
|
if ($organization = $this->resolveOrganizationFromId()) { |
|
136
|
|
|
return $organization; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return null; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @return Organization|null |
|
144
|
|
|
*/ |
|
145
|
|
|
private function resolveOrganizationFromId() |
|
146
|
|
|
{ |
|
147
|
|
|
if (null === ($organizationId = $this->internalGetOrganizationId())) { |
|
148
|
|
|
return null; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
return Organization::findOne($organizationId); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* Attempt to verify that the passed 'organization' is a valid element. A primary key or query |
|
156
|
|
|
* can be passed to lookup an organization. |
|
157
|
|
|
* |
|
158
|
|
|
* @param mixed $organization |
|
159
|
|
|
* @return Organization|null |
|
160
|
|
|
*/ |
|
161
|
|
|
protected function verifyOrganization($organization = null) |
|
162
|
|
|
{ |
|
163
|
|
|
if (null === $organization) { |
|
164
|
|
|
return null; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if ($organization instanceof Organization) { |
|
168
|
|
|
return $organization; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return Organization::findOne($organization); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
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..