DSN::getCharset()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace ExileeD\PDOParseDSN;
4
5
class DSN
6
{
7
8
    /**
9
     * @var string
10
     */
11
    private $dsn;
12
13
    /**
14
     * Mysql, pgsql
15
     *
16
     * @var string
17
     */
18
    private $protocol;
19
20
    /**
21
     * @var array
22
     */
23
    private $parameters = [];
24
25
    /**
26
     * Constructor.
27
     *
28
     * @param string $dsn
29
     *
30
     * @throws \LogicException
31
     */
32 21
    public function __construct($dsn)
33
    {
34 21
        $this->dsn = $dsn;
35 21
        $this->parseDsn($dsn);
36 12
    }
37
38
    /**
39
     * @param string $dsn
40
     *
41
     * @throws \LogicException
42
     */
43 21
    private function parseDsn($dsn)
44
    {
45 21
        $dsn = trim($dsn);
46
47 21
        if (strpos($dsn, ':') === false) {
48 6
            throw new \LogicException(sprintf('The DSN is invalid. It does not have scheme separator ":".'));
49
        }
50
51 15
        list($prefix, $dsnWithoutPrefix) = preg_split('#\s*:\s*#', $dsn, 2);
52
53 15
        $this->protocol = $prefix;
54
55 15
        if (preg_match('/^[a-z\d]+$/', strtolower($prefix)) == false) {
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing preg_match('/^[a-z\d]+$/', strtolower($prefix)) of type integer to the boolean false. If you are specifically checking for 0, consider using something more explicit like === 0 instead.
Loading history...
56 3
            throw new \LogicException('The DSN is invalid. Prefix contains illegal symbols.');
57
        }
58
59 12
        $dsnElements = preg_split('#\s*\;\s*#', $dsnWithoutPrefix);
60
61 12
        $elements = [];
62 12
        foreach ($dsnElements as $element) {
63 12
            if (strpos($dsnWithoutPrefix, '=') !== false) {
64 12
                list($key, $value) = preg_split('#\s*=\s*#', $element, 2);
65 12
                $elements[$key] = $value;
66
            } else {
67
                $elements = [
68 4
                    $dsnWithoutPrefix,
69
                ];
70
            }
71
        }
72 12
        $this->parameters = $elements;
73 12
    }
74
75
    /**
76
     * @return string
77
     */
78 3
    public function getDsn()
79
    {
80 3
        return $this->dsn;
81
    }
82
83
    /**
84
     * @return string|null
85
     */
86 9
    public function getProtocol()
87
    {
88 9
        return $this->protocol;
89
    }
90
91
    /**
92
     * @return string|null
93
     */
94 9
    public function getDatabase()
95
    {
96 9
        return $this->getAttribute('dbname');
97
    }
98
99
    /**
100
     * @return string
101
     */
102 9
    public function getHost()
103
    {
104 9
        return $this->getAttribute('host');
105
    }
106
107
    /**
108
     * @return string
109
     */
110 9
    public function getPort()
111
    {
112 9
        return $this->getAttribute('port');
113
    }
114
115
    /**
116
     * @return string
117
     */
118 3
    public function getCharset()
119
    {
120 3
        return $this->getAttribute('charset');
121
    }
122
123
    /**
124
     * Get an attribute from the $attributes array.
125
     *
126
     * @param string $key
127
     *
128
     * @return mixed
129
     */
130 9
    private function getAttribute($key)
131
    {
132 9
        if (isset($this->parameters[$key])) {
133 9
            return $this->parameters[$key];
134
        }
135
136 6
        return null;
137
    }
138
139
    /**
140
     * @return array
141
     */
142 2
    public function getParameters()
143
    {
144 2
        return $this->parameters;
145
    }
146
}
147