fixed edges again
This commit is contained in:
+11
-4
@@ -17,6 +17,7 @@ final class Graph
|
|||||||
public Collection $records,
|
public Collection $records,
|
||||||
public Collection $edges,
|
public Collection $edges,
|
||||||
public Collection $parentEdges,
|
public Collection $parentEdges,
|
||||||
|
public QueryOptions $queryOptions,
|
||||||
public ?int $total = null,
|
public ?int $total = null,
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
@@ -41,14 +42,18 @@ final class Graph
|
|||||||
return $this->getRootRecords()
|
return $this->getRootRecords()
|
||||||
->map(fn($r) => $this->findParents($r))
|
->map(fn($r) => $this->findParents($r))
|
||||||
->map(fn($r) => $this->findChildren($r));
|
->map(fn($r) => $this->findChildren($r));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord
|
public function findChildren(QueryRecord $record, int $depth = 1): QueryRecord
|
||||||
{
|
{
|
||||||
$recordEdges = $this->edges->filter(fn(Edge $ed) => $ed->source === $record->id && $ed->depth === $depth)
|
if($this->queryOptions->childrenDepth < $depth){
|
||||||
->unique(fn(Edge $ed) => $ed->targetSchema . $ed->field . $ed->target . $ed->source . $ed->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();
|
->sort(fn($a, $b) => $a->rank <=> $b->rank)->values();
|
||||||
|
|
||||||
$groupRecordEdges = [];
|
$groupRecordEdges = [];
|
||||||
foreach ($recordEdges as $element) {
|
foreach ($recordEdges as $element) {
|
||||||
$groupRecordEdges[$element->field][] = $element;
|
$groupRecordEdges[$element->field][] = $element;
|
||||||
@@ -73,7 +78,9 @@ final class Graph
|
|||||||
|
|
||||||
public function findParents(QueryRecord $record, int $depth = 1): QueryRecord
|
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();
|
$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 = [];
|
$groupRecordEdges = [];
|
||||||
|
|||||||
+6
-3
@@ -78,8 +78,9 @@ final class Query
|
|||||||
// ->unique(fn($edge) => $edge->source . $edge->target . $edge->field)
|
// ->unique(fn($edge) => $edge->source . $edge->target . $edge->field)
|
||||||
// ->toArray();
|
// ->toArray();
|
||||||
|
|
||||||
|
$formattedRecords = $this->formatRecords($resultsRecordsUnique, $resultChildrenEdges, $resultParentEdges);
|
||||||
$this->reset();
|
$this->reset();
|
||||||
return $this->formatRecords($resultsRecordsUnique, $resultChildrenEdges, $resultParentEdges);
|
return $formattedRecords;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -105,19 +106,21 @@ final class Query
|
|||||||
|
|
||||||
return Edge::fromArray((array)$edgeData);
|
return Edge::fromArray((array)$edgeData);
|
||||||
|
|
||||||
})->sortBy("rank")->values()->toArray();
|
})->sortBy(['depth', 'asc'], ['rank', 'desc'])->values()->toArray();
|
||||||
|
|
||||||
$queryParentEdges = collect($parentEdges)->map(function ($edgeData) {
|
$queryParentEdges = collect($parentEdges)->map(function ($edgeData) {
|
||||||
|
|
||||||
return Edge::fromArray((array)$edgeData);
|
return Edge::fromArray((array)$edgeData);
|
||||||
|
|
||||||
})->sortBy("rank")->values()->toArray();
|
})->sortBy(['depth', 'asc'], ['rank', 'desc'])->values()->toArray();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return new Graph(
|
return new Graph(
|
||||||
new Collection($queryRecords),
|
new Collection($queryRecords),
|
||||||
new Collection($queryEdges),
|
new Collection($queryEdges),
|
||||||
new Collection($queryParentEdges),
|
new Collection($queryParentEdges),
|
||||||
|
$this->options,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user