Completed
Push — master ( e89bd9...69c880 )
by WEBEWEB
01:44
created

AbstractDatabase::prepareBinding()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 NdC/WBW
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Database;
13
14
use Exception;
15
use PDO;
16
17
/**
18
 * Abstract database.
19
 *
20
 * @author NdC/WBW <https://github.com/webeweb/>
21
 * @package WBW\Library\Core\Database
22
 */
23
abstract class AbstractDatabase {
24
25
    /**
26
     * Connection.
27
     *
28
     * @var PDO
29
     */
30
    private $connection;
31
32
    /**
33
     * Database.
34
     *
35
     * @var string
36
     */
37
    private $database;
38
39
    /**
40
     * Password.
41
     *
42
     * @var string
43
     */
44
    private $password;
45
46
    /**
47
     * Username.
48
     *
49
     * @var string
50
     */
51
    private $username;
52
53
    /**
54
     * Constructor.
55
     */
56
    protected function __construct() {
57
        // NOTHING TO DO.
58
    }
59
60
    /**
61
     * Connect.
62
     *
63
     * @return PDO Returns the connection.
64
     * @throws Exception Throws an exception if the connection failed.
65
     */
66
    abstract protected function connect();
67
68
    /**
69
     * Get the connection.
70
     *
71
     * @return PDO Returns the connection.
72
     * @throws Exception Throws an exception if the connection failed.
73
     */
74
    final public function getConnection() {
75
        if (null === $this->connection) {
76
            $this->connection = $this->connect();
77
        }
78
        return $this->connection;
79
    }
80
81
    /**
82
     * Get the database.
83
     *
84
     * @return string Returns the database.
85
     */
86
    final public function getDatabase() {
87
        return $this->database;
88
    }
89
90
    /**
91
     * Get the paswword.
92
     *
93
     * @return string Returns the password.
94
     */
95
    final public function getPassword() {
96
        return $this->password;
97
    }
98
99
    /**
100
     * Get the username.
101
     *
102
     * @return string Returns the username.
103
     */
104
    final public function getUsername() {
105
        return $this->username;
106
    }
107
108
    /**
109
     * Prepare a binding.
110
     *
111
     * @param array $fields The fields.
112
     * @return array Returns the binding as key => :key.
113
     */
114
    final public function prepareBinding(array $fields) {
115
        $output = [];
116
        foreach ($fields as $current) {
117
            $output[$current] = ":" . $current;
118
        }
119
        return $output;
120
    }
121
122
    /**
123
     * Prepare an INSERT SQL query.
124
     *
125
     * @param string $table The table.
126
     * @param array $fields The fields [field => value].
127
     * @return string Returns the INSERT SQL query.
128
     */
129
    final public function prepareInsert($table, array $fields) {
130
131
        // Initialize the SQL.
132
        $sql = [];
133
134
        $sql[] = "INSERT INTO ";
135
        $sql[] = $table;
136
        $sql[] = " (";
137
        $sql[] = implode(", ", array_keys($fields));
138
        $sql[] = ") VALUES (";
139
        $sql[] = implode(", ", array_values($fields));
140
        $sql[] = ")";
141
142
        // Return the SQL.
143
        return implode("", $sql);
144
    }
145
146
    /**
147
     * Set the database.
148
     *
149
     * @param string $database The database.
150
     * @return AbstractDatabase Returns the database.
151
     */
152
    final public function setDatabase($database) {
153
        $this->database = $database;
154
        return $this;
155
    }
156
157
    /**
158
     * Set the password.
159
     *
160
     * @param string $password The password.
161
     * @return AbstractDatabase Returns the database.
162
     */
163
    final public function setPassword($password) {
164
        $this->password = $password;
165
        return $this;
166
    }
167
168
    /**
169
     * Set the username.
170
     *
171
     * @param string $username The username.
172
     * @return AbstractDatabase Returns the database.
173
     */
174
    final public function setUsername($username) {
175
        $this->username = $username;
176
        return $this;
177
    }
178
179
}
180