Dsn::asString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 5
c 1
b 1
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Sqlite;
6
7
use Yiisoft\Db\Connection\AbstractDsn;
8
9
/**
10
 * Implement a Data Source Name (DSN) for an SQLite Server.
11
 *
12
 * @link https://www.php.net/manual/en/ref.pdo-sqlite.connection.php
13
 */
14
final class Dsn extends AbstractDsn
15
{
16
    /**
17
     * @psalm-param string[] $options
18
     */
19 701
    public function __construct(private string $driver, private string|null $databaseName = null)
20
    {
21 701
        parent::__construct($driver, '', $databaseName);
22
    }
23
24
    /**
25
     * @return string The Data Source Name, or DSN, has the information required to connect to the database.
26
     *
27
     * Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string.
28
     *
29
     * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as
30
     * `key=value` and concatenated by `;`. For example:
31
     *
32
     * ```php
33
     * $dsn = new Dsn('sqlite', __DIR__ . '/data/test.sq3');
34
     * $driver = new Driver($dsn->asString());
35
     * $db = new Connection($driver, $schemaCache);
36
     * ```
37
     *
38
     * Will result in the DSN string `sqlite:/path/to/data/test.sq3`.
39
     */
40 701
    public function asString(): string
41
    {
42 701
        return match ($this->databaseName) {
43 701
            '' => $this->driver . ':',
44 701
            'memory' => $this->driver . '::memory:',
45 701
            null => $this->driver . ':',
46 701
            default => $this->driver . ':' . $this->databaseName,
47 701
        };
48
    }
49
}
50