<?php

declare(strict_types=1);

namespace App\Framework\Database\Driver;

final readonly class MysqlDriver implements Driver
{
    public string $dsn;

    public array $options;

    public function __construct(
        public DriverConfig $config,
    ) {
        $this->dsn = $this->createDns();
        $this->options = $this->getOptions();
    }

    private function createDns(): string
    {
        return sprintf(
            'mysql:host=%s;port=%d;dbname=%s;charset=%s',
            $this->config->host,
            $this->config->port,
            $this->config->database,
            $this->config->charset,
        );
    }

    private function getOptions(): array
    {
        return [
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC,
            \PDO::ATTR_EMULATE_PREPARES => false,
        ];
    }
}
