Passed
Push — develop ( a3aa5d...84fa8b )
by nguereza
06:38
created

Configuration::getAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 1
c 1
b 1
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file DbConfig.php
33
 *
34
 *  The connection configuration class
35
 *
36
 *  @package    Platine\Database
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   http://www.iacademy.cf
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database;
48
49
use PDO;
50
use Platine\Database\Driver\Driver;
51
use Platine\Database\Driver\MySQL;
52
use Platine\Database\Driver\Oracle;
53
use Platine\Database\Driver\PostgreSQL;
54
use Platine\Database\Driver\SQLite;
55
use Platine\Database\Driver\SQLServer;
56
57
/**
58
 * Class Configuration
59
 * @package Platine\Database
60
 */
61
class Configuration implements ConfigurationInterface
62
{
63
    /**
64
     * The connection driver to use
65
     * @var string
66
     */
67
    protected string $driver = 'mysql';
68
69
    /**
70
     * The driver character set
71
     * Only for some drivers
72
     * @var string
73
     */
74
    protected string $charset = 'UTF8';
75
76
    /**
77
     * The application name
78
     * Only for Microsoft SQL server
79
     * @var string
80
     */
81
    protected string $appname = '';
82
83
    /**
84
     * The connection host name
85
     * @var string
86
     */
87
    protected string $hostname = 'localhost';
88
89
    /**
90
     * The connection username
91
     * @var string
92
     */
93
    protected string $username = '';
94
95
    /**
96
     * The connection username
97
     * @var string
98
     */
99
    protected string $password = '';
100
101
    /**
102
     * The connection port. If null will use the standard
103
     * port for database server
104
     * @var int|null
105
     */
106
    protected ?int $port = null;
107
108
    /**
109
     * The connection database name to use
110
     * Note: for SQLite this is the path to the database file
111
     * @var string
112
     */
113
    protected string $database = '';
114
115
    /**
116
     * The database server collation to use
117
     * Only for MySQL
118
     * @var string
119
     */
120
    protected string $collation = 'utf8_general_ci';
121
122
    /**
123
     * The connection socket to use for if the driver is MySQL
124
     * @var string
125
     */
126
    protected string $socket = '';
127
128
    /**
129
     * Whether the connection is persistent
130
     * @var bool
131
     */
132
    protected bool $persistent = false;
133
134
135
    /**
136
     * The PDO connection options
137
     * @var array<mixed, mixed>
138
     */
139
    protected array $options = [
140
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
141
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
142
        PDO::ATTR_STRINGIFY_FETCHES => false,
143
        PDO::ATTR_EMULATE_PREPARES => false,
144
    ];
145
146
    /**
147
     * The connection attributes to customize some drivers
148
     * @var array<mixed, mixed>
149
     */
150
    protected array $attributes = [];
151
152
    /**
153
     * The list of SQL command to execute after connection
154
     * @var array<int, string>
155
     */
156
    protected array $commands = [];
157
158
    /**
159
     * Class constructor
160
     * @param array<string, mixed> $config the connection configuration
161
     */
162
    public function __construct(array $config = [])
163
    {
164
        $this->load($config);
165
    }
166
167
    /**
168
     * {@inheritedoc}
169
     */
170
    public function getDriverName(): string
171
    {
172
        return $this->driver;
173
    }
174
175
    /**
176
     * {@inheritedoc}
177
     */
178
    public function getCharset(): string
179
    {
180
        return $this->charset;
181
    }
182
183
    /**
184
     * {@inheritedoc}
185
     */
186
    public function getAppname(): string
187
    {
188
        return $this->appname;
189
    }
190
191
    /**
192
     * {@inheritedoc}
193
     */
194
    public function getHostname(): string
195
    {
196
        return $this->hostname;
197
    }
198
199
    /**
200
     * {@inheritedoc}
201
     */
202
    public function getUsername(): string
203
    {
204
        return $this->username;
205
    }
206
207
    /**
208
     * {@inheritedoc}
209
     */
210
    public function getPassword(): string
211
    {
212
        return $this->password;
213
    }
214
215
    /**
216
     * {@inheritedoc}
217
     */
218
    public function getPort(): ?int
219
    {
220
        return $this->port;
221
    }
222
223
    /**
224
     * {@inheritedoc}
225
     */
226
    public function getDatabase(): string
227
    {
228
        return $this->database;
229
    }
230
231
    /**
232
     * {@inheritedoc}
233
     */
234
    public function getCollation(): string
235
    {
236
        return $this->collation;
237
    }
238
239
    /**
240
     * {@inheritedoc}
241
     */
242
    public function getSocket(): string
243
    {
244
        return $this->socket;
245
    }
246
247
    /**
248
     * {@inheritedoc}
249
     */
250
    public function getOptions(): array
251
    {
252
        return $this->options;
253
    }
254
255
    /**
256
     * {@inheritedoc}
257
     */
258
    public function setOption($name, $value): self
259
    {
260
        $this->options[$name] = $value;
261
262
        return $this;
263
    }
264
265
    /**
266
     * {@inheritedoc}
267
     */
268
    public function getAttributes(): array
269
    {
270
        return $this->attributes;
271
    }
272
273
    /**
274
     * {@inheritedoc}
275
     */
276
    public function setAttribute($name, $value): self
277
    {
278
        $this->attributes[$name] = $value;
279
280
        return $this;
281
    }
282
283
    /**
284
     * {@inheritedoc}
285
     */
286
    public function hasAttribute(string $name): bool
287
    {
288
        return array_key_exists($name, $this->attributes);
289
    }
290
291
    /**
292
     * {@inheritedoc}
293
     */
294
    public function getAttribute(string $name, $default = null)
295
    {
296
        return $this->hasAttribute($name)
297
                       ? $this->attributes[$name]
298
                       : $default;
299
    }
300
301
    /**
302
     * {@inheritedoc}
303
     */
304
    public function getCommands(): array
305
    {
306
        return $this->commands;
307
    }
308
309
    /**
310
     * {@inheritedoc}
311
     */
312
    public function addCommand(string $command): self
313
    {
314
        $this->commands[] = $command;
315
316
        return $this;
317
    }
318
319
    /**
320
     * {@inheritedoc}
321
     */
322
    public function load(array $config): void
323
    {
324
        foreach ($config as $name => $value) {
325
            $key = str_replace('_', '', lcfirst(ucwords($name, '_')));
326
            if (property_exists($this, $key)) {
327
                $this->{$key} = $value;
328
            }
329
        }
330
    }
331
332
    /**
333
     * {@inheritedoc}
334
     */
335
    public function getDriverClassName(): string
336
    {
337
        $class = Driver::class;
338
339
        switch ($this->driver) {
340
            case 'mysql':
341
                $class = MySQL::class;
342
                break;
343
            case 'pgsql':
344
                $class = PostgreSQL::class;
345
                break;
346
            case 'sqlsrv':
347
                $class = SQLServer::class;
348
                break;
349
            case 'oci':
350
            case 'oracle':
351
                $class = Oracle::class;
352
                break;
353
            case 'sqlite':
354
                $class = SQLite::class;
355
                break;
356
        }
357
358
        return $class;
359
    }
360
361
    /**
362
     * {@inheritedoc}
363
     */
364
    public function isPersistent(): bool
365
    {
366
        return $this->persistent;
367
    }
368
}
369