add find method to node repo and impl to fake repo

This commit is contained in:
Yisroel Baum 2026-02-21 22:14:10 +02:00
parent 725b895de9
commit 483110f773
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 36 additions and 1 deletions

View file

@ -7,4 +7,6 @@ use App\Node\Node;
interface NodeRepository
{
public function create(CreateNodeDto $dto): Node;
public function find(int $id): ?Node;
}

View file

@ -8,11 +8,44 @@ use App\Node\NodeRepository;
class FakeNodeRepository implements NodeRepository
{
private array $existingNodes = [];
public function create(CreateNodeDto $dto): Node
{
return new Node(
$id = $this->nextId();
$node = new Node(
id: $id,
text: $dto->text,
title: $dto->title,
parentNode: $dto->parentNode,
);
$this->existingNodes[$id] = $node;
return $node;
}
private function nextId(): int
{
return count($this->existingNodes);
}
public function find(int $id): ?Node
{
$node = array_find(
$this->existingNodes,
function (Node $node) use ($id) {
return $node->getId() === $id;
}
);
if ($node === null) {
return nullj;
}
return new Node(
id: $id,
title: $node->getTitle(),
text: $node->getText(),
parentNode: $node->getParentNode(),
);
}
}