textRepository->getAll(); $data = array_map(function ($text) { return [ 'id' => $text->getId(), 'name' => $text->getName(), ]; }, $texts); $response->getBody()->write(json_encode($data)); return $response->withHeader('Content-Type', 'application/json'); } public function getText(Response $response, int $textId): Response { $text = $this->textRepository->find($textId); if ($text === null) { return $response->withStatus(404); } $response->getBody()->write(json_encode([ 'id' => $text->getId(), 'name' => $text->getName(), ])); return $response->withHeader('Content-Type', 'application/json'); } public function createText( Request $request, Response $response, CreateText $createTextUseCase, ): Response { $data = $request->getParsedBody(); $name = $data['name'] ?? null; try { $text = $createTextUseCase->execute(new CreateTextRequest( name: $name, )); } catch (BadRequestException $e) { $response->getBody()->write(json_encode(['error' => $e->getMessage()])); return $response->withStatus(400)->withHeader('Content-Type', 'application/json'); } $response->getBody()->write(json_encode([ 'id' => $text->getId(), 'name' => $text->getName(), ])); return $response->withHeader('Content-Type', 'application/json'); } }