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
|
|
|
* @property int|null $organizationId |
17
|
|
|
* @property Organization|null $organization |
18
|
|
|
* |
19
|
|
|
* @author Flipbox Factory <[email protected]> |
20
|
|
|
* @since 1.0.0 |
21
|
|
|
*/ |
22
|
|
|
trait OrganizationMutatorTrait |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Organization|null |
26
|
|
|
*/ |
27
|
|
|
private $organization; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function isOrganizationSet(): bool |
33
|
|
|
{ |
34
|
|
|
return null !== $this->organization; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set associated organizationId |
39
|
|
|
* |
40
|
|
|
* @param $id |
41
|
|
|
* @return $this |
42
|
|
|
*/ |
43
|
|
|
public function setOrganizationId(int $id) |
44
|
|
|
{ |
45
|
|
|
$this->organizationId = $id; |
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get associated organizationId |
51
|
|
|
* |
52
|
|
|
* @return int|null |
53
|
|
|
*/ |
54
|
|
|
public function getOrganizationId() |
55
|
|
|
{ |
56
|
|
|
if (null === $this->organizationId && null !== $this->organization) { |
57
|
|
|
$this->organizationId = $this->organization->id; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $this->organizationId; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* AssociateUserToOrganization a organization |
65
|
|
|
* |
66
|
|
|
* @param mixed $organization |
67
|
|
|
* @return $this |
68
|
|
|
*/ |
69
|
|
|
public function setOrganization($organization = null) |
70
|
|
|
{ |
71
|
|
|
$this->organization = null; |
72
|
|
|
|
73
|
|
|
if (null === ($organization = $this->internalResolveOrganization($organization))) { |
74
|
|
|
$this->organization = $this->organizationId = null; |
75
|
|
|
} else { |
76
|
|
|
$this->organizationId = $organization->id; |
77
|
|
|
$this->organization = $organization; |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return Organization|null |
85
|
|
|
*/ |
86
|
|
|
public function getOrganization() |
87
|
|
|
{ |
88
|
|
|
if ($this->organization === null) { |
89
|
|
|
$organization = $this->resolveOrganization(); |
90
|
|
|
$this->setOrganization($organization); |
91
|
|
|
return $organization; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$organizationId = $this->organizationId; |
95
|
|
|
if ($organizationId !== null && |
96
|
|
|
$organizationId !== $this->organization->id |
97
|
|
|
) { |
98
|
|
|
$this->organization = null; |
99
|
|
|
return $this->getOrganization(); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->organization; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return Organization|null |
107
|
|
|
*/ |
108
|
|
|
protected function resolveOrganization() |
109
|
|
|
{ |
110
|
|
|
if ($model = $this->resolveOrganizationFromId()) { |
111
|
|
|
return $model; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
return null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @return Organization|null |
119
|
|
|
*/ |
120
|
|
|
private function resolveOrganizationFromId() |
121
|
|
|
{ |
122
|
|
|
if (null === $this->organizationId) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return Organization::findOne($this->organizationId); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @param $organization |
131
|
|
|
* @return Organization|null |
132
|
|
|
*/ |
133
|
|
|
protected function internalResolveOrganization($organization = null) |
134
|
|
|
{ |
135
|
|
|
if (null === $organization) { |
136
|
|
|
return null; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
if ($organization instanceof Organization) { |
140
|
|
|
return $organization; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
if (is_numeric($organization) || is_string($organization)) { |
144
|
|
|
return Organization::findOne($organization); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
try { |
148
|
|
|
$object = Craft::createObject(Organization::class, [$organization]); |
149
|
|
|
} catch (\Exception $e) { |
150
|
|
|
$object = new Organization(); |
151
|
|
|
ObjectHelper::populate( |
152
|
|
|
$object, |
153
|
|
|
$organization |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** @var Organization $object */ |
158
|
|
|
return $object; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
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..