Passed
Push — master ( 65ba04...698d84 )
by Ben
01:03 queued 11s
created

ParsedUrl::query()   A

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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 17
    public function __construct(Root $root, ?string $path = null, ?string $query = null, ?string $hash = null)
22
    {
23 17
        $this->root = $root;
24 17
        $this->path = $path;
25 17
        $this->query = $query;
26 17
        $this->hash = $hash;
27 17
    }
28
29 18
    public static function fromString(string $url)
30
    {
31 18
        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 15
    public function get(): string
35
    {
36 15
        return  $this->root->get() .
37 15
            ($this->hasPath() ? '/' . $this->path() : '') .
38 15
            ($this->hasQuery() ? '?' . $this->query() : '') .
39 15
            ($this->hasHash() ? '#' . $this->hash() : '');
40
    }
41
42 18
    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 18
        if ($url == '//') {
47 1
            $url = '/';
48
        }
49
50 18
        $parsed = parse_url($url);
51
52 18
        if (false === $parsed) {
53 1
            throw new InvalidUrl('Failed to parse url. Invalid url ['.$url.'] passed as parameter.');
54
        }
55
56 17
        $root = Root::fromString($url)->defaultScheme(null);
57
58
        return [
59 17
            '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 17
            'path'   => (isset($parsed['path']) && $parsed['path'] && $parsed['path'] != $root->host()) ? trim($parsed['path'], '/') : null,
62 17
            'query'  => $parsed['query'] ?? null,
63 17
            'hash'   => $parsed['fragment'] ?? null,
64
        ];
65
    }
66
67 4
    public function replaceRoot(Root $root): self
68
    {
69 4
        return new static(
70 4
            $root,
71 4
            $this->path,
72 4
            $this->query,
73 4
            $this->hash
74
        );
75
    }
76
77 6
    public function replaceScheme(string $scheme): self
78
    {
79 6
        return new static(
80 6
            $this->root->replaceScheme($scheme),
81 6
            $this->path,
82 6
            $this->query,
83 6
            $this->hash
84
        );
85
    }
86
87 9
    public function replacePath(string $path): self
88
    {
89 9
        return new static(
90 9
            $this->root,
91 9
            trim($path, '/'),
92 9
            $this->query,
93 9
            $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 10
    public function path(): ?string
113
    {
114 10
        return $this->path;
115
    }
116
117 3
    public function query(): ?string
118
    {
119 3
        return $this->query;
120
    }
121
122 1
    public function hash(): ?string
123
    {
124 1
        return $this->hash;
125
    }
126
127
    public function hasScheme(): bool
128
    {
129
        return !!$this->root->scheme();
130
    }
131
132 1
    public function hasHost(): bool
133
    {
134 1
        return !!$this->root->host();
135
    }
136
137
    public function hasPort(): bool
138
    {
139
        return !!$this->root->port();
140
    }
141
142 15
    public function hasPath(): bool
143
    {
144 15
        return !!$this->path;
145
    }
146
147 15
    public function hasQuery(): bool
148
    {
149 15
        return !!$this->query;
150
    }
151
152 15
    public function hasHash(): bool
153
    {
154 15
        return !!$this->hash;
155
    }
156
}
157