Completed
Push — master ( 54e54a...70f940 )
by Mr
02:22
created

Pgsql::genDsn()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
nc 3
nop 1
1
<?php
2
3
namespace DrMVC\Database\Drivers;
4
5
class Pgsql extends SQL
6
{
7
    const DEFAULT_HOST = '127.0.0.1';
8
    const DEFAULT_PORT = '5432';
9
10
    /**
11
     * @link https://secure.php.net/manual/en/ref.pdo-pgsql.connection.php
12
     * @link https://www.postgresql.org/docs/8.3/static/libpq-connect.html
13
     *
14
     * The PDO_PGSQL Data Source Name (DSN) is composed of the following elements:
15
     */
16
    const AVAILABLE_ELEMENTS = [
17
        'host',
18
        'hostaddr',
19
        'port',
20
        'dbname',
21
22
        /**
23
         * These two parameters below is not needed, because we can put user/pass
24
         * as parameters of PDO class, you can read more by link
25
         * @link https://secure.php.net/manual/en/pdo.construct.php
26
         */
27
        //'user',
28
        //'password',
29
30
        'passfile',
31
        'connect_timeout',
32
        'client_encoding',
33
        'options',
34
        'application_name',
35
        'fallback_application_name',
36
        'keepalives',
37
        'keepalives_idle',
38
        'keepalives_interval',
39
        'keepalives_count',
40
        'tty',
41
        'sslmode',
42
        'requiressl',
43
        'sslcompression',
44
        'sslcert',
45
        'sslkey',
46
        'sslrootcert',
47
        'sslcrl',
48
        'requirepeer',
49
        'krbsrvname',
50
        'gsslib',
51
        'service',
52
        'target_session_attrs'
53
    ];
54
55
    /**
56
     * Generate DSN by parameters in config
57
     *
58
     * @param   array $config
59
     * @return  string
60
     */
61
    public function genDsn($config): string
62
    {
63
        // Parse config
64
        $dsn = '';
65
        foreach ($config as $key => $value) {
66
            if (\in_array($key, self::AVAILABLE_ELEMENTS, false)) {
67
                $dsn .= "$key=$value;";
68
            }
69
        }
70
        return $dsn;
71
    }
72
}
73