Completed
Push — master ( 956484...f9dbde )
by WEBEWEB
01:39
created

AbstractDatabase::setDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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