Passed
Pull Request — master (#442)
by Wilmer
02:36
created

Dsn::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
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 956
final class Dsn implements Stringable
16
{
17 956
    /**
18 956
     * @psalm-param string[] $options
19 956
     */
20 956
    public function __construct(
21 956
        private string $driver,
22 956
        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 956
     * $dsn = new Dsn('mysql', '127.0.0.1', 'yiitest', '3306');
39
     * $connection = new Connection($this->cache, $this->logger, $this->profiler, $dsn->getDsn());
40 956
     * ```
41
     *
42 956
     * Will result in the DSN string `mysql:host=127.0.0.1;dbname=yiitest;port=3306`.
43 956
     */
44
    public function asString(): string
45
    {
46 956
        $dsn = "$this->driver:" . "host=$this->host" . ';' . "dbname=$this->databaseName";
47
48 956
        if ($this->port !== null) {
49
            $dsn .= ';' . "port=$this->port";
50
        }
51
52 956
        $parts = [];
53
54
        foreach ($this->options as $key => $value) {
55
            $parts[] = "$key=$value";
56 956
        }
57
58
        if (!empty($parts)) {
59
            $dsn .= ';' . implode(';', $parts);
60
        }
61
62
        return $dsn;
63
    }
64
65
    /**
66
     * @return string The Data Source Name, or DSN, contains the information required to connect to the database.
67
     */
68
    public function __toString(): string
69
    {
70
        return $this->asString();
71
    }
72
73
    /**
74
     * @return string The database name to connect to.
75
     */
76
    public function getDatabaseName(): string
77
    {
78
        return $this->databaseName;
79
    }
80
81
    /**
82
     * @return string The database driver name.
83
     */
84
    public function getDriver(): string
85
    {
86
        return $this->driver;
87
    }
88
89
    /**
90
     * @return string The database host name or IP address.
91
     */
92
    public function getHost(): string
93
    {
94
        return $this->host;
95
    }
96
97
    /**
98
     * @return array The database connection options. Default value to an empty array.
99
     */
100
    public function getOptions(): array
101
    {
102
        return $this->options;
103
    }
104
105
    /**
106
     * @return string|null The database port. Null if not set.
107
     */
108
    public function getPort(): string|null
109
    {
110
        return $this->port;
111
    }
112
}
113