add element persistence
This commit is contained in:
parent
aaab0e5379
commit
fcc6ade8f3
7 changed files with 289 additions and 0 deletions
78
backend/tests/Fakes/FakeElementRepository.php
Normal file
78
backend/tests/Fakes/FakeElementRepository.php
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
<?php
|
||||
|
||||
namespace Tests\Fakes;
|
||||
|
||||
use App\Element\CreateElementDto;
|
||||
use App\Element\Element;
|
||||
use App\Element\ElementRepository;
|
||||
|
||||
class FakeElementRepository implements ElementRepository
|
||||
{
|
||||
/**
|
||||
* @var array<int, Element>
|
||||
*/
|
||||
private array $elements = [];
|
||||
|
||||
public function create(CreateElementDto $dto): Element
|
||||
{
|
||||
$id = count($this->elements) + 1;
|
||||
$element = new Element(
|
||||
id: $id,
|
||||
name: $dto->name,
|
||||
kind: $dto->kind,
|
||||
set: $dto->set,
|
||||
parentElement: $dto->parentElement,
|
||||
position: $this->nextPosition($dto),
|
||||
);
|
||||
$this->elements[$id] = $element;
|
||||
|
||||
return $this->copy($element);
|
||||
}
|
||||
|
||||
public function find(int $id): ?Element
|
||||
{
|
||||
$element = $this->elements[$id] ?? null;
|
||||
|
||||
return $element === null ? null : $this->copy($element);
|
||||
}
|
||||
|
||||
private function nextPosition(CreateElementDto $dto): int
|
||||
{
|
||||
$requestedParentId = $dto->parentElement?->getId();
|
||||
$maximumPosition = 0;
|
||||
|
||||
foreach ($this->elements as $element) {
|
||||
if ($element->getSet()->getId() !== $dto->set->getId()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$elementParentId = $element->getParentElement()?->getId();
|
||||
if ($elementParentId !== $requestedParentId) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$maximumPosition = max(
|
||||
$maximumPosition,
|
||||
$element->getPosition(),
|
||||
);
|
||||
}
|
||||
|
||||
return $maximumPosition + 1;
|
||||
}
|
||||
|
||||
private function copy(Element $element): Element
|
||||
{
|
||||
$parentElement = $element->getParentElement();
|
||||
|
||||
return new Element(
|
||||
id: $element->getId(),
|
||||
name: $element->getName(),
|
||||
kind: $element->getKind(),
|
||||
set: $element->getSet(),
|
||||
parentElement: $parentElement === null
|
||||
? null
|
||||
: $this->copy($parentElement),
|
||||
position: $element->getPosition(),
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue