|
4 | 4 |
|
5 | 5 | namespace Yiisoft\Db\Mssql; |
6 | 6 |
|
7 | | -use Yiisoft\Db\Connection\AbstractDsn; |
| 7 | +use Stringable; |
8 | 8 |
|
9 | 9 | /** |
10 | | - * Implement a Data Source Name (DSN) for an MSSQL Server. |
| 10 | + * Represents a Data Source Name (DSN) for a MSSQL Server that's used to configure a {@see Driver} instance. |
| 11 | + * |
| 12 | + * To get DSN in string format, use the `(string)` type casting operator. |
11 | 13 | * |
12 | 14 | * @link https://www.php.net/manual/en/ref.pdo-sqlsrv.connection.php |
13 | 15 | */ |
14 | | -final class Dsn extends AbstractDsn |
| 16 | +final class Dsn implements Stringable |
15 | 17 | { |
16 | 18 | /** |
| 19 | + * @param string $driver The database driver name. |
| 20 | + * @param string $host The database host name or IP address. |
| 21 | + * @param string $databaseName The database name to connect to. Empty string if isn't set. |
| 22 | + * @param string $port The database port. Empty string if isn't set. |
| 23 | + * @param string[] $options The database connection options. Default value to an empty array. |
| 24 | + * |
17 | 25 | * @psalm-param array<string,string> $options |
18 | 26 | */ |
19 | 27 | public function __construct( |
20 | | - string $driver = 'sqlsrv', |
21 | | - string $host = '127.0.0.1', |
22 | | - string|null $databaseName = null, |
23 | | - string|null $port = '1433', |
24 | | - array $options = [] |
| 28 | + public readonly string $driver = 'sqlsrv', |
| 29 | + public readonly string $host = '127.0.0.1', |
| 30 | + public readonly string $databaseName = '', |
| 31 | + public readonly string $port = '1433', |
| 32 | + public readonly array $options = [], |
25 | 33 | ) { |
26 | | - parent::__construct($driver, $host, $databaseName, $port, $options); |
27 | 34 | } |
28 | 35 |
|
29 | 36 | /** |
30 | 37 | * @return string the Data Source Name, or DSN, has the information required to connect to the database. |
31 | 38 | * Please refer to the [PHP manual](https://php.net/manual/en/pdo.construct.php) on the format of the DSN string. |
32 | 39 | * |
33 | | - * The `driver` array key is used as the driver prefix of the DSN, all further key-value pairs are rendered as |
34 | | - * `key=value` and concatenated by `;`. For example: |
| 40 | + * The `driver` property is used as the driver prefix of the DSN. For example: |
35 | 41 | * |
36 | 42 | * ```php |
37 | 43 | * $dsn = new Dsn('sqlsrv', 'localhost', 'yiitest', '1433'); |
38 | | - * $driver = new Driver($dsn->asString(), 'username', 'password'); |
| 44 | + * $driver = new Driver($dsn, 'username', 'password'); |
39 | 45 | * $db = new Connection($driver, $schemaCache); |
40 | 46 | * ``` |
41 | 47 | * |
42 | 48 | * Will result in the DSN string `sqlsrv:Server=localhost,1433;Database=yiitest`. |
43 | 49 | */ |
44 | | - public function asString(): string |
| 50 | + public function __toString(): string |
45 | 51 | { |
46 | | - $driver = $this->getDriver(); |
47 | | - $host = $this->getHost(); |
48 | | - $port = $this->getPort(); |
49 | | - $databaseName = $this->getDatabaseName(); |
50 | | - $options = $this->getOptions(); |
51 | | - |
52 | | - $dsn = "$driver:Server=$host"; |
| 52 | + $dsn = "$this->driver:Server=$this->host"; |
53 | 53 |
|
54 | | - if (!empty($port)) { |
55 | | - $dsn .= ",$port"; |
| 54 | + if ($this->port !== '') { |
| 55 | + $dsn .= ",$this->port"; |
56 | 56 | } |
57 | 57 |
|
58 | | - if (!empty($databaseName)) { |
59 | | - $dsn .= ";Database=$databaseName"; |
| 58 | + if ($this->databaseName !== '') { |
| 59 | + $dsn .= ";Database=$this->databaseName"; |
60 | 60 | } |
61 | 61 |
|
62 | | - if (!empty($options)) { |
63 | | - foreach ($options as $key => $value) { |
64 | | - $dsn .= ";$key=$value"; |
65 | | - } |
| 62 | + foreach ($this->options as $key => $value) { |
| 63 | + $dsn .= ";$key=$value"; |
66 | 64 | } |
67 | 65 |
|
68 | 66 | return $dsn; |
|
0 commit comments