Skip to content

Commit

Permalink
Refactor populateRelation() to separate ArrayAccess implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed May 27, 2024
1 parent 3beda50 commit abea1b2
Showing 1 changed file with 21 additions and 13 deletions.
34 changes: 21 additions & 13 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,44 +283,51 @@ public function populateRelation(string $name, array &$primaryModels): array

$this->indexBy($indexBy);

$indexBy = $this->getIndexBy();

if ($indexBy !== null && $this->multiple) {
$buckets = $this->indexBuckets($buckets, $indexBy);
}

$link = array_values($this->link);
if (isset($viaQuery)) {
$deepViaQuery = $viaQuery;

while ($deepViaQuery->via) {
$deepViaQuery = is_array($deepViaQuery->via) ? $deepViaQuery->via[1] : $deepViaQuery->via;
}

$link = array_values($deepViaQuery->link);
$link = $deepViaQuery->link;
} else {
$link = $this->link;
}

foreach ($primaryModels as $i => $primaryModel) {
$keys = null;

if ($this->multiple && count($link) === 1) {
$primaryModelKey = reset($link);
$keys = $primaryModel[$primaryModelKey] ?? null;

if ($primaryModel instanceof ActiveRecordInterface) {
$keys = $primaryModel->getAttribute($primaryModelKey);
} else {
$keys = $primaryModel[$primaryModelKey] ?? null;
}
}

if (is_array($keys)) {
$value = [];

foreach ($keys as $key) {
$key = $this->normalizeModelKey($key);
if (isset($buckets[$key])) {
if ($indexBy !== null) {
/** if indexBy is set, array_merge will cause renumbering of numeric array */
foreach ($buckets[$key] as $bucketKey => $bucketValue) {
$value[$bucketKey] = $bucketValue;
}
} else {
$value = array_merge($value, $buckets[$key]);
}
$value[] = $buckets[$key];

Check warning on line 321 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L321

Added line #L321 was not covered by tests
}
}

if ($indexBy !== null) {

Check warning on line 325 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L325

Added line #L325 was not covered by tests
/** if indexBy is set, array_merge will cause renumbering of numeric array */
$value = array_replace(...$value);

Check warning on line 327 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L327

Added line #L327 was not covered by tests
} else {
$value = array_merge(...$value);

Check warning on line 329 in src/ActiveRelationTrait.php

View check run for this annotation

Codecov / codecov/patch

src/ActiveRelationTrait.php#L329

Added line #L329 was not covered by tests
}
} else {
$key = $this->getModelKey($primaryModel, $link);
$value = $buckets[$key] ?? ($this->multiple ? [] : null);
Expand All @@ -332,6 +339,7 @@ public function populateRelation(string $name, array &$primaryModels): array
$primaryModels[$i][$name] = $value;
}
}

if ($this->inverseOf !== null) {
$this->populateInverseRelation($primaryModels, $models, $name, $this->inverseOf);
}
Expand Down

0 comments on commit abea1b2

Please sign in to comment.