<?php

declare(strict_types=1);

namespace SeoScope\Database;

use PDO;

final class ConnectionFactory
{
    /**
     * @param array<string, mixed> $config Full config array (local.php)
     */
    public static function fromConfig(array $config): PDO
    {
        $dbp = $config['db'];
        $dsn = sprintf(
            'mysql:host=%s;port=%d;dbname=%s;charset=%s',
            (string) $dbp['host'],
            (int) $dbp['port'],
            (string) $dbp['database'],
            (string) ($dbp['charset'] ?? 'utf8mb4')
        );
        return new PDO($dsn, (string) $dbp['username'], (string) $dbp['password'], [
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
        ]);
    }
}
