From e024ed2c65160ce3bae97d64bbc7b5f3a1a2c0e0 Mon Sep 17 00:00:00 2001 From: lexx Date: Tue, 12 Dec 2023 14:08:39 +0200 Subject: [PATCH] fixed edges again --- src/Query/Graph.php | 15 +++++++++++---- src/Query/Query.php | 9 ++++++--- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/Query/Graph.php b/src/Query/Graph.php index 73a562c..55e34ac 100644 --- a/src/Query/Graph.php +++ b/src/Query/Graph.php @@ -17,6 +17,7 @@ final class Graph public Collection $records, public Collection $edges, public Collection $parentEdges, + public QueryOptions $queryOptions, public ?int $total = null, ) { @@ -41,14 +42,18 @@ final class Graph return $this->getRootRecords() ->map(fn($r) => $this->findParents($r)) ->map(fn($r) => $this->findChildren($r)); + } public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord { - $recordEdges = $this->edges->filter(fn(Edge $ed) => $ed->source === $record->id && $ed->depth === $depth) - ->unique(fn(Edge $ed) => $ed->targetSchema . $ed->field . $ed->target . $ed->source . $ed->depth) + if($this->queryOptions->childrenDepth < $depth){ + return $record; + } + $recordEdges = $this->edges + ->filter(fn(Edge $ed) => $ed->source === $record->id ) + ->unique(fn(Edge $ed) => $ed->targetSchema . $ed->field . $ed->target . $ed->source) ->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); - $groupRecordEdges = []; foreach ($recordEdges as $element) { $groupRecordEdges[$element->field][] = $element; @@ -73,7 +78,9 @@ final class Graph public function findParents(QueryRecord $record, int $depth = 1): QueryRecord { - + if($this->queryOptions->parentsDepth < $depth){ + return $record; + } $recordEdges = $this->parentEdges->filter(fn(Edge $ed) => $ed->target === $record->id)->where("depth", $depth)->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); $groupRecordEdges = []; diff --git a/src/Query/Query.php b/src/Query/Query.php index eeccab5..e6a5834 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -78,8 +78,9 @@ final class Query // ->unique(fn($edge) => $edge->source . $edge->target . $edge->field) // ->toArray(); + $formattedRecords = $this->formatRecords($resultsRecordsUnique, $resultChildrenEdges, $resultParentEdges); $this->reset(); - return $this->formatRecords($resultsRecordsUnique, $resultChildrenEdges, $resultParentEdges); + return $formattedRecords; } @@ -105,19 +106,21 @@ final class Query return Edge::fromArray((array)$edgeData); - })->sortBy("rank")->values()->toArray(); + })->sortBy(['depth', 'asc'], ['rank', 'desc'])->values()->toArray(); $queryParentEdges = collect($parentEdges)->map(function ($edgeData) { return Edge::fromArray((array)$edgeData); - })->sortBy("rank")->values()->toArray(); + })->sortBy(['depth', 'asc'], ['rank', 'desc'])->values()->toArray(); + return new Graph( new Collection($queryRecords), new Collection($queryEdges), new Collection($queryParentEdges), + $this->options, ); }