From bd2f3bddffb1f48f02cb8eeffdb65e41b99cf25c Mon Sep 17 00:00:00 2001 From: Artur Welp Date: Wed, 19 Aug 2026 10:55:16 -0300 Subject: [PATCH] Some identation and documentation --- src/ORM/Connection.php | 84 +++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 18 deletions(-) diff --git a/src/ORM/Connection.php b/src/ORM/Connection.php index 68f76b6..67c39e6 100644 --- a/src/ORM/Connection.php +++ b/src/ORM/Connection.php @@ -3,29 +3,72 @@ namespace ORM; /** -* \PDO generator class -* -* This code is based on the TConnection.php class on the Adianti Framework -* -* @license http://www.adianti.com.br/framework-license -* -*/ + * \PDO generator class + * + * This code is based on the TConnection.php class on the Adianti Framework + * + * @license http://www.adianti.com.br/framework-license + * + */ -class Connection { +class Connection +{ - private function __construct() { + /** + * Available Database Drivers + * @var array + */ + const array drivers = ["pgsql", "mysql", "sqlite", "ibase", "oci8", "mssq"]; - } - public static function open($connection_info = null) { + /** + * Private constructor + * This class will not exist, it only encapsulate logic + */ + private function __construct(){ } - $user = isset($connection_info['user']) ? $connection_info['user'] : NULL; - $pass = isset($connection_info['pass']) ? $connection_info['pass'] : NULL; - $name = isset($connection_info['name']) ? $connection_info['name'] : NULL; - $host = isset($connection_info['host']) ? $connection_info['host'] : NULL; - $driver = isset($connection_info['driver']) ? $connection_info['driver'] : NULL; - $port = isset($connection_info['port']) ? $connection_info['port'] : NULL; - $prefix = isset($connection_info['prefix']) ? $connection_info['prefix'] : ''; + /** + * Open a new connection + * + * You need to pass a connection configuration in a Object/Array + * + * user + * pass + * name + * host + * driver + * - pgsql (Postgres) + * - mysql (MySQL) + * - sqlite (SQLite) + * - ibase (Firebird) + * - oci8 (Oracle) + * - mssql SQLServer + * port + * prefix + * + * @param mixed $connectionInfo + * + * @return \PDO + */ + public static function open(mixed $connectionInfo = []): \PDO + { + + if (is_array($connectionInfo)) { + $connectionInfo = (object) $connectionInfo; + } + + if(!isset($connectionInfo->driver)){ + throw new \Exception("Driver not defined"); + } + + $user = isset($connectionInfo->user) ? $connectionInfo->user : NULL; + $pass = isset($connectionInfo->pass) ? $connectionInfo->pass : NULL; + $name = isset($connectionInfo->name) ? $connectionInfo->name : NULL; + $host = isset($connectionInfo->host) ? $connectionInfo->host : NULL; + $driver = isset($connectionInfo->driver) ? $connectionInfo->driver : NULL; + $port = isset($connectionInfo->port) ? $connectionInfo->port : NULL; + $prefix = isset($connectionInfo->prefix) ? $connectionInfo->prefix : ''; + $traceSQL = isset($connectionInfo->traceSQL) ? $connectionInfo->traceSQL : false; switch ($driver) { case 'pgsql': @@ -49,8 +92,13 @@ class Connection { $conn = new \PDO("mssql:host={$host},1433;dbname={$name}", $user, $pass); break; } + $conn->prefix = $prefix; + $conn->driver = $driver; + $conn->traceSQL = $traceSQL; + $conn->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + return $conn; }