1 | <?php |
||
2 | |||
3 | /** |
||
4 | * This file is part of Cycle Database package. |
||
5 | * |
||
6 | * For the full copyright and license information, please view the LICENSE |
||
7 | * file that was distributed with this source code. |
||
8 | */ |
||
9 | |||
10 | declare(strict_types=1); |
||
11 | |||
12 | namespace Cycle\Database\Config\MySQL; |
||
13 | |||
14 | use Cycle\Database\Config\ProvidesSourceString; |
||
15 | |||
16 | class TcpConnectionConfig extends ConnectionConfig implements ProvidesSourceString |
||
17 | { |
||
18 | /** |
||
19 | * @var positive-int |
||
0 ignored issues
–
show
Documentation
Bug
introduced
by
![]() |
|||
20 | */ |
||
21 | public int $port; |
||
22 | |||
23 | /** |
||
24 | * @param non-empty-string $host |
||
0 ignored issues
–
show
|
|||
25 | * @param numeric-string|positive-int $port |
||
26 | * @param non-empty-string $database |
||
27 | 4 | * @param non-empty-string|null $charset |
|
28 | * @param non-empty-string|null $user |
||
29 | * @param non-empty-string|null $password |
||
30 | * @param array<int, non-empty-string|non-empty-string> $options |
||
31 | */ |
||
32 | public function __construct( |
||
33 | public string $database, |
||
34 | public string $host = 'localhost', |
||
35 | int|string $port = 3307, |
||
36 | 4 | public ?string $charset = null, |
|
37 | 4 | ?string $user = null, |
|
38 | ?string $password = null, |
||
39 | array $options = [], |
||
40 | ) { |
||
41 | $this->port = (int) $port; |
||
42 | 484 | ||
43 | parent::__construct($user, $password, $options); |
||
44 | 484 | } |
|
45 | |||
46 | public function getSourceString(): string |
||
47 | { |
||
48 | return $this->database; |
||
49 | } |
||
50 | |||
51 | /** |
||
52 | * Returns the MySQL-specific PDO DataSourceName with connection URI, |
||
53 | * that looks like: |
||
54 | * <code> |
||
55 | * mysql:host=localhost;port=3307;dbname=dbname |
||
56 | 8 | * </code> |
|
57 | * |
||
58 | 8 | * {@inheritDoc} |
|
59 | 8 | */ |
|
60 | 8 | public function getDsn(): string |
|
61 | 8 | { |
|
62 | 8 | $config = [ |
|
63 | 'host' => $this->host, |
||
64 | 'port' => $this->port, |
||
65 | 8 | 'dbname' => $this->database, |
|
66 | 'charset' => $this->charset, |
||
67 | ]; |
||
68 | |||
69 | return \sprintf('%s:%s', $this->getName(), $this->dsn($config)); |
||
70 | } |
||
71 | } |
||
72 |