Passed
Pull Request — master (#442)
by Wilmer
03:14 queued 55s
created

AbstractDsn::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Connection;
6
7
use Stringable;
8
9
/**
10
 * The Dsn class is typically used to parse a DSN string, which is a string that contains all the necessary information
11
 * to connect to a database, such as the database driver, hostname, database name, port and options.
12
 *
13
 * It also allows you to access individual components of the DSN, such as the driver or the database name.
14
 */
15
abstract class AbstractDsn implements Stringable
16
{
17
    /**
18
     * @psalm-param string[] $options
19
     */
20
    public function __construct(
21
        private string $driver,
22
        private string $host,
23
        private string $databaseName,
24
        private string|null $port = null,
25
        private array $options = []
26
    ) {
27
    }
28
29
    /**
30
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
31
     *
32
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
33
     *
34
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
35
     * `key=value` and concatenated by `;`. For example:
36
     *
37
     * ```php
38
     * $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306', ['charset' => 'utf8mb4']);
39
     * $pdoDriver = new PDODriver($dsn->asString(), 'username', 'password');
40
     * $connection = new Connection($pdoDriver, $queryCache, $schemaCache);
41
     * ```
42
     *
43
     * Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306;charset=utf8mb4`.
44
     */
45
    public function asString(): string
46
    {
47
        $dsn = "$this->driver:" . "host=$this->host" . ';' . "dbname=$this->databaseName";
48
49
        if ($this->port !== null) {
50
            $dsn .= ';' . "port=$this->port";
51
        }
52
53
        $parts = [];
54
55
        foreach ($this->options as $key => $value) {
56
            $parts[] = "$key=$value";
57
        }
58
59
        if (!empty($parts)) {
60
            $dsn .= ';' . implode(';', $parts);
61
        }
62
63
        return $dsn;
64
    }
65
66
    /**
67
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
68
     */
69
    public function __toString(): string
70
    {
71
        return $this->asString();
72
    }
73
74
    /**
75
     * @return string The database name to connect to.
76
     */
77
    public function getDatabaseName(): string
78
    {
79
        return $this->databaseName;
80
    }
81
82
    /**
83
     * @return string The database driver name.
84
     */
85
    public function getDriver(): string
86
    {
87
        return $this->driver;
88
    }
89
90
    /**
91
     * @return string The database host name or IP address.
92
     */
93
    public function getHost(): string
94
    {
95
        return $this->host;
96
    }
97
98
    /**
99
     * @return array The database connection options. Default value to an empty array.
100
     */
101
    public function getOptions(): array
102
    {
103
        return $this->options;
104
    }
105
106
    /**
107
     * @return string|null The database port. Null if not set.
108
     */
109
    public function getPort(): string|null
110
    {
111
        return $this->port;
112
    }
113
}
114