Passed
Push — develop ( e333c2...b3554d )
by nguereza
02:25
created

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