Passed
Push — master ( 07b934...bffbb7 )
by 世昌
02:21
created

MySQLConnector::getDsn()   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
namespace nebula\component\database\connector;
3
4
use nebula\component\database\exception\ConfigException;
5
6
/**
7
 * 连接器
8
 */
9
class MySQLConnector extends Connector
10
{
11
    protected static $type = 'mysql';
12
13
    protected $defaultConfig = [
14
        'database-prefix' => '',
15
        'database-name' => 'nebula',
16
        'database-user' => 'root',
17
        'database-password' => '',
18
        'server-host' => '127.0.0.1',
19
        'server-port' => 3306,
20
        'database-charset' => 'utf8mb4',
21
    ];
22
23
    /**
24
     * 应用配置
25
     *
26
     * @param array $config
27
     * @return void
28
     */
29
    public function applyConfig(array $config)
30
    {
31
        foreach ($config as $name => $value) {
32
            if (in_array($name, array_keys($defaultConfig))) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $defaultConfig seems to be never defined.
Loading history...
33
                $this->config[$name] = $config[$name] ?? $this->defaultConfig[$name];
34
            } else {
35
                throw new ConfigException(sprintf(__CLASS__.' : unknown config property %s', $name));
36
            }
37
        }
38
    }
39
40
    /**
41
     * 获取PDO链接描述
42
     *
43
     * @return string
44
     */
45
    public function getDsn():string
46
    {
47
        return 'mysql:host='.$this->config['server-host'].';charset='.$this->config['database-charset'].';port='.$this->config['server-port'];
48
    }
49
50
    
51
}
52