diff --git a/src/Query/Graph.php b/src/Query/Graph.php index 16e8a62..96b66c4 100644 --- a/src/Query/Graph.php +++ b/src/Query/Graph.php @@ -27,6 +27,7 @@ final class Graph * */ public function getRootRecords(): Collection { + return $this->records->where("isRoot", true)->values(); } @@ -35,58 +36,19 @@ final class Graph return !empty($this->records); } -// public function getRootRecordsWithChildren(): Collection -// { -// $rootRecords = $this->records->where("isRoot", true)->values(); -// return $rootRecords->map([$this, 'findChildren']); -// } -// -// public function findChildren(QueryRecord $record): QueryRecord -// { -// $recordEdges = $this->edges -// ->where("source", $record->id) -// ->values() -// ->sortBy("rank") -// ->groupBy("field")->toArray(); -// -// -// foreach ($recordEdges as $field => $edges) { -// $recordEdges[$field] = []; -// foreach ($edges as $anEdge) { -// $aRecord = $this->records->where("id", $anEdge->target)->first(); -// if (empty($aRecord)) { -// continue; -// } -// $recordEdges[$field][] = $this->findChildren($aRecord); -// } -// } -// -// $record->_children = new Collection($recordEdges); -// return $record; -// } - public function tree(): Collection { return $this->getRootRecords() ->map([$this, 'findParents']) ->map([$this, 'findChildren']) ; - - return $rootRecords; - - - - return $this->records->filter(function (QueryRecord $record) { - return $this->edges->filter(fn(Edge $ed) => $ed->target == $record->id)->isEmpty(); - })->values() - ->map([$this, 'findChildren']); - } - public function findChildren(QueryRecord $record): QueryRecord + public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord { $recordEdges = $this->edges->filter(fn(Edge $ed) => $ed->source === $record->id) - ->unique(fn(Edge $ed) => $ed->targetSchema.$ed->field.$ed->target.$ed->source) + ->unique(fn(Edge $ed) => $ed->targetSchema.$ed->field.$ed->target.$ed->source.$ed->depth) + ->where("depth", $depth) ->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); $groupRecordEdges = []; @@ -103,7 +65,7 @@ final class Graph continue; } - $children[$field][] = $this->findChildren($aRecord[0]); + $children[$field][] = $this->findChildren($aRecord[0], $depth + 1); } } $record->_children = $children; @@ -111,9 +73,10 @@ final class Graph } - public function findParents(QueryRecord $record): QueryRecord + public function findParents(QueryRecord $record, int $depth = 1): QueryRecord { - $recordEdges = $this->parentEdges->filter(fn(Edge $ed) => $ed->target === $record->id)->values()->sort(fn($a, $b) => $a->rank <=> $b->rank)->values(); + + $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 = []; foreach ($recordEdges as $element) { @@ -129,7 +92,7 @@ final class Graph continue; } - $parents[$field][] = $this->findParents($aRecord[0]); + $parents[$field][] = $this->findParents($aRecord[0],$depth + 1); } } $record->_parents = $parents;