PHP Classes

File: src/Connection.php

Recommend this page to a friend!
  Classes of Vitaly   Queasy DB   src/Connection.php   Download  
File: src/Connection.php
Role: Class source
Content type: text/plain
Description: Class source
Class: Queasy DB
Execute queries by accessing class variables
Author: By
Last change:
Date: 3 years ago
Size: 2,220 bytes
 

Contents

Class file image Download
<?php

namespace queasy\db;

use
ArrayAccess;

use
InvalidArgumentException;

class
Connection
{
    const DEFAULT =
'sqlite::memory:';
    const
DEFAULT_DRIVER = 'sqlite';

    const
GENERIC_TEMPLATE = '%s:host=%s;port=%s;dbname=%s';
    const
SQLITE_TEMPLATE = 'sqlite:%s';

   
/**
     * @var string Connection string.
     */
   
private $string;

   
/**
     * Constructor.
     *
     * @param string|array|ArrayAccess $config String representing DSN, or array (or ArrayAccess instance) with database connection config options
     *
     * @throws DbException When $config doesn't represent a recognizable structure to build connection string
     */
   
public function __construct($config = null)
    {
        if (empty(
$config)) {
           
$this->string = static::DEFAULT;

            return;
        }

        if (
is_string($config)) {
           
$this->string = $config;

            return;
        }

        if (
is_array($config) || (is_object($config) && ($config instanceof ArrayAccess))) {
            if (isset(
$config['dsn'])) {
               
$this->string = $config['dsn'];

                return;
            }

           
$this->string = isset($config['driver'])
                ?
sprintf(
                    static::
GENERIC_TEMPLATE,
                   
$config['driver'],
                    isset(
$config['host'])? $config['host']: null,
                    isset(
$config['port'])? $config['port']: null,
                    isset(
$config['name'])? $config['name']: null)
                :
sprintf(
                    static::
SQLITE_TEMPLATE,
                    isset(
$config['path'])
                        ?
$config['path']
                        :
':memory:');

            return;
        }

        throw new
InvalidArgumentException('Wrong config argument.');
    }

   
/**
     * Returns generated connection string.
     *
     * @return string Connection string
     */
   
public function get()
    {
        return
$this->string;
    }

   
/**
     * Returns generated connection string when class instance is invoked as a function.
     *
     * @return string Connection string
     */
   
public function __invoke()
    {
        return
$this->get();
    }
}