add set layout api

This commit is contained in:
Yisroel Baum 2026-08-08 23:32:45 +03:00
parent eb7ac7d261
commit 4222d4c0a2
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
16 changed files with 487 additions and 6 deletions

View file

@ -14,6 +14,11 @@ interface ElementRepository
public function find(int $id): ?Element;
/**
* @return list<Element>
*/
public function findBySet(Set $set): array;
/**
* @return list<Element>
*/

View file

@ -45,6 +45,37 @@ class EloquentElementRepository implements ElementRepository
return $model === null ? null : $this->toDomain($model);
}
public function findBySet(Set $set): array
{
$models = ElementModel::query()
->where('set_id', $set->getId())
->orderBy('id')
->get();
$elements = [];
$elementsById = [];
foreach ($models as $model) {
$parentElement = null;
if ($model->parent_element_id !== null) {
$parentElement = $elementsById[$model->parent_element_id]
?? null;
if ($parentElement === null) {
throw new RuntimeException('element parent not found');
}
}
$element = $this->toDomainWithRelations(
model: $model,
set: $set,
parentElement: $parentElement,
);
$elements[] = $element;
$elementsById[$element->getId()] = $element;
}
return $elements;
}
public function findTopLevelBySet(Set $set): array
{
$models = ElementModel::query()
@ -160,10 +191,9 @@ class EloquentElementRepository implements ElementRepository
private function findSet(int $id): Set
{
foreach ($this->setRepository->all() as $set) {
if ($set->getId() === $id) {
return $set;
}
$set = $this->setRepository->find($id);
if ($set !== null) {
return $set;
}
throw new RuntimeException('element set not found');