add find method to node repo and impl to fake repo
This commit is contained in:
parent
725b895de9
commit
483110f773
2 changed files with 36 additions and 1 deletions
|
|
@ -7,4 +7,6 @@ use App\Node\Node;
|
||||||
interface NodeRepository
|
interface NodeRepository
|
||||||
{
|
{
|
||||||
public function create(CreateNodeDto $dto): Node;
|
public function create(CreateNodeDto $dto): Node;
|
||||||
|
|
||||||
|
public function find(int $id): ?Node;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,44 @@ use App\Node\NodeRepository;
|
||||||
|
|
||||||
class FakeNodeRepository implements NodeRepository
|
class FakeNodeRepository implements NodeRepository
|
||||||
{
|
{
|
||||||
|
private array $existingNodes = [];
|
||||||
|
|
||||||
public function create(CreateNodeDto $dto): Node
|
public function create(CreateNodeDto $dto): Node
|
||||||
{
|
{
|
||||||
return new Node(
|
$id = $this->nextId();
|
||||||
|
$node = new Node(
|
||||||
|
id: $id,
|
||||||
text: $dto->text,
|
text: $dto->text,
|
||||||
title: $dto->title,
|
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(),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue