61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
<?php
|
|
|
|
namespace Tests\Fakes;
|
|
|
|
use App\Node\CreateNodeDto;
|
|
use App\Node\Node;
|
|
use App\Node\NodeRepository;
|
|
|
|
class FakeNodeRepository implements NodeRepository
|
|
{
|
|
private array $existingNodes = [];
|
|
|
|
public function create(CreateNodeDto $dto): 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 null;
|
|
}
|
|
|
|
return new Node(
|
|
id: $id,
|
|
title: $node->getTitle(),
|
|
text: $node->getText(),
|
|
parentNode: $node->getParentNode(),
|
|
);
|
|
}
|
|
|
|
public function findByTextId(int $id): array
|
|
{
|
|
return array_filter(
|
|
$this->existingNodes,
|
|
function (Node $node) use ($id) {
|
|
return $node->getId() === $id;
|
|
}
|
|
);
|
|
}
|
|
}
|