Passed
Push — master ( 20361a...82a2cb )
by Tobias
01:15
created

Dsn::withScheme()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Nyholm\Dsn\Configuration;
4
5
/**
6
 * Base DSN object.
7
 *
8
 * Example:
9
 * - null://
10
 * - redis:?host[h1]&host[h2]&host[/foo:]
11
 *
12
 * @author Tobias Nyholm <[email protected]>
13
 */
14
class Dsn
15
{
16
    /**
17
     * @var string|null
18
     */
19
    private $scheme;
20
21
    /**
22
     * @var array
23
     */
24
    private $parameters = [];
25
26
    public function __construct(?string $scheme, array $parameters = [])
27
    {
28
        $this->scheme = $scheme;
29
        $this->parameters = $parameters;
30
    }
31
32
    public function getScheme(): ?string
33
    {
34
        return $this->scheme;
35
    }
36
37
    public function withScheme(?string $scheme): self
38
    {
39
        $new = clone $this;
40
        $new->scheme = $scheme;
41
42
        return $new;
43
    }
44
45
    public function getParameters(): array
46
    {
47
        return $this->parameters;
48
    }
49
50
    /**
51
     * @param mixed|null $default
52
     *
53
     * @return mixed
54
     */
55
    public function getParameter(string $key, $default = null)
56
    {
57
        return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default;
58
    }
59
60
    /**
61
     * @param mixed $value
62
     */
63
    public function withParameter(string $key, $value): self
64
    {
65
        $new = clone $this;
66
        $new->parameters[$key] = $value;
67
68
        return $new;
69
    }
70
71
    public function withoutParameter(string $key): self
72
    {
73
        $new = clone $this;
74
        unset($new->parameters[$key]);
75
76
        return $new;
77
    }
78
79
    public function getHost(): ?string
80
    {
81
        return null;
82
    }
83
84
    public function getPort(): ?int
85
    {
86
        return null;
87
    }
88
89
    public function getPath(): ?string
90
    {
91
        return null;
92
    }
93
94
    public function getUser(): ?string
95
    {
96
        return null;
97
    }
98
99
    public function getPassword(): ?string
100
    {
101
        return null;
102
    }
103
104
    /**
105
     * @var string
106
     */
107
    public function __toString()
108
    {
109
        return sprintf('%s://%s', $this->getScheme(), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters));
110
    }
111
}
112