<?php

namespace Core\Controller;

class Controller
{
    protected function view(string $view, array $data = [])
    {
        extract($data);

        ob_start();

        require __DIR__ . '/../../views/' . $view . '.php';

        $content = ob_get_clean();

        if ($this->isAjax()) {
            echo $content;
            return;
        }

        require __DIR__ . '/../../views/layout/main.php';
    }

    protected function authView(string $view, array $data = [])
    {
        extract($data);

        ob_start();

        require VIEW_PATH . '/' . $view . '.php';

        $content = ob_get_clean();

        require VIEW_PATH . '/layout/auth.php';
    }
    protected function adminView(string $view, array $data = [])
    {
        extract($data);

        ob_start();

        require VIEW_PATH . '/' . $view . '.php';

        $content = ob_get_clean();

        if ($this->isAjax()) {
            echo $content;
            return;
        }

        require VIEW_PATH . '/layout/admin.php';
    }
    private function isAjax(): bool
    {
        return isset($_SERVER['HTTP_X_REQUESTED_WITH'])
            && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest';
    }
}