1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nyholm\Dsn\Configuration; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* A function with one or more arguments. The default function is called "dsn". |
9
|
|
|
* Other function may be "failover" or "roundrobin". |
10
|
|
|
* |
11
|
|
|
* Examples: |
12
|
|
|
* - failover(redis://localhost memcached://example.com) |
13
|
|
|
* - dsn(amqp://guest:password@localhost:1234) |
14
|
|
|
* - foobar(amqp://guest:password@localhost:1234 amqp://localhost)?delay=10 |
15
|
|
|
* |
16
|
|
|
* @author Tobias Nyholm <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class DsnFunction |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $name; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $arguments; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $parameters; |
34
|
|
|
|
35
|
|
|
public function __construct(string $name, array $arguments, array $parameters = []) |
36
|
|
|
{ |
37
|
|
|
$this->name = $name; |
38
|
|
|
$this->arguments = $arguments; |
39
|
|
|
$this->parameters = $parameters; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getName(): string |
43
|
|
|
{ |
44
|
|
|
return $this->name; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array<DsnFunction|Dsn> |
49
|
|
|
*/ |
50
|
|
|
public function getArguments(): array |
51
|
|
|
{ |
52
|
|
|
return $this->arguments; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getParameters(): array |
56
|
|
|
{ |
57
|
|
|
return $this->parameters; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getParameter(string $key, $default = null) |
61
|
|
|
{ |
62
|
|
|
return \array_key_exists($key, $this->parameters) ? $this->parameters[$key] : $default; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param mixed $value |
67
|
|
|
*/ |
68
|
|
|
public function withParameter(string $key, $value): self |
69
|
|
|
{ |
70
|
|
|
$new = clone $this; |
71
|
|
|
$new->parameters[$key] = $value; |
72
|
|
|
|
73
|
|
|
return $new; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function withoutParameter(string $key): self |
77
|
|
|
{ |
78
|
|
|
$new = clone $this; |
79
|
|
|
unset($new->parameters[$key]); |
80
|
|
|
|
81
|
|
|
return $new; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return DsnFunction|Dsn |
86
|
|
|
*/ |
87
|
|
|
public function first() |
88
|
|
|
{ |
89
|
|
|
return reset($this->arguments); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function __toString() |
96
|
|
|
{ |
97
|
|
|
return sprintf('%s(%s)%s', $this->getName(), implode(' ', $this->getArguments()), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters)); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|