<?php

namespace App\Core\Database;

use PDO;
use PDOException;

class Db
{
    /**
     * @var array|mixed
     */
    private array $config;
    private PDO $connection;

    public function __construct($config = [])
    {
        $this->config = $config;
    }

    public function connect()
    {
        try {
            $this->connection = new PDO($this->config['dsn'], $this->config['login'], $this->config['password']);
            $this->connection->exec("set names utf8");
            $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        } catch (PDOException $e) {
            echo "Error!: " . $e->getMessage() . "\n";
            throw $e;
        }
    }

    public function query(string $sql, array $params = [])
    {
        $stmt = $this->connection->prepare($sql);
        $stmt->execute($params);

        return $stmt;
    }
}