<?php
declare(strict_types=1);

namespace Core;

abstract class Controller
{
    protected View $view;

    public function __construct()
    {
        $this->view = new View();
    }

    /** Subclasses may declare middleware to run before any action. */
    public function middleware(): array { return []; }

    protected function render(string $template, array $vars = []): void
    {
        $this->view->render($template, $vars);
    }

    protected function json(mixed $data, int $code = 200): void
    {
        $this->view->json($data, $code);
    }

    protected function input(): array
    {
        if (str_contains((string)($_SERVER['CONTENT_TYPE'] ?? ''), 'application/json')) {
            return input_json();
        }
        return $_POST;
    }
}
