Goal-Calibration/app/Node/NodeController.php

37 lines
1,001 B
PHP

<?php
namespace App\Node;
use App\Node\NodeRepository;
use App\Text\TextRepository;
use Psr\Http\Message\ResponseInterface as Response;
class NodeController
{
public function __construct(
private NodeRepository $nodeRepository,
private TextRepository $textRepository,
) {}
public function getNodesOfText(Response $response, int $textId): Response
{
$text = $this->textRepository->find($textId);
if ($text === null) {
return $response->withStatus(404);
}
$nodes = $this->nodeRepository->findByTextId($textId);
$data = array_map(function ($node) {
return [
'id' => $node->getId(),
'title' => $node->getTitle(),
'parentNodeId' => $node->getParentNode()?->getId(),
];
}, $nodes);
$response->getBody()->write(json_encode(array_values($data)));
return $response->withHeader('Content-Type', 'application/json');
}
}