Skip to content

Commit

Permalink
Improve
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Jun 9, 2024
1 parent 4851017 commit 7bd8f38
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/ActiveRelationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,15 +379,11 @@ private function buildBuckets(
foreach ($viaModels as $viaModel) {
$key1 = $this->getModelKeys($viaModel, $viaLinkKeys);
$key2 = $this->getModelKeys($viaModel, $linkValues);
$flags = array_fill_keys($key1, true);
$map[] = array_fill_keys($key2, array_fill_keys($key1, true));
}

foreach ($key2 as $key) {
if (isset($map[$key])) {
$map[$key] += $flags;
} else {
$map[$key] = $flags;
}
}
if (!empty($map)) {
$map = array_replace_recursive(...$map);
}

if ($viaQuery !== null) {
Expand Down Expand Up @@ -415,12 +411,10 @@ private function buildBuckets(
foreach ($models as $model) {
$keys = $this->getModelKeys($model, $linkKeys);
/** @var bool[][] $filtered */
$filtered = array_intersect_key($map, array_flip($keys));
$filtered = array_intersect_key($map, array_fill_keys($keys, null));

foreach ($filtered as $keys2) {
foreach (array_keys($keys2) as $key2) {
$buckets[$key2][] = $model;
}
foreach (array_keys(array_replace(...$filtered)) as $key2) {
$buckets[$key2][] = $model;
}
}
} else {
Expand Down Expand Up @@ -448,11 +442,7 @@ private function mapVia(array $map, array $viaMap): array
$resultMap = [];

foreach ($map as $key => $linkKeys) {
$resultMap[$key] = [];
foreach (array_keys($linkKeys) as $linkKey) {
/** @psalm-suppress InvalidArrayOffset */
$resultMap[$key] += $viaMap[$linkKey];
}
$resultMap[$key] = array_replace(...array_intersect_key($viaMap, $linkKeys));
}

return $resultMap;
Expand Down

0 comments on commit 7bd8f38

Please sign in to comment.