fixed edges again
This commit is contained in:
+11
-4
@@ -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 = [];
|
||||
|
||||
+6
-3
@@ -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,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user