staudenmeir /
eloquent-has-many-deep
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Staudenmeir\EloquentHasManyDeep; |
||
| 4 | |||
| 5 | use Illuminate\Database\Eloquent\Collection; |
||
| 6 | use Illuminate\Database\Eloquent\Model; |
||
| 7 | use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * @template TRelatedModel of \Illuminate\Database\Eloquent\Model |
||
| 11 | * |
||
| 12 | * @extends \Staudenmeir\EloquentHasManyDeep\HasManyDeep<TRelatedModel> |
||
| 13 | */ |
||
| 14 | class HasOneDeep extends HasManyDeep |
||
| 15 | { |
||
| 16 | use SupportsDefaultModels; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Get the results of the relationship. |
||
| 20 | * |
||
| 21 | * @return mixed |
||
| 22 | */ |
||
| 23 | public function getResults() |
||
| 24 | { |
||
| 25 | return $this->first() ?: $this->getDefaultFor(end($this->throughParents)); |
||
| 26 | } |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Initialize the relation on a set of models. |
||
| 30 | * |
||
| 31 | * @param array $models |
||
| 32 | * @param string $relation |
||
| 33 | * @return array |
||
| 34 | */ |
||
| 35 | public function initRelation(array $models, $relation) |
||
| 36 | { |
||
| 37 | foreach ($models as $model) { |
||
| 38 | $model->setRelation($relation, $this->getDefaultFor($model)); |
||
| 39 | } |
||
| 40 | |||
| 41 | return $models; |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Match the eagerly loaded results to their parents. |
||
| 46 | * |
||
| 47 | * @param array $models |
||
| 48 | * @param \Illuminate\Database\Eloquent\Collection $results |
||
| 49 | * @param string $relation |
||
| 50 | * @return array |
||
| 51 | */ |
||
| 52 | public function match(array $models, Collection $results, $relation) |
||
| 53 | { |
||
| 54 | if ($this->customEagerMatchingCallbacks) { |
||
|
0 ignored issues
–
show
|
|||
| 55 | foreach ($this->customEagerMatchingCallbacks as $callback) { |
||
| 56 | $models = $callback($models, $results, $relation, 'one'); |
||
| 57 | } |
||
| 58 | |||
| 59 | return $models; |
||
| 60 | } |
||
| 61 | |||
| 62 | $dictionary = $this->buildDictionary($results); |
||
| 63 | |||
| 64 | foreach ($models as $model) { |
||
| 65 | if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) { |
||
| 66 | $model->setRelation( |
||
| 67 | $relation, |
||
| 68 | reset($dictionary[$key]) |
||
| 69 | ); |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return $models; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * Make a new related instance for the given model. |
||
| 78 | * |
||
| 79 | * @param \Illuminate\Database\Eloquent\Model $parent |
||
| 80 | * @return \Illuminate\Database\Eloquent\Model |
||
| 81 | */ |
||
| 82 | public function newRelatedInstanceFor(Model $parent) |
||
| 83 | { |
||
| 84 | return $this->related->newInstance(); |
||
| 85 | } |
||
| 86 | } |
||
| 87 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.