Completed
Pull Request — master (#1)
by Ben
02:04
created

ParsedUrl::query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace Thinktomorrow\Url;
4
5
use Thinktomorrow\Url\Exceptions\InvalidUrl;
6
7
class ParsedUrl
8
{
9
    /** @var Root */
10
    private $root;
11
12
    /** @var null|string */
13
    private $path;
14
15
    /** @var null|string */
16
    private $query;
17
18
    /** @var null|string */
19
    private $hash;
20
21
    public function __construct(Root $root, ?string $path = null, ?string $query = null, ?string $hash = null)
22
    {
23
        $this->root = $root;
24
        $this->path = $path;
25
        $this->query = $query;
26
        $this->hash = $hash;
27
    }
28
29
    public static function fromString(string $url)
30
    {
31
        return new static(...array_values(static::parse($url)));
0 ignored issues
show
Bug introduced by
array_values(static::parse($url)) is expanded, but the parameter $root of Thinktomorrow\Url\ParsedUrl::__construct() does not expect variable arguments. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

31
        return new static(/** @scrutinizer ignore-type */ ...array_values(static::parse($url)));
Loading history...
32
    }
33
34
    public function get(): string
35
    {
36
        return  $this->root->get() .
37
            ($this->hasPath() ? '/' . $this->path() : '') .
38
            ($this->hasQuery() ? '?' . $this->query() : '') .
39
            ($this->hasHash() ? '#' . $this->hash() : '');
40
    }
41
42
    private static function parse(string $url): array
43
    {
44
        // Specific case where we accept double slashes and convert it to a relative url.
45
        // This would otherwise not be able to be parsed.
46
        if ($url == '//') {
47
            $url = '/';
48
        }
49
50
        $parsed = parse_url($url);
51
52
        if (false === $parsed) {
53
            throw new InvalidUrl('Failed to parse url. Invalid url ['.$url.'] passed as parameter.');
54
        }
55
56
        $root = Root::fromString($url)->defaultScheme(null);
57
58
        return [
59
            'root' => $root,
60
            // Check if path could match host because this means something as foobar.com is passed and this is regarded as 'path' by the parse_url function
61
            'path'   => (isset($parsed['path']) && $parsed['path'] && $parsed['path'] != $root->host()) ? trim($parsed['path'], '/') : null,
62
            'query'  => $parsed['query'] ?? null,
63
            'hash'   => $parsed['fragment'] ?? null,
64
        ];
65
    }
66
67
    public function replaceRoot(Root $root): self
68
    {
69
        return new static(
70
            $root,
71
            $this->path,
72
            $this->query,
73
            $this->hash
74
        );
75
    }
76
77
    public function replaceScheme(string $scheme): self
78
    {
79
        return new static(
80
            $this->root->replaceScheme($scheme),
81
            $this->path,
82
            $this->query,
83
            $this->hash
84
        );
85
    }
86
87
    public function replacePath(string $path): self
88
    {
89
        return new static(
90
            $this->root,
91
            trim($path, '/'),
92
            $this->query,
93
            $this->hash
94
        );
95
    }
96
97
    public function scheme(): ?string
98
    {
99
        return $this->root->scheme();
100
    }
101
102
    public function host(): ?string
103
    {
104
        return $this->root->host();
105
    }
106
107
    public function port(): ?string
108
    {
109
        return $this->root->port();
110
    }
111
112
    public function path(): ?string
113
    {
114
        return $this->path;
115
    }
116
117
    public function query(): ?string
118
    {
119
        return $this->query;
120
    }
121
122
    public function hash(): ?string
123
    {
124
        return $this->hash;
125
    }
126
127
    public function hasScheme(): bool
128
    {
129
        return !!$this->root->scheme();
130
    }
131
132
    public function hasHost(): bool
133
    {
134
        return !!$this->root->host();
135
    }
136
137
    public function hasPort(): bool
138
    {
139
        return !!$this->root->port();
140
    }
141
142
    public function hasPath(): bool
143
    {
144
        return !!$this->path;
145
    }
146
147
    public function hasQuery(): bool
148
    {
149
        return !!$this->query;
150
    }
151
152
    public function hasHash(): bool
153
    {
154
        return !!$this->hash;
155
    }
156
}
157