8000 Adding "toString" functions · symfony/symfony@6949081 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6949081

Browse files
committed
Adding "toString" functions
1 parent 4e705f1 commit 6949081

File tree

6 files changed

+69
-4
lines changed

6 files changed

+69
-4
lines changed

src/Symfony/Component/Dsn/Configuration/Dsn.php

+8
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,12 @@ public function getPassword(): ?string
7676
{
7777
return null;
7878
}
79+
80+
/**
81+
* @var string
82+
*/
83+
public function __toString()
84+
{
85+
return sprintf('%s://%s', $this->getScheme(), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters));
86+
}
7987
}

src/Symfony/Component/Dsn/Configuration/DsnFunction.php

+8
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,12 @@ public function first()
7575
{
7676
return reset($this->arguments);
7777
}
78+
79+
/**
80+
* @return string
81+
*/
82+
public function __toString()
83+
{
84+
return sprintf('%s(%s)%s', $this->getName(), implode(' ', $this->getArguments()), empty($this->parameters) ? '' : '?'.http_build_query($this->parameters));
85+
}
7886
}

src/Symfony/Component/Dsn/Configuration/Path.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,34 @@ class Path extends Dsn
3030
*/
3131
private $path;
3232

33-
public function __construct(?string $scheme, string $path, array $parameters = [], array $authentication = [])
33+
public function __construct(string $scheme, string $path, array $parameters = [], array $authentication = [])
3434
{
3535
$this->path = $path;
3636
$this->setAuthentication($authentication);
3737
parent::__construct($scheme, $parameters);
3838
}
3939

40+
public function getScheme(): string
41+
{
42+
return parent::getScheme();
43+
}
44+
4045
public function getPath(): string
4146
{
4247
return $this->path;
4348
}
49+
50+
/**
51+
* @var string
52+
*/
53+
public function __toString()
54+
{
55+
$parameters = $this->getParameters();
56+
57+
return
58+
$this->getScheme().'://'.
59+
$this->getUserInfoString().
60+
$this->getPath().
61+
(empty($parameters) ? '' : '?'.http_build_query($parameters));
62+
}
4463
}

src/Symfony/Component/Dsn/Configuration/Url.php

+19
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
*
1919
* Example:
2020
* - memcached://user:password@127.0.0.1?weight=50
21+
* - 127.0.0.1:80
22+
* - amqp://127.0.0.1/%2f/messages
2123
*
2224
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
2325
*/
@@ -63,4 +65,21 @@ public function getPath(): ?string
6365
{
6466
return $this->path;
6567
}
68+
69+
/**
70+
* @var string
71+
*/
72+
public function __toString()
73+
{
74+
$parameters = $this->getParameters();
75+
$scheme = $this->getScheme();
76+
77+
return
78+
(empty($scheme) ? '' : $scheme.'://').
79+
$this->getUserInfoString().
80+
$this->getHost().
81+
(empty($this->port) ? '' : ':'.$this->port).
82+
($this->getPath() ?? '').
83+
(empty($parameters) ? '' : '?'.http_build_query($parameters));
84+
}
6685
}

src/Symfony/Component/Dsn/Configuration/UserPasswordTrait.php

+12
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,16 @@ public function getPassword(): ?string
4747
{
4848
return $this->authentication['password'] ?? null;
4949
}
50+
51+
private function getUserInfoString(): string
52+
{
53+
$user = $this->getUser() ?? '';
54+
$password = $this->getPassword() ?? '';
55+
$userInfo = $user.(empty($password) ? '' : ':'.$password).'@';
56+
if (\strlen($userInfo) <= 2) {
57+
$userInfo = '';
58+
}
59+
60+
return $userInfo;
61+
}
5062
}

src/Symfony/Component/Mailer/Transport.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
3636
use Symfony\Contracts\HttpClient\HttpClientInterface;
3737

38-
3938
/**
4039
* @author Fabien Potencier <fabien@symfony.com>
4140
* @author Konstantin Myakshin <molodchick@gmail.com>
@@ -90,7 +89,7 @@ public function fromString(string $dsn): TransportInterface
9089
return self::fromDsnComponent(DsnParser::parse($dsn));
9190
}
9291

93-
private function fromDsnComponent($dsn) : TransportInterface
92+
private function fromDsnComponent($dsn): TransportInterface
9493
{
9594
static $keywords = [
9695
'failover' => FailoverTransport::class,
@@ -102,7 +101,7 @@ private function fromDsnComponent($dsn) : TransportInterface
102101
}
103102

104103
if (!$dsn instanceof DsnFunction) {
105-
throw new \InvalidArgumentException(\sprintf('First argument to Transport::fromDsnComponent() must be a "%s" or %s', DsnFunction::class, Dsn::class));
104+
throw new \InvalidArgumentException(sprintf('First argument to Transport::fromDsnComponent() must be a "%s" or "%s".', DsnFunction::class, Dsn::class));
106105
}
107106

108107
if (!isset($keywords[$dsn->getName()])) {

0 commit comments

Comments
 (0)
0