Issues (5)

src/Configuration/Url.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nyholm\Dsn\Configuration;
6
7
/**
8
 * A "URL like" DSN string.
9
 *
10
 * Example:
11
 * - memcached://user:[email protected]?weight=50
12
 * - 127.0.0.1:80
13
 * - amqp://127.0.0.1/%2f/messages
14
 *
15
 * @author Tobias Nyholm <[email protected]>
16
 */
17
class Url extends Dsn
18
{
19
    use UserPasswordTrait;
20
21
    /**
22
     * @var string
23
     */
24
    private $host;
25
26
    /**
27
     * @var int|null
28
     */
29
    private $port;
30
31
    /**
32
     * @var string|null
33
     */
34
    private $path;
35
36
    /**
37
     * @param array{ user?: string|null, password?: string|null, } $authentication
0 ignored issues
show
Documentation Bug introduced by
The doc comment user?: string|null, password?: string|null, } at position 0 could not be parsed: Unknown type name 'user?' at position 0 in user?: string|null, password?: string|null, }.
Loading history...
38
     */
39
    public function __construct(?string $scheme, string $host, ?int $port = null, ?string $path = null, array $parameters = [], array $authentication = [])
40
    {
41
        $this->host = $host;
42
        $this->port = $port;
43
        $this->path = $path;
44
        $this->setAuthentication($authentication);
45
        parent::__construct($scheme, $parameters);
46
    }
47
48
    public function getHost(): string
49
    {
50
        return $this->host;
51
    }
52
53
    /**
54
     * @return static
55
     */
56
    public function withHost(string $host)
57
    {
58
        $new = clone $this;
59
        $new->host = $host;
60
61
        return $new;
62
    }
63
64
    public function getPort(): ?int
65
    {
66
        return $this->port;
67
    }
68
69
    /**
70
     * @return static
71
     */
72
    public function withPort(?int $port)
73
    {
74
        $new = clone $this;
75
        $new->port = $port;
76
77
        return $new;
78
    }
79
80
    public function getPath(): ?string
81
    {
82
        return $this->path;
83
    }
84
85
    /**
86
     * @return static
87
     */
88
    public function withPath(?string $path)
89
    {
90
        $new = clone $this;
91
        $new->path = $path;
92
93
        return $new;
94
    }
95
96
    /**
97
     * @return string
98
     */
99
    public function __toString()
100
    {
101
        $parameters = $this->getParameters();
102
        $scheme = $this->getScheme();
103
104
        return
105
            (empty($scheme) ? '' : $scheme.'://').
106
            $this->getUserInfoString().
107
            $this->getHost().
108
            (empty($this->port) ? '' : ':'.$this->port).
109
            ($this->getPath() ?? '').
110
            (empty($parameters) ? '' : '?'.http_build_query($parameters));
111
    }
112
}
113