Completed
Push — master ( d0e678...183caa )
by WEBEWEB
01:15
created

AbstractDatabaseConnector::connect()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
c 0
b 0
f 0
nc 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\Connector;
13
14
use Exception;
15
use PDO;
16
use WBW\Library\Core\Security\Authenticator;
17
18
/**
19
 * Abstract database connector.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Library\Core\Database\Connector
23
 */
24
abstract class AbstractDatabaseConnector {
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
     * @param Authenticator $authenticator The authenticator.
51
     */
52
    protected function __construct(Authenticator $authenticator) {
53
        $this->authenticator = $authenticator;
54
    }
55
56
    /**
57
     * Connect.
58
     *
59
     * @return PDO Returns the connection.
60
     * @throws Exception Throws an exception if the connection failed.
61
     */
62
    abstract protected function connect();
63
64
    /**
65
     * Get the authenticator.
66
     *
67
     * @return Authenticator Returns the authenticator.
68
     */
69
    public function getAuthenticator() {
70
        return $this->authenticator;
71
    }
72
73
    /**
74
     * Get the connection.
75
     *
76
     * @return PDO Returns the connection.
77
     * @throws Exception Throws an exception if the connection failed.
78
     */
79
    public function getConnection() {
80
        if (null === $this->connection) {
81
            $this->connection = $this->connect();
82
        }
83
        return $this->connection;
84
    }
85
86
    /**
87
     * Get the database.
88
     *
89
     * @return string Returns the database.
90
     */
91
    public function getDatabase() {
92
        return $this->database;
93
    }
94
95
    /**
96
     * Prepare a binding.
97
     *
98
     * @param array $fields The fields.
99
     * @return array Returns the binding as key => :key.
100
     */
101
    public function prepareBinding(array $fields) {
102
        $output = [];
103
        foreach ($fields as $current) {
104
            $output[$current] = ":" . $current;
105
        }
106
        return $output;
107
    }
108
109
    /**
110
     * Prepare an INSERT SQL query.
111
     *
112
     * @param string $table The table.
113
     * @param array $values The values [field => value].
114
     * @return string Returns the INSERT SQL query.
115
     */
116
    public function prepareInsert($table, array $values) {
117
118
        $query = [
119
            "INSERT INTO ",
120
            $table,
121
            " (`",
122
            implode("`, `", array_keys($values)),
123
            "`) VALUES (",
124
            implode(", ", array_values($values)),
125
            ")",
126
        ];
127
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
    public function prepareUpdate($table, array $values) {
139
140
        $set = [];
141
        foreach ($values as $k => $v) {
142
            $set[] = "`" . $k . "` = " . $v;
143
        }
144
145
        $query = [
146
            "UPDATE ",
147
            $table,
148
            " SET ",
149
            implode(", ", $set),
150
        ];
151
152
        return implode("", $query);
153
    }
154
155
    /**
156
     * Set the authenticator.
157
     *
158
     * @param Authenticator $authenticator The authenticator.
159
     * @return AbstractDatabase Returns this abstract database.
160
     */
161
    protected function setAuthenticator(Authenticator $authenticator) {
162
        $this->authenticator = $authenticator;
163
        return $this;
164
    }
165
166
    /**
167
     * Set the database.
168
     *
169
     * @param string $database The database.
170
     * @return AbstractDatabase Returns this abstract database.
171
     */
172
    protected function setDatabase($database) {
173
        $this->database = $database;
174
        return $this;
175
    }
176
}
177