<?php
define("DB_HOST",($_SERVER["SERVER_NAME"] == "localhost") ? "200.13.89.27" : "localhost");
define('DB_USER', 'checa_usr');
define('DB_PASS', 'Cr0n0m3tr4d0&$');
define('DB_NAME', 'checador');

class Connection {
    private $conn;
    public function __construct() {
        $this->conn = new PDO(
            "pgsql:host=".DB_HOST.";dbname=".DB_NAME, DB_USER, DB_PASS,
            array(PDO::ATTR_PERSISTENT => true)
        );
    }
    public function getConnection() {
        return $this->conn;
    }

    public function query() {}
}

try {
    $pdo = new PDO(
        "pgsql:host=" . DB_HOST . ";dbname=" . DB_NAME, DB_USER, DB_PASS,
        array(
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
            PDO::ATTR_PERSISTENT => true
        )
    );
} catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}

function SQL(string $sql, array $params = [])
{
    global $pdo;
    $stmt = $pdo->prepare($sql);
    foreach ($params as $key => $value) {
        // bind Parameter
        $stmt->bindParam($key, $value);
    }
    $stmt->execute($params);
    return $stmt->fetchAll();
}

function filter_by(array $array, array $fields): array
{
    $result = [];
    foreach ($array as $key => $value) {
        $result[$key] = [];
        foreach ($fields as $field) {
            $result[$key][$field] = $value[$field];
        }
    }
    return $result;
}