This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Sofa\Eloquence; |
||
4 | |||
5 | use LogicException; |
||
6 | use Illuminate\Support\Arr; |
||
7 | use Sofa\Eloquence\Mappable\Hooks; |
||
8 | use Sofa\Hookable\Contracts\ArgumentBag; |
||
9 | use Illuminate\Contracts\Support\Arrayable; |
||
10 | use Illuminate\Database\Eloquent\Relations\HasOne; |
||
11 | use Illuminate\Database\Eloquent\Relations\MorphTo; |
||
12 | use Illuminate\Database\Eloquent\Relations\MorphOne; |
||
13 | use Illuminate\Database\Eloquent\Relations\Relation; |
||
14 | use Illuminate\Database\Eloquent\Relations\BelongsTo; |
||
15 | use Illuminate\Database\Eloquent\Model as EloquentModel; |
||
16 | |||
17 | /** |
||
18 | * @property array $maps |
||
19 | */ |
||
20 | trait Mappable |
||
21 | { |
||
22 | /** |
||
23 | * Flat array representation of mapped attributes. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $mappedAttributes; |
||
28 | |||
29 | /** |
||
30 | * Related mapped objects to save along with the mappable instance. |
||
31 | * |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $targetsToSave = []; |
||
35 | |||
36 | /** |
||
37 | * Register hooks for the trait. |
||
38 | * |
||
39 | * @codeCoverageIgnore |
||
40 | * |
||
41 | * @return void |
||
42 | */ |
||
43 | public static function bootMappable() |
||
44 | { |
||
45 | $hooks = new Hooks; |
||
46 | |||
47 | foreach ([ |
||
48 | 'getAttribute', |
||
49 | 'setAttribute', |
||
50 | 'save', |
||
51 | '__isset', |
||
52 | '__unset', |
||
53 | 'queryHook', |
||
54 | ] as $method) { |
||
55 | static::hook($method, $hooks->{$method}()); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Custom query handler for querying mapped attributes. |
||
61 | * |
||
62 | * @param \Sofa\Eloquence\Builder $query |
||
63 | * @param string $method |
||
64 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
65 | * @return mixed |
||
66 | */ |
||
67 | protected function mappedQuery(Builder $query, $method, ArgumentBag $args) |
||
68 | { |
||
69 | $mapping = $this->getMappingForAttribute($args->get('column')); |
||
70 | |||
71 | if ($this->relationMapping($mapping)) { |
||
72 | return $this->mappedRelationQuery($query, $method, $args, $mapping); |
||
73 | } |
||
74 | |||
75 | $args->set('column', $mapping); |
||
76 | |||
77 | return $query->callParent($method, $args->all()); |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * Adjust mapped columns for select statement. |
||
82 | * |
||
83 | * @param \Sofa\Eloquence\Builder $query |
||
84 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
85 | * @return void |
||
86 | */ |
||
87 | protected function mappedSelect(Builder $query, ArgumentBag $args) |
||
88 | { |
||
89 | $columns = $args->get('columns'); |
||
90 | |||
91 | foreach ($columns as $key => $column) { |
||
92 | list($column, $as) = $this->extractColumnAlias($column); |
||
93 | |||
94 | // Each mapped column will be selected appropriately. If it's alias |
||
95 | // then prefix it with current table and use original field name |
||
96 | // otherwise join required mapped tables and select the field. |
||
97 | if ($this->hasMapping($column)) { |
||
98 | $mapping = $this->getMappingForAttribute($column); |
||
99 | |||
100 | if ($this->relationMapping($mapping)) { |
||
101 | list($target, $mapped) = $this->parseMappedColumn($mapping); |
||
102 | |||
103 | $table = $this->joinMapped($query, $target); |
||
104 | } else { |
||
105 | list($table, $mapped) = [$this->getTable(), $mapping]; |
||
106 | } |
||
107 | |||
108 | $columns[$key] = "{$table}.{$mapped}"; |
||
109 | |||
110 | if ($as !== $column) { |
||
111 | $columns[$key] .= " as {$as}"; |
||
112 | } |
||
113 | |||
114 | // For non mapped columns present on this table we will simply |
||
115 | // add the prefix, in order to avoid any column collisions, |
||
116 | // that are likely to happen when we are joining tables. |
||
117 | } elseif ($this->hasColumn($column)) { |
||
118 | $columns[$key] = "{$this->getTable()}.{$column}"; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | $args->set('columns', $columns); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Handle querying relational mappings. |
||
127 | * |
||
128 | * @param \Sofa\Eloquence\Builder $query |
||
129 | * @param string $method |
||
130 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
131 | * @param string $mapping |
||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function mappedRelationQuery($query, $method, ArgumentBag $args, $mapping) |
||
135 | { |
||
136 | list($target, $column) = $this->parseMappedColumn($mapping); |
||
137 | |||
138 | if (in_array($method, ['pluck', 'value', 'aggregate', 'orderBy', 'lists'])) { |
||
139 | return $this->mappedJoinQuery($query, $method, $args, $target, $column); |
||
140 | } |
||
141 | |||
142 | return $this->mappedHasQuery($query, $method, $args, $target, $column); |
||
143 | } |
||
144 | |||
145 | /** |
||
146 | * Join mapped table(s) in order to call given method. |
||
147 | * |
||
148 | * @param \Sofa\Eloquence\Builder $query |
||
149 | * @param string $method |
||
150 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
151 | * @param string $target |
||
152 | * @param string $column |
||
153 | * @return mixed |
||
154 | */ |
||
155 | protected function mappedJoinQuery($query, $method, ArgumentBag $args, $target, $column) |
||
156 | { |
||
157 | $table = $this->joinMapped($query, $target); |
||
158 | |||
159 | // For aggregates we need the actual function name |
||
160 | // so it can be called directly on the builder. |
||
161 | $method = $args->get('function') ?: $method; |
||
162 | |||
163 | return (in_array($method, ['orderBy', 'lists', 'pluck'])) |
||
164 | ? $this->{"{$method}Mapped"}($query, $args, $table, $column, $target) |
||
165 | : $this->mappedSingleResult($query, $method, "{$table}.{$column}"); |
||
166 | } |
||
167 | |||
168 | /** |
||
169 | * Order query by mapped attribute. |
||
170 | * |
||
171 | * @param \Sofa\Eloquence\Builder $query |
||
172 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
173 | * @param string $table |
||
174 | * @param string $column |
||
175 | * @param string $target |
||
176 | * @return \Sofa\Eloquence\Builder |
||
177 | */ |
||
178 | protected function orderByMapped(Builder $query, ArgumentBag $args, $table, $column, $target) |
||
179 | { |
||
180 | $query->with($target)->getQuery()->orderBy("{$table}.{$column}", $args->get('direction')); |
||
181 | |||
182 | return $query; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @param \Sofa\Eloquence\Builder $query |
||
187 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
188 | * @param string $table |
||
189 | * @param string $column |
||
190 | * |
||
191 | * @return array |
||
192 | */ |
||
193 | protected function listsMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
194 | { |
||
195 | return $this->pluckMapped($query, $args, $table, $column); |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * Get an array with the values of given mapped attribute. |
||
200 | * |
||
201 | * @param \Sofa\Eloquence\Builder $query |
||
202 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
203 | * @param string $table |
||
204 | * @param string $column |
||
205 | * @return array |
||
206 | */ |
||
207 | protected function pluckMapped(Builder $query, ArgumentBag $args, $table, $column) |
||
208 | { |
||
209 | $query->select("{$table}.{$column}"); |
||
210 | |||
211 | if (!is_null($args->get('key'))) { |
||
212 | $this->mappedSelectListsKey($query, $args->get('key')); |
||
213 | } |
||
214 | |||
215 | $args->set('column', $column); |
||
216 | |||
217 | return $query->callParent('pluck', $args->all()); |
||
218 | } |
||
219 | |||
220 | /** |
||
221 | * Add select clause for key of the list array. |
||
222 | * |
||
223 | * @param \Sofa\Eloquence\Builder $query |
||
224 | * @param string $key |
||
225 | * @return \Sofa\Eloquence\Builder |
||
226 | */ |
||
227 | protected function mappedSelectListsKey(Builder $query, $key) |
||
228 | { |
||
229 | if ($this->hasColumn($key)) { |
||
230 | return $query->addSelect($this->getTable() . '.' . $key); |
||
231 | } |
||
232 | |||
233 | return $query->addSelect($key); |
||
234 | } |
||
235 | |||
236 | /** |
||
237 | * Join mapped table(s). |
||
238 | * |
||
239 | * @param \Sofa\Eloquence\Builder $query |
||
240 | * @param string $target |
||
241 | * @return string |
||
242 | */ |
||
243 | protected function joinMapped(Builder $query, $target) |
||
244 | { |
||
245 | $query->prefixColumnsForJoin(); |
||
246 | |||
247 | $parent = $this; |
||
248 | |||
249 | foreach (explode('.', $target) as $segment) { |
||
250 | list($table, $parent) = $this->joinSegment($query, $segment, $parent); |
||
251 | } |
||
252 | |||
253 | return $table; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Join relation's table accordingly. |
||
258 | * |
||
259 | * @param \Sofa\Eloquence\Builder $query |
||
260 | * @param string $segment |
||
261 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
262 | * @return array |
||
263 | */ |
||
264 | protected function joinSegment(Builder $query, $segment, EloquentModel $parent) |
||
265 | { |
||
266 | $relation = $parent->{$segment}(); |
||
267 | $related = $relation->getRelated(); |
||
268 | $table = $related->getTable(); |
||
269 | |||
270 | // If the table has been already joined let's skip it. Otherwise we will left join |
||
271 | // it in order to allow using some query methods on mapped columns. Polymorphic |
||
272 | // relations require also additional constraints, so let's handle it as well. |
||
273 | if (!$this->alreadyJoined($query, $table)) { |
||
274 | list($fk, $pk) = $this->getJoinKeys($relation); |
||
275 | |||
276 | $query->leftJoin($table, function ($join) use ($fk, $pk, $relation, $parent, $related) { |
||
277 | $join->on($fk, '=', $pk); |
||
278 | |||
279 | if ($relation instanceof MorphOne || $relation instanceof MorphTo) { |
||
280 | $morphClass = ($relation instanceof MorphOne) |
||
281 | ? $parent->getMorphClass() |
||
282 | : $related->getMorphClass(); |
||
283 | |||
284 | $join->where($relation->getQualifiedMorphType(), '=', $morphClass); |
||
285 | } |
||
286 | }); |
||
287 | } |
||
288 | |||
289 | return [$table, $related]; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Determine whether given table has been already joined. |
||
294 | * |
||
295 | * @param \Sofa\Eloquence\Builder $query |
||
296 | * @param string $table |
||
297 | * @return bool |
||
298 | */ |
||
299 | protected function alreadyJoined(Builder $query, $table) |
||
300 | { |
||
301 | $joined = Arr::pluck((array) $query->getQuery()->joins, 'table'); |
||
0 ignored issues
–
show
|
|||
302 | |||
303 | return in_array($table, $joined); |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Get the keys from relation in order to join the table. |
||
308 | * |
||
309 | * @param \Illuminate\Database\Eloquent\Relations\Relation $relation |
||
310 | * @return array |
||
311 | * |
||
312 | * @throws \LogicException |
||
313 | */ |
||
314 | protected function getJoinKeys(Relation $relation) |
||
315 | { |
||
316 | if ($relation instanceof HasOne || $relation instanceof MorphOne) { |
||
317 | return [$relation->getQualifiedForeignKeyName(), $relation->getQualifiedParentKeyName()]; |
||
318 | } |
||
319 | |||
320 | if ($relation instanceof BelongsTo && !$relation instanceof MorphTo) { |
||
321 | return [$relation->getQualifiedForeignKeyName(), $relation->getQualifiedOwnerKeyName()]; |
||
322 | } |
||
323 | |||
324 | $class = get_class($relation); |
||
325 | |||
326 | throw new LogicException( |
||
327 | "Only HasOne, MorphOne and BelongsTo mappings can be queried. {$class} given." |
||
328 | ); |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get single value result from the mapped attribute. |
||
333 | * |
||
334 | * @param \Sofa\Eloquence\Builder $query |
||
335 | * @param string $method |
||
336 | * @param string $qualifiedColumn |
||
337 | * @return mixed |
||
338 | */ |
||
339 | protected function mappedSingleResult(Builder $query, $method, $qualifiedColumn) |
||
340 | { |
||
341 | return $query->getQuery()->select("{$qualifiedColumn}")->{$method}("{$qualifiedColumn}"); |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Add whereHas subquery on the mapped attribute relation. |
||
346 | * |
||
347 | * @param \Sofa\Eloquence\Builder $query |
||
348 | * @param string $method |
||
349 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
350 | * @param string $target |
||
351 | * @param string $column |
||
352 | * @return \Sofa\Eloquence\Builder |
||
353 | */ |
||
354 | protected function mappedHasQuery(Builder $query, $method, ArgumentBag $args, $target, $column) |
||
355 | { |
||
356 | $boolean = $this->getMappedBoolean($args); |
||
357 | |||
358 | $operator = $this->getMappedOperator($method, $args); |
||
359 | |||
360 | $args->set('column', $column); |
||
361 | |||
362 | return $query |
||
363 | ->has($target, $operator, 1, $boolean, $this->getMappedWhereConstraint($method, $args)) |
||
364 | ->with($target); |
||
365 | } |
||
366 | |||
367 | /** |
||
368 | * Get the relation constraint closure. |
||
369 | * |
||
370 | * @param string $method |
||
371 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
372 | * @return \Closure |
||
373 | */ |
||
374 | protected function getMappedWhereConstraint($method, ArgumentBag $args) |
||
375 | { |
||
376 | return function ($query) use ($method, $args) { |
||
377 | call_user_func_array([$query, $method], $args->all()); |
||
378 | }; |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * Get boolean called on the original method and set it to default. |
||
383 | * |
||
384 | * @param \Sofa\EloquenceArgumentBag $args |
||
385 | * @return string |
||
386 | */ |
||
387 | protected function getMappedBoolean(ArgumentBag $args) |
||
388 | { |
||
389 | $boolean = $args->get('boolean'); |
||
390 | |||
391 | $args->set('boolean', 'and'); |
||
392 | |||
393 | return $boolean; |
||
394 | } |
||
395 | |||
396 | /** |
||
397 | * Determine the operator for count relation query and set 'not' appropriately. |
||
398 | * |
||
399 | * @param string $method |
||
400 | * @param \Sofa\Hookable\Contracts\ArgumentBag $args |
||
401 | * @return string |
||
402 | */ |
||
403 | protected function getMappedOperator($method, ArgumentBag $args) |
||
404 | { |
||
405 | if ($not = $args->get('not')) { |
||
406 | $args->set('not', false); |
||
407 | } |
||
408 | |||
409 | if ($null = $this->isWhereNull($method, $args)) { |
||
410 | $args->set('not', true); |
||
411 | } |
||
412 | |||
413 | return ($not ^ $null) ? '<' : '>='; |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * Get the mapping key. |
||
418 | * |
||
419 | * @param string $key |
||
420 | * @return string|null |
||
421 | */ |
||
422 | public function getMappingForAttribute($key) |
||
423 | { |
||
424 | if ($this->hasMapping($key)) { |
||
425 | return $this->mappedAttributes[$key]; |
||
426 | } |
||
427 | } |
||
428 | |||
429 | /** |
||
430 | * Determine whether the mapping points to relation. |
||
431 | * |
||
432 | * @param string $mapping |
||
433 | * @return bool |
||
434 | */ |
||
435 | protected function relationMapping($mapping) |
||
436 | { |
||
437 | return strpos($mapping, '.') !== false; |
||
438 | } |
||
439 | |||
440 | /** |
||
441 | * Determine whether a mapping exists for an attribute. |
||
442 | * |
||
443 | * @param string $key |
||
444 | * @return bool |
||
445 | */ |
||
446 | public function hasMapping($key) |
||
447 | { |
||
448 | if (is_null($this->mappedAttributes)) { |
||
449 | $this->parseMappings(); |
||
450 | } |
||
451 | |||
452 | return array_key_exists((string) $key, $this->mappedAttributes); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Parse defined mappings into flat array. |
||
457 | * |
||
458 | * @return void |
||
459 | */ |
||
460 | protected function parseMappings() |
||
461 | { |
||
462 | $this->mappedAttributes = []; |
||
463 | |||
464 | foreach ($this->getMaps() as $attribute => $mapping) { |
||
465 | if (is_array($mapping)) { |
||
466 | $this->parseImplicitMapping($mapping, $attribute); |
||
467 | } else { |
||
468 | $this->mappedAttributes[$attribute] = $mapping; |
||
469 | } |
||
470 | } |
||
471 | } |
||
472 | |||
473 | /** |
||
474 | * Parse implicit mappings. |
||
475 | * |
||
476 | * @param array $attributes |
||
477 | * @param string $target |
||
478 | * @return void |
||
479 | */ |
||
480 | protected function parseImplicitMapping($attributes, $target) |
||
481 | { |
||
482 | foreach ($attributes as $attribute) { |
||
483 | $this->mappedAttributes[$attribute] = "{$target}.{$attribute}"; |
||
484 | } |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Map an attribute to a value. |
||
489 | * |
||
490 | * @param string $key |
||
491 | * @return mixed |
||
492 | */ |
||
493 | protected function mapAttribute($key) |
||
494 | { |
||
495 | $segments = explode('.', $this->getMappingForAttribute($key)); |
||
496 | |||
497 | return $this->getTarget($this, $segments); |
||
498 | } |
||
499 | |||
500 | /** |
||
501 | * Get mapped value. |
||
502 | * |
||
503 | * @param \Illuminate\Database\Eloquent\Model $target |
||
504 | * @param array $segments |
||
505 | * @return mixed |
||
506 | */ |
||
507 | protected function getTarget($target, array $segments) |
||
508 | { |
||
509 | foreach ($segments as $segment) { |
||
510 | if (!$target) { |
||
511 | return; |
||
512 | } |
||
513 | |||
514 | $target = $target->{$segment}; |
||
515 | } |
||
516 | |||
517 | return $target; |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Set value of a mapped attribute. |
||
522 | * |
||
523 | * @param string $key |
||
524 | * @param mixed $value |
||
525 | */ |
||
526 | View Code Duplication | protected function setMappedAttribute($key, $value) |
|
527 | { |
||
528 | $segments = explode('.', $this->getMappingForAttribute($key)); |
||
529 | |||
530 | $attribute = array_pop($segments); |
||
531 | |||
532 | if ($target = $this->getTarget($this, $segments)) { |
||
533 | $this->addTargetToSave($target); |
||
534 | |||
535 | $target->{$attribute} = $value; |
||
536 | } |
||
537 | } |
||
538 | |||
539 | /** |
||
540 | * Flag mapped model to be saved along with this model. |
||
541 | * |
||
542 | * @param \Illuminate\Database\Eloquent\Model $target |
||
543 | */ |
||
544 | protected function addTargetToSave($target) |
||
545 | { |
||
546 | if ($this !== $target) { |
||
547 | $this->targetsToSave[] = $target; |
||
548 | } |
||
549 | } |
||
550 | |||
551 | /** |
||
552 | * Save mapped relations. |
||
553 | * |
||
554 | * @return void |
||
555 | */ |
||
556 | protected function saveMapped() |
||
557 | { |
||
558 | foreach (array_unique($this->targetsToSave) as $target) { |
||
559 | $target->save(); |
||
560 | } |
||
561 | |||
562 | $this->targetsToSave = []; |
||
563 | } |
||
564 | |||
565 | /** |
||
566 | * Unset mapped attribute. |
||
567 | * |
||
568 | * @param string $key |
||
569 | * @return void |
||
570 | */ |
||
571 | View Code Duplication | protected function forget($key) |
|
572 | { |
||
573 | $mapping = $this->getMappingForAttribute($key); |
||
574 | |||
575 | list($target, $attribute) = $this->parseMappedColumn($mapping); |
||
576 | |||
577 | $target = $target ? $this->getTarget($this, explode('.', $target)) : $this; |
||
578 | |||
579 | unset($target->{$attribute}); |
||
580 | } |
||
581 | |||
582 | /** |
||
583 | * @codeCoverageIgnore |
||
584 | * |
||
585 | * @param string $key |
||
586 | * @param mixed $value |
||
587 | * |
||
588 | * @inheritdoc |
||
589 | */ |
||
590 | protected function mutateAttributeForArray($key, $value) |
||
591 | { |
||
592 | if ($this->hasMapping($key)) { |
||
593 | $value = $this->mapAttribute($key); |
||
594 | |||
595 | return $value instanceof Arrayable ? $value->toArray() : $value; |
||
596 | } |
||
597 | |||
598 | return parent::mutateAttributeForArray($key, $value); |
||
599 | } |
||
600 | |||
601 | /** |
||
602 | * Get the array of attribute mappings. |
||
603 | * |
||
604 | * @return array |
||
605 | */ |
||
606 | public function getMaps() |
||
607 | { |
||
608 | return (property_exists($this, 'maps')) ? $this->maps : []; |
||
609 | } |
||
610 | } |
||
611 |
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: