Passed
Branch dev (f56f10)
by Wilmer
04:41 queued 01:34
created

PDODriver::getUsername()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Db\Driver;
6
7
use PDO;
8
9
final class PDODriver implements DriverInterface
10
{
11
    private ?string $charset = null;
12
    private ?PDO $pdo = null;
13
14
    public function __construct(
15
        private string $dsn,
16
        private string $username = '',
17
        private string $password = '',
18
        private array $attributes = []
19
    ) {
20
    }
21
22
    /**
23
     * PDO attributes (name => value) that should be set when calling {@see open()} to establish a DB connection.
24
     * Please refer to the [PHP manual](http://php.net/manual/en/pdo.setattribute.php) for details about available
25
     * attributes.
26
     *
27
     * @param array $attributes the attributes (name => value) to be set on the DB connection.
28
     */
29
    public function attributes(array $attributes): void
30
    {
31
        $this->attributes = $attributes;
32
    }
33
34
    public function createConnection(): PDO
35
    {
36
        return new PDO($this->dsn, $this->username, $this->password, $this->attributes);
37
    }
38
39
    /**
40
     * The charset used for database connection. The property is only used for MySQL, PostgresSQL databases. Defaults to
41
     * null, meaning using default charset as configured by the database.
42
     *
43
     * For Oracle Database, the charset must be specified in the {@see dsn}, for example for UTF-8 by appending
44
     * `;charset=UTF-8` to the DSN string.
45
     *
46
     * The same applies for if you're using GBK or BIG5 charset with MySQL, then it's highly recommended specifying
47
     * charset via {@see dsn} like `'mysql:dbname=mydatabase;host=127.0.0.1;charset=GBK;'`.
48
     *
49
     * @param string|null $charset
50
     */
51
    public function charset(?string $charset): void
52
    {
53
        $this->charset = $charset;
54
    }
55
56
    /**
57
     * Returns the charset currently used for database connection. The returned charset is only applicable for MySQL,
58
     * PostgresSQL databases.
59
     *
60
     * @return string|null the charset of the pdo instance. Null is returned if the charset is not set yet or not
61
     * supported by the pdo driver
62
     */
63
    public function getCharset(): ?string
64
    {
65
        return $this->charset;
66
    }
67
68
    /**
69
     * Return dsn string for current driver.
70
     */
71
    public function getDsn(): string
72
    {
73
        return $this->dsn;
74
    }
75
76
    /**
77
     * Returns the password for establishing DB connection.
78
     *
79
     * @return string the password for establishing DB connection.
80
     */
81
    public function getPassword(): string
82
    {
83
        return $this->password;
84
    }
85
86
    /**
87
     * Returns the PDO instance.
88
     *
89
     * @return PDO|null the PDO instance, null if the connection is not established yet.
90
     */
91
    public function getPDO(): ?PDO
92
    {
93
        return $this->pdo;
94
    }
95
96
    /**
97
     * Returns the username for establishing DB connection.
98
     *
99
     * @return string the username for establishing DB connection.
100
     */
101
    public function getUsername(): string
102
    {
103
        return $this->username;
104
    }
105
106
    /**
107
     * The password for establishing DB connection. Defaults to `null` meaning no password to use.
108
     *
109
     * @param string $password the password for establishing DB connection.
110
     */
111
    public function password(string $password): void
112
    {
113
        $this->password = $password;
114
    }
115
116
    /**
117
     * The username for establishing DB connection. Defaults to `null` meaning no username to use.
118
     *
119
     * @param string $username the username for establishing DB connection.
120
     */
121
    public function username(string $username): void
122
    {
123
        $this->username = $username;
124
    }
125
}
126