<?php
declare(strict_types=1); namespace App\Http\Controllers; use App\Application\BinderService; use App\Exceptions\NotFoundException; use App\Exceptions\HttpException; class BinderController { private BinderService $binderService; public function __construct(BinderService $binderService) { $this->binderService = $binderService; } public function index(): array { $binderes = $this->binderService->findAll(); return [ 'status' => 200, 'body' => $binderes ]; } public function store(string $payload): array { $data = json_decode($payload, true); if (!is_array($data) || empty($data['title'])) { throw new HttpException(422, 'Invalid payload. "title" is required.'); } $id = $this->binderService->createBinder($data); $binder = $this->binderService->findById($id); return [ 'status' => 201, 'body' => $binder ]; } public function show(string $id): array { $binder = $this->binderService->findById($id); return [ 'status' => 200, 'body' => $binder ]; } public function update(string $id, string $payload): array { $data = json_decode($payload, true); if (!is_array($data)) { throw new HttpException(422, 'Invalid payload.'); } $updateData = []; if (array_key_exists('title', $data)) { if (empty($data['title'])) { throw new HttpException(422, 'Invalid payload. "title" must not be empty.'); } $updateData['title'] = $data['title']; $updateData['description'] = $data['description'] ?? ''; } foreach (['scale', 'translateX', 'translateY'] as $viewportKey) { if (array_key_exists($viewportKey, $data)) { if (!is_numeric($data[$viewportKey])) { throw new HttpException(422, "Invalid payload. \"{$viewportKey}\" must be numeric."); } $updateData[$viewportKey] = (float)$data[$viewportKey]; } } if (array_key_exists('pinned', $data)) { if (!is_bool($data['pinned'])) { throw new HttpException(422, 'Invalid payload. "pinned" must be a boolean.'); } $updateData['pinned'] = $data['pinned']; } if (empty($updateData)) { throw new HttpException(422, 'Invalid payload. No updatable fields provided.'); } $this->binderService->updateBinder($id, $updateData); $binder = $this->binderService->findById($id); return [ 'status' => 200, 'body' => $binder ]; } public function updatePlacements(string $id, string $payload): array { $data = json_decode($payload, true); if (!is_array($data) || !isset($data['placements'])) { throw new HttpException(422, 'Invalid payload. "placements" array is required.'); } $this->binderService->updateBinder($id, ['placements' => $data['placements']]); return [ 'status' => 200, 'body' => ['message' => 'Placements updated successfully'] ]; } public function destroy(string $id): array { $this->binderService->deleteBinder($id); return [ 'status' => 200, 'body' => ['message' => 'Binder deleted successfully'] ]; } } 