Passed
Push — develop ( d2d625...405251 )
by nguereza
03:36
created

Configuration::hasAttribute()   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 1
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 connection name
71
     * @var string
72
     */
73
    protected string $name = 'default';
74
75
    /**
76
     * The driver character set
77
     * Only for some drivers
78
     * @var string
79
     */
80
    protected string $charset = 'UTF8';
81
82
    /**
83
     * The application name
84
     * Only for Microsoft SQL server
85
     * @var string
86
     */
87
    protected string $appname = '';
88
89
    /**
90
     * The connection host name
91
     * @var string
92
     */
93
    protected string $hostname = 'localhost';
94
95
    /**
96
     * The connection username
97
     * @var string
98
     */
99
    protected string $username = '';
100
101
    /**
102
     * The connection username
103
     * @var string
104
     */
105
    protected string $password = '';
106
107
    /**
108
     * The connection port. If null will use the standard
109
     * port for database server
110
     * @var int|null
111
     */
112
    protected ?int $port = null;
113
114
    /**
115
     * The connection database name to use
116
     * Note: for SQLite this is the path to the database file
117
     * @var string
118
     */
119
    protected string $database = '';
120
121
    /**
122
     * The database server collation to use
123
     * Only for MySQL
124
     * @var string
125
     */
126
    protected string $collation = 'utf8_general_ci';
127
128
    /**
129
     * The connection socket to use for if the driver is MySQL
130
     * @var string
131
     */
132
    protected string $socket = '';
133
134
    /**
135
     * Whether the connection is persistent
136
     * @var bool
137
     */
138
    protected bool $persistent = false;
139
140
141
    /**
142
     * The PDO connection options
143
     * @var array<mixed, mixed>
144
     */
145
    protected array $options = [
146
        PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
147
        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
148
        PDO::ATTR_STRINGIFY_FETCHES => false,
149
        PDO::ATTR_EMULATE_PREPARES => false,
150
    ];
151
152
    /**
153
     * The connection attributes to customize some drivers
154
     * @var array<mixed, mixed>
155
     */
156
    protected array $attributes = [];
157
158
    /**
159
     * The list of SQL command to execute after connection
160
     * @var array<int, string>
161
     */
162
    protected array $commands = [];
163
164
    /**
165
     * Class constructor
166
     * @param array<string, mixed> $config the connection configuration
167
     */
168
    public function __construct(array $config = [])
169
    {
170
        $this->load($config);
171
    }
172
173
    /**
174
     * {@inheritedoc}
175
     */
176
    public function getDriverName(): string
177
    {
178
        return $this->driver;
179
    }
180
181
    /**
182
     * {@inheritedoc}
183
     */
184
    public function getName(): string
185
    {
186
        return $this->name;
187
    }
188
189
    /**
190
     * {@inheritedoc}
191
     */
192
    public function getCharset(): string
193
    {
194
        return $this->charset;
195
    }
196
197
    /**
198
     * {@inheritedoc}
199
     */
200
    public function getAppname(): string
201
    {
202
        return $this->appname;
203
    }
204
205
    /**
206
     * {@inheritedoc}
207
     */
208
    public function getHostname(): string
209
    {
210
        return $this->hostname;
211
    }
212
213
    /**
214
     * {@inheritedoc}
215
     */
216
    public function getUsername(): string
217
    {
218
        return $this->username;
219
    }
220
221
    /**
222
     * {@inheritedoc}
223
     */
224
    public function getPassword(): string
225
    {
226
        return $this->password;
227
    }
228
229
    /**
230
     * {@inheritedoc}
231
     */
232
    public function getPort(): ?int
233
    {
234
        return $this->port;
235
    }
236
237
    /**
238
     * {@inheritedoc}
239
     */
240
    public function getDatabase(): string
241
    {
242
        return $this->database;
243
    }
244
245
    /**
246
     * {@inheritedoc}
247
     */
248
    public function getCollation(): string
249
    {
250
        return $this->collation;
251
    }
252
253
    /**
254
     * {@inheritedoc}
255
     */
256
    public function getSocket(): string
257
    {
258
        return $this->socket;
259
    }
260
261
    /**
262
     * {@inheritedoc}
263
     */
264
    public function getOptions(): array
265
    {
266
        return $this->options;
267
    }
268
269
    /**
270
     * {@inheritedoc}
271
     */
272
    public function setOption($name, $value): self
273
    {
274
        $this->options[$name] = $value;
275
276
        return $this;
277
    }
278
279
    /**
280
     * {@inheritedoc}
281
     */
282
    public function setOptions(array $options): self
283
    {
284
        foreach ($options as $name => $value) {
285
            $this->setOption($name, $value);
286
        }
287
288
        return $this;
289
    }
290
291
    /**
292
     * {@inheritedoc}
293
     */
294
    public function getAttributes(): array
295
    {
296
        return $this->attributes;
297
    }
298
299
    /**
300
     * {@inheritedoc}
301
     */
302
    public function setAttribute($name, $value): self
303
    {
304
        $this->attributes[$name] = $value;
305
306
        return $this;
307
    }
308
309
    /**
310
     * {@inheritedoc}
311
     */
312
    public function setAttributes(array $attributes): self
313
    {
314
        foreach ($attributes as $name => $value) {
315
            $this->setAttribute($name, $value);
316
        }
317
318
        return $this;
319
    }
320
321
    /**
322
     * {@inheritedoc}
323
     */
324
    public function hasAttribute(string $name): bool
325
    {
326
        return array_key_exists($name, $this->attributes);
327
    }
328
329
    /**
330
     * {@inheritedoc}
331
     */
332
    public function getAttribute(string $name, $default = null)
333
    {
334
        return $this->hasAttribute($name)
335
                       ? $this->attributes[$name]
336
                       : $default;
337
    }
338
339
    /**
340
     * {@inheritedoc}
341
     */
342
    public function getCommands(): array
343
    {
344
        return $this->commands;
345
    }
346
347
    /**
348
     * {@inheritedoc}
349
     */
350
    public function addCommand(string $command): self
351
    {
352
        $this->commands[] = $command;
353
354
        return $this;
355
    }
356
357
    /**
358
     * {@inheritedoc}
359
     */
360
    public function addCommands(array $commands): self
361
    {
362
        foreach ($commands as $command) {
363
            $this->addCommand($command);
364
        }
365
366
        return $this;
367
    }
368
369
    /**
370
     * {@inheritedoc}
371
     */
372
    public function load(array $config): void
373
    {
374
        foreach ($config as $name => $value) {
375
            $key = str_replace('_', '', lcfirst(ucwords($name, '_')));
376
            if (property_exists($this, $key)) {
377
                if (in_array($key, ['options', 'attributes', 'commands']) && is_array($value)) {
378
                    $method = 'set' . ucfirst($key);
379
                    if ($key === 'commands') {
380
                        $method = 'addCommands';
381
                    }
382
                    $this->{$method}($value);
383
                } else {
384
                    $this->{$key} = $value;
385
                }
386
            }
387
        }
388
    }
389
390
    /**
391
     * {@inheritedoc}
392
     */
393
    public function getDriverClassName(): string
394
    {
395
        $class = Driver::class;
396
397
        switch ($this->driver) {
398
            case 'mysql':
399
                $class = MySQL::class;
400
                break;
401
            case 'pgsql':
402
                $class = PostgreSQL::class;
403
                break;
404
            case 'sqlsrv':
405
                $class = SQLServer::class;
406
                break;
407
            case 'oci':
408
            case 'oracle':
409
                $class = Oracle::class;
410
                break;
411
            case 'sqlite':
412
                $class = SQLite::class;
413
                break;
414
        }
415
416
        return $class;
417
    }
418
419
    /**
420
     * {@inheritedoc}
421
     */
422
    public function isPersistent(): bool
423
    {
424
        return $this->persistent;
425
    }
426
}
427