These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @copyright Copyright (c) Flipbox Digital Limited |
||
5 | * @license https://flipboxfactory.com/software/organization/license |
||
6 | * @link https://www.flipboxfactory.com/software/organization/ |
||
7 | */ |
||
8 | |||
9 | namespace flipbox\organizations\elements; |
||
10 | |||
11 | use Craft; |
||
12 | use flipbox\craft\ember\helpers\QueryHelper; |
||
13 | use flipbox\organizations\managers\RelationshipManagerInterface; |
||
14 | use flipbox\organizations\managers\OrganizationTypeRelationshipManager; |
||
15 | use flipbox\organizations\queries\OrganizationTypeQuery; |
||
16 | use flipbox\organizations\records\OrganizationType; |
||
17 | use Tightenco\Collect\Support\Collection; |
||
18 | |||
19 | /** |
||
20 | * @author Flipbox Factory <[email protected]> |
||
21 | * @since 1.0.0 |
||
22 | * |
||
23 | * @mixin Organization |
||
24 | */ |
||
25 | trait TypesAttributeTrait |
||
26 | { |
||
27 | /** |
||
28 | * @var RelationshipManagerInterface |
||
29 | */ |
||
30 | private $typeManager; |
||
31 | |||
32 | /** |
||
33 | * @return RelationshipManagerInterface |
||
34 | */ |
||
35 | public function getTypeManager(): RelationshipManagerInterface |
||
36 | { |
||
37 | if (null === $this->typeManager) { |
||
38 | $this->typeManager = new OrganizationTypeRelationshipManager($this); |
||
39 | } |
||
40 | |||
41 | return $this->typeManager; |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @var OrganizationType|false |
||
46 | */ |
||
47 | private $activeType; |
||
48 | |||
49 | /************************************************************ |
||
50 | * REQUEST |
||
51 | ************************************************************/ |
||
52 | |||
53 | /** |
||
54 | * AssociateUserToOrganization an array of types from request input |
||
55 | * |
||
56 | * @param string $identifier |
||
57 | * @return $this |
||
58 | */ |
||
59 | public function setTypesFromRequest(string $identifier = 'types') |
||
60 | { |
||
61 | if (null !== ($types = Craft::$app->getRequest()->getBodyParam($identifier))) { |
||
62 | $this->getTypeManager()->setMany((array)$types); |
||
63 | } |
||
64 | |||
65 | return $this; |
||
66 | } |
||
67 | |||
68 | /************************************************************ |
||
69 | * ACTIVE TYPE |
||
70 | ************************************************************/ |
||
71 | |||
72 | /** |
||
73 | * @param OrganizationType|null $type |
||
74 | * @return $this |
||
75 | */ |
||
76 | public function setActiveType(OrganizationType $type = null) |
||
77 | { |
||
78 | if ($type) { |
||
79 | $this->getTypeManager()->addOne($type); |
||
80 | } |
||
81 | |||
82 | $this->activeType = (null === $type) ? false : $type; |
||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @return OrganizationType|null |
||
88 | */ |
||
89 | public function getActiveType() |
||
90 | { |
||
91 | if (null === $this->activeType) { |
||
92 | if (!$activeType = $this->getPrimaryType()) { |
||
93 | $activeType = false; |
||
94 | } |
||
95 | |||
96 | $this->activeType = $activeType; |
||
97 | } |
||
98 | |||
99 | return (false === $this->activeType) ? null : $this->activeType; |
||
100 | } |
||
101 | |||
102 | /************************************************************ |
||
103 | * TYPES QUERY |
||
104 | ************************************************************/ |
||
105 | |||
106 | /** |
||
107 | * @param array $criteria |
||
108 | * @return OrganizationTypeQuery |
||
109 | */ |
||
110 | public function userTypeQuery($criteria = []): OrganizationTypeQuery |
||
111 | { |
||
112 | /** @noinspection PhpUndefinedMethodInspection */ |
||
113 | $query = OrganizationType::find() |
||
114 | ->organization($this); |
||
115 | |||
116 | if (!empty($criteria)) { |
||
117 | QueryHelper::configure( |
||
118 | $query, |
||
119 | $criteria |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | return $query; |
||
124 | } |
||
125 | |||
126 | |||
127 | /************************************************************ |
||
128 | * TYPES |
||
129 | ************************************************************/ |
||
130 | |||
131 | /** |
||
132 | * Get an array of types associated to an organization |
||
133 | * |
||
134 | * @param array $criteria |
||
135 | * @return OrganizationType[]|Collection |
||
136 | */ |
||
137 | public function getTypes(array $criteria = []): Collection |
||
138 | { |
||
139 | return Collection::make(QueryHelper::configure( |
||
140 | $this->userTypeQuery($criteria) |
||
141 | ->id( |
||
142 | $this->getTypeManager()->findAll() |
||
143 | ->pluck('typeId') |
||
144 | ) |
||
145 | ->limit(null), |
||
146 | $criteria |
||
147 | )->all()); |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Set an array or query of users to an organization |
||
152 | * |
||
153 | * @param $types |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function setTypes($types) |
||
157 | { |
||
158 | $this->getTypeManager()->setMany($types); |
||
159 | return $this; |
||
160 | } |
||
161 | |||
162 | /************************************************************ |
||
163 | * PRIMARY TYPE |
||
164 | ************************************************************/ |
||
165 | |||
166 | /** |
||
167 | * Identify whether a primary type is set |
||
168 | * |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function hasPrimaryType() |
||
172 | { |
||
173 | return $this->getTypes()->isNotEmpty(); |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Identify whether the type is primary |
||
178 | * |
||
179 | * @param $type |
||
180 | * @return bool |
||
181 | */ |
||
182 | public function isPrimaryType(OrganizationType $type) |
||
183 | { |
||
184 | if ($primaryType = $this->getPrimaryType()) { |
||
185 | return $primaryType->id === $type->id; |
||
186 | } |
||
187 | |||
188 | return false; |
||
189 | } |
||
190 | |||
191 | /** |
||
192 | * @param OrganizationType $type |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setPrimaryType(OrganizationType $type = null) |
||
196 | { |
||
197 | if (null === $type) { |
||
198 | return $this; |
||
199 | } |
||
200 | |||
201 | $this->getTypeManager()->setMany( |
||
202 | array_merge( |
||
0 ignored issues
–
show
|
|||
203 | [ |
||
204 | $type |
||
205 | ], |
||
206 | $this->getTypes() |
||
207 | ) |
||
208 | ); |
||
209 | |||
210 | return $this; |
||
211 | } |
||
212 | |||
213 | /** |
||
214 | * Get the primary type |
||
215 | * |
||
216 | * @return OrganizationType|null |
||
217 | */ |
||
218 | public function getPrimaryType() |
||
219 | { |
||
220 | return $this->getTypes()->first(); |
||
221 | } |
||
222 | } |
||
223 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: