Url::hasPort()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/value-object project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
13
final class Url implements ValueObjectInterface
14
{
15
    private const NIL = null;
16
17
    private Text $fragment;
18
19
    private Text $host;
20
21
    private Text $scheme;
22
23
    private Text $query;
24
25
    private IntValue $port;
26
27
    private Text $path;
28
29
    /** @param null|string $value */
30 9
    public static function fromNative($value): self
31
    {
32 9
        $value = empty($value) ? null : $value;
33 9
        Assertion::nullOrUrl($value, "Trying to create Url VO from unsupported value type: $value");
34 9
        return empty($value) ? new self(self::NIL) : new self($value);
35
    }
36
37 3
    public function toNative(): ?string
38
    {
39 3
        if ($this->host->isEmpty()) {
40 1
            return self::NIL;
41
        }
42 3
        return sprintf(
43 3
            '%s://%s',
44 3
            (string)$this->scheme,
45 3
            implode('', [
46 3
                $this->host,
47 3
                $this->formatPort(),
48 3
                $this->path,
49 3
                $this->prefix('?', $this->query),
50 3
                $this->prefix('#', $this->fragment),
51
            ])
52
        );
53
    }
54
55
    /** @param self $comparator */
56 1
    public function equals($comparator): bool
57
    {
58 1
        Assertion::isInstanceOf($comparator, self::class);
59 1
        return $this->toNative() === $comparator->toNative();
60
    }
61
62 1
    public function __toString(): string
63
    {
64 1
        return $this->toNative() ?? '';
65
    }
66
67 1
    public function getPath(): Text
68
    {
69 1
        return $this->path;
70
    }
71
72 1
    public function getFragment(): Text
73
    {
74 1
        return $this->fragment;
75
    }
76
77 1
    public function getHost(): Text
78
    {
79 1
        return $this->host;
80
    }
81
82 1
    public function getQuery(): Text
83
    {
84 1
        return $this->query;
85
    }
86
87 1
    public function getScheme(): Text
88
    {
89 1
        return $this->scheme;
90
    }
91
92 1
    public function getPort(): ?IntValue
93
    {
94 1
        return $this->port;
95
    }
96
97 3
    public function hasPort(): bool
98
    {
99 3
        return $this->port->toNative() !== null;
100
    }
101
102 9
    private function __construct(?string $url = null)
103
    {
104 9
        if (is_null($url)) {
105 1
            $emptyText = Text::fromNative(null);
106 1
            $this->host = $emptyText;
107 1
            $this->scheme = $emptyText;
108 1
            $this->query = $emptyText;
109 1
            $this->fragment = $emptyText;
110 1
            $this->path = $emptyText;
111 1
            $this->port = IntValue::fromNative(null);
112
        } else {
113 9
            $this->host = $this->parse($url, PHP_URL_HOST);
114 9
            $this->scheme = $this->parse($url, PHP_URL_SCHEME);
115 9
            $this->query = $this->parse($url, PHP_URL_QUERY);
116 9
            $this->fragment = $this->parse($url, PHP_URL_FRAGMENT);
117 9
            $this->path = $this->parse($url, PHP_URL_PATH);
118 9
            $this->port = $this->parsePort($url);
119
        }
120 9
    }
121
122 9
    private function parse(string $url, int $urlPart): Text
123
    {
124 9
        return Text::fromNative(parse_url($url, $urlPart) ?: self::NIL);
125
    }
126
127 9
    private function parsePort(string $url): IntValue
128
    {
129 9
        $port = parse_url($url, PHP_URL_PORT);
130 9
        return IntValue::fromNative($port ?? null);
131
    }
132
133 3
    private function prefix(string $prefix, Text $value): string
134
    {
135 3
        return $value->isEmpty() ? '' : $prefix . $value;
136
    }
137
138 3
    private function formatPort(): string
139
    {
140 3
        return $this->hasPort() ? ':' . $this->port : '';
141
    }
142
}
143