<?php
declare(strict_types=1);
namespace App\Core;

use PDO;

abstract class Repository {
    protected PDO $db;

    public function __construct() {
        $this->db = Database::instance();
    }

    protected function one(string $sql, array $params = []): ?array {
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        $row = $stmt->fetch();
        return $row ?: null;
    }

    protected function all(string $sql, array $params = []): array {
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        return $stmt->fetchAll();
    }

    protected function exec(string $sql, array $params = []): int {
        $stmt = $this->db->prepare($sql);
        $stmt->execute($params);
        return $stmt->rowCount();
    }

    protected function insert(string $sql, array $params = []): int {
        $this->exec($sql, $params);
        return (int)$this->db->lastInsertId();
    }
}
