Completed
Push — master ( 99696f...35c3f5 )
by Rougin
02:37
created

DatabaseDriver::columns()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Rougin\Describe\Driver;
4
5
use Rougin\Describe\Exceptions\DriverNotFoundException;
6
7
/**
8
 * Database Driver
9
 *
10
 * A database driver for using available database drivers.
11
 *
12
 * @package Describe
13
 * @author  Rougin Royce Gutib <[email protected]>
14
 */
15
class DatabaseDriver implements DriverInterface
16
{
17
    /**
18
     * @var array
19
     */
20
    protected $data = array();
21
22
    /**
23
     * @var \Rougin\Describe\Driver\DriverInterface
24
     */
25
    protected $driver;
26
27
    /**
28
     * @var array
29
     */
30 30
    protected $mysql = array('mysql', 'mysqli');
31
32 30
    /**
33 30
     * @var array
34 27
     */
35
    protected $sqlite = array('pdo', 'sqlite', 'sqlite3');
36
37
    /**
38
     * Initializes the driver instance.
39
     *
40
     * @param string $name
41
     * @param array  $data
42
     */
43
    public function __construct($name, $data = array())
44 30
    {
45
        $this->data = $data;
46 30
47 30
        $this->driver = $this->driver($name, $data);
48
    }
49 30
50
    /**
51 30
     * Returns an array of Column instances from a table.
52 15
     *
53 15
     * @param  string $table
54
     * @return \Rougin\Describe\Column[]
55 15
     */
56 15
    public function columns($table)
57 12
    {
58
        return $this->driver->columns($table);
59 12
    }
60
61
    /**
62 3
     * Returns an array of Column instances from a table.
63
     * NOTE: To be removed in v2.0.0. Use columns() instead.
64
     *
65
     * @param  string $table
66
     * @return \Rougin\Describe\Column[]
67
     */
68
    public function getColumns($table)
69
    {
70
        return $this->columns($table);
71
    }
72 18
73
    /**
74 18
     * Returns an array of Column instances from a table.
75
     * NOTE: To be removed in v2.0.0. Use getColumns() instead.
76
     *
77
     * @param  string $table
78
     * @return \Rougin\Describe\Column[]
79
     */
80
    public function getTable($table)
81
    {
82
        return $this->getColumns($table);
83
    }
84
85 18
    /**
86
     * Returns an array of table names.
87 18
     * NOTE: To be removed in v2.0.0. Use tables() instead.
88
     *
89
     * @return array
90
     */
91
    public function getTableNames()
92
    {
93
        return $this->tables();
94
    }
95 9
96
    /**
97 9
     * Returns an array of table names.
98
     * NOTE: To be removed in v2.0.0. Use getTableNames() instead.
99
     *
100
     * @return array
101
     */
102
    public function showTables()
103
    {
104
        return $this->getTableNames();
105
    }
106 9
107
    /**
108 9
     * Returns an array of Table instances.
109
     *
110
     * @return \Rougin\Describe\Table[]
111
     */
112
    public function tables()
113
    {
114
        return $this->driver->tables();
115
    }
116
117 30
    /**
118
     * Returns the Driver instance from the configuration.
119 30
     *
120 30
     * @param  string $name
121 30
     * @param  array  $data
122 30
     * @return \Rougin\Describe\Driver\DriverInterface
123
     *
124 30
     * @throws \Rougin\Describe\Exceptions\DriverNotFoundException
125 27
     */
126 27
    protected function driver($name, $data = array())
127 27
    {
128 30
        if (in_array($name, $this->mysql) === true) {
129
            $dsn = 'mysql:host=' . $data['hostname'];
130 30
131
            $dsn .= ';dbname=' . $data['database'];
132
133
            $pdo = new \PDO($dsn, $data['username'], $data['password']);
134
135
            return new MySQLDriver($pdo, $data['database']);
136
        }
137
138
        if (in_array($name, $this->sqlite)) {
139
            $pdo = new \PDO($data['hostname']);
140
141
            return new SQLiteDriver($pdo);
142
        }
143
144
        throw new DriverNotFoundException;
145
    }
146
}
147