Url::fromUri()   A
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
c 0
b 0
f 0
ccs 11
cts 11
cp 1
rs 9.7998
cc 4
nc 8
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use Psr\Http\Message\UriInterface;
8
use TechDeCo\ElasticApmAgent\Serialization;
9
10
final class Url implements JsonSerializable
11
{
12
    /**
13
     * @var string|null
14
     */
15
    private $raw;
16
17
    /**
18
     * @var string|null
19
     */
20
    private $protocol;
21
22
    /**
23
     * @var string|null
24
     */
25
    private $full;
26
27
    /**
28
     * @var string|null
29
     */
30
    private $hostname;
31
32
    /**
33
     * @var string|null
34
     */
35
    private $port;
36
37
    /**
38
     * @var string|null
39
     */
40
    private $path;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $query;
46
47
    /**
48
     * @var string|null
49
     */
50
    private $fragment;
51
52 22
    private function __construct()
53
    {
54 22
    }
55
56 22
    public static function fromUri(UriInterface $uri): self
57
    {
58 22
        $me           = new self();
59 22
        $me->raw      = $uri->__toString();
60 22
        $me->protocol = $uri->getScheme();
61 22
        $me->full     = $uri->__toString();
62 22
        $me->hostname = $uri->getHost();
63 22
        $me->port     = $uri->getPort() ? (string) $uri->getPort() : null;
64 22
        $me->path     = $uri->getPath();
65 22
        $me->query    = $uri->getQuery() ?: null;
66 22
        $me->fragment = $uri->getFragment() ?: null;
67
68 22
        return $me;
69
    }
70
71
    /**
72
     * @return mixed[]
73
     */
74 20
    public function jsonSerialize(): array
75
    {
76 20
        return Serialization::filterUnset([
77 20
            'raw' => $this->raw,
78 20
            'protocol' => $this->protocol,
79 20
            'full' => $this->full,
80 20
            'hostname' => $this->hostname,
81 20
            'port' => $this->port,
82 20
            'pathname' => $this->path,
83 20
            'search' => $this->query,
84 20
            'hash' => $this->fragment,
85
        ]);
86
    }
87
}
88