Completed
Pull Request — master (#37)
by Eugene
09:24
created

Dsn::getString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Tarantool Client package.
7
 *
8
 * (c) Eugene Leonovich <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Tarantool\Client;
15
16
final class Dsn
17
{
18
    private const DEFAULT_TCP_PORT = '3301';
19
20
    private $connectionUri;
21
    private $username;
22
    private $password;
23
    private $options;
24
25
    public static function parse(string $dsn) : self
26
    {
27
        $isSocket = 0 === \strpos($dsn, 'unix://');
28
29
        if ($isSocket && $pos = \strpos($dsn, '/', 7)) {
30
            $dsn = \substr_replace($dsn, '.', $pos, 0);
31
        }
32
33
        if (false === $parts = \parse_url($dsn)) {
34
            throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn));
35
        }
36
37
        $self = new self();
38
39
        if ($isSocket) {
40
            if ('' === \trim($parts['path'], '/')) {
41
                throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn));
42
            }
43
            $self->connectionUri = 'unix://'.$parts['path'];
44
        } else {
45
            if (!isset($parts['scheme'], $parts['host'])) {
46
                throw new \InvalidArgumentException(\sprintf('Malformed DSN "%s".', $dsn));
47
            }
48
            $self->connectionUri = $parts['scheme'].'://'.$parts['host'].':'.($parts['port'] ?? self::DEFAULT_TCP_PORT);
49
        }
50
51
        if (isset($parts['user'])) {
52
            $self->username  = $parts['user'];
53
            $self->password = $parts['pass'] ?? '';
54
        }
55
56
        if (isset($parts['query'])) {
57
            \parse_str($parts['query'], $self->options);
58
        } else {
59
            $self->options = [];
60
        }
61
62
        return $self;
63
    }
64
65
    public function getConnectionUri() : string
66
    {
67
        return $this->connectionUri;
68
    }
69
70
    public function getUsername() : ?string
71
    {
72
        return $this->username;
73
    }
74
75
    public function getPassword() : ?string
76
    {
77
        return $this->password;
78
    }
79
80
    public function getString(string $name, ?string $default = null) : ?string
81
    {
82
        return $this->options[$name] ?? $default;
83
    }
84
85
    public function getBool(string $name, ?bool $default = null) : ?bool
86
    {
87
        if (!isset($this->options[$name])) {
88
            return $default;
89
        }
90
91
        if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_BOOLEAN, \FILTER_NULL_ON_FAILURE)) {
92
            throw new \InvalidArgumentException($name);
93
        }
94
95
        return $value;
96
    }
97
98
    public function getInt(string $name, ?int $default = null) : ?int
99
    {
100
        if (!isset($this->options[$name])) {
101
            return $default;
102
        }
103
104
        if (null === $value = \filter_var($this->options[$name], \FILTER_VALIDATE_INT, \FILTER_NULL_ON_FAILURE)) {
105
            throw new \InvalidArgumentException($name);
106
        }
107
108
        return $value;
109
    }
110
}
111