Passed
Push — master ( 70e1b5...077e0f )
by ignace nyamagana
03:56 queued 52s
created

UriInfo::getOrigin()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php
2
3
/**
4
 * League.Uri (https://uri.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace League\Uri;
15
16
use League\Uri\Contracts\UriInterface;
17
use Psr\Http\Message\UriInterface as Psr7UriInterface;
18
use function explode;
19
use function implode;
20
use function preg_replace_callback;
21
use function rawurldecode;
22
use function sprintf;
23
24
final class UriInfo
25
{
26
    private const REGEXP_ENCODED_CHARS = ',%(2[D|E]|3[0-9]|4[1-9|A-F]|5[0-9|A|F]|6[1-9|A-F]|7[0-9|E]),i';
27
28
    private const WHATWG_SPECIAL_SCHEMES = ['ftp', 'http', 'https', 'ws', 'wss'];
29
30
    /**
31
     * @codeCoverageIgnore
32
     */
33
    private function __construct()
34
    {
35
    }
36
37
    /**
38
     * @param Psr7UriInterface|UriInterface $uri
39
     */
40 138
    private static function emptyComponentValue($uri): ?string
41
    {
42 138
        return $uri instanceof Psr7UriInterface ? '' : null;
43
    }
44
45
    /**
46
     * Filter the URI object.
47
     *
48
     * To be valid an URI MUST implement at least one of the following interface:
49
     *     - League\Uri\UriInterface
50
     *     - Psr\Http\Message\UriInterface
51
     *
52
     * @param mixed $uri the URI to validate
53
     *
54
     * @throws \TypeError if the URI object does not implements the supported interfaces.
55
     *
56
     * @return Psr7UriInterface|UriInterface
57
     */
58 152
    private static function filterUri($uri)
59
    {
60 152
        if ($uri instanceof Psr7UriInterface || $uri instanceof UriInterface) {
61 142
            return $uri;
62
        }
63
64 12
        throw new \TypeError(sprintf('The uri must be a valid URI object received `%s`', is_object($uri) ? get_class($uri) : gettype($uri)));
65
    }
66
67
    /**
68
     * Normalize an URI for comparison.
69
     *
70
     * @param Psr7UriInterface|UriInterface $uri
71
     *
72
     * @return Psr7UriInterface|UriInterface
73
     */
74 24
    private static function normalize($uri)
75
    {
76 24
        $uri = self::filterUri($uri);
77 22
        $null = self::emptyComponentValue($uri);
78
79 22
        $path = $uri->getPath();
80 22
        if ('/' === ($path[0] ?? '') || '' !== $uri->getScheme().$uri->getAuthority()) {
81 18
            $path = UriResolver::resolve($uri, $uri->withPath('')->withQuery($null))->getPath();
82
        }
83
84 22
        $query = $uri->getQuery();
85 22
        $fragment = $uri->getFragment();
86 22
        $fragmentOrig = $fragment;
87 22
        $pairs = null === $query ? [] : explode('&', $query);
88 22
        sort($pairs, SORT_REGULAR);
89
90
        $replace = static function (array $matches): string {
91 4
            return rawurldecode($matches[0]);
92 22
        };
93
94 22
        $retval = preg_replace_callback(self::REGEXP_ENCODED_CHARS, $replace, [$path, implode('&', $pairs), $fragment]);
95 22
        if (null !== $retval) {
96 22
            [$path, $query, $fragment] = $retval + ['', $null, $null];
97
        }
98
99 22
        if ($null !== $uri->getAuthority() && '' === $path) {
100 10
            $path = '/';
101
        }
102
103
        return $uri
104 22
            ->withHost(Uri::createFromComponents(['host' => $uri->getHost()])->getHost())
105 22
            ->withPath($path)
106 22
            ->withQuery([] === $pairs ? $null : $query)
107 22
            ->withFragment($null === $fragmentOrig ? $fragmentOrig : $fragment);
108
    }
109
110
    /**
111
     * Tell whether the URI represents an absolute URI.
112
     *
113
     * @param Psr7UriInterface|UriInterface $uri
114
     */
115 10
    public static function isAbsolute($uri): bool
116
    {
117 10
        return self::emptyComponentValue($uri) !== self::filterUri($uri)->getScheme();
118
    }
119
120
    /**
121
     * Tell whether the URI represents a network path.
122
     *
123
     * @param Psr7UriInterface|UriInterface $uri
124
     */
125 10
    public static function isNetworkPath($uri): bool
126
    {
127 10
        $uri = self::filterUri($uri);
128 8
        $null = self::emptyComponentValue($uri);
129
130 8
        return $null === $uri->getScheme() && $null !== $uri->getAuthority();
131
    }
132
133
    /**
134
     * Tell whether the URI represents an absolute path.
135
     *
136
     * @param Psr7UriInterface|UriInterface $uri
137
     */
138 10
    public static function isAbsolutePath($uri): bool
139
    {
140 10
        $uri = self::filterUri($uri);
141 8
        $null = self::emptyComponentValue($uri);
142
143 8
        return $null === $uri->getScheme()
144 8
            && $null === $uri->getAuthority()
145 8
            && '/' === ($uri->getPath()[0] ?? '');
146
    }
147
148
    /**
149
     * Tell whether the URI represents a relative path.
150
     *
151
     * @param Psr7UriInterface|UriInterface $uri
152
     */
153 110
    public static function isRelativePath($uri): bool
154
    {
155 110
        $uri = self::filterUri($uri);
156 108
        $null = self::emptyComponentValue($uri);
157
158 108
        return $null === $uri->getScheme()
159 108
            && $null === $uri->getAuthority()
160 108
            && '/' !== ($uri->getPath()[0] ?? '');
161
    }
162
163
    /**
164
     * Tell whether both URI refers to the same document.
165
     *
166
     * @param Psr7UriInterface|UriInterface $uri
167
     * @param Psr7UriInterface|UriInterface $base_uri
168
     */
169 24
    public static function isSameDocument($uri, $base_uri): bool
170
    {
171 24
        $uri = self::normalize($uri);
172 22
        $base_uri = self::normalize($base_uri);
173
174 20
        return (string) $uri->withFragment($uri instanceof Psr7UriInterface ? '' : null)
175 20
            === (string) $base_uri->withFragment($base_uri instanceof Psr7UriInterface ? '' : null);
176
    }
177
178
    /**
179
     * Returns the URI origin property as defined by WHATWG URL living standard.
180
     *
181
     * {@see https://url.spec.whatwg.org/#origin}
182
     *
183
     * For URI without a special scheme the method returns null
184
     * For URI with the file scheme the method will return null (as this is left to the implementation decision)
185
     * For URI with a special scheme the method returns the scheme followed by its authority (without the userinfo part)
186
     *
187
     * @param Psr7UriInterface|UriInterface $uri
188
     */
189 14
    public static function getOrigin($uri): ?string
190
    {
191 14
        $scheme = self::filterUri($uri)->getScheme();
192 14
        if ('blob' === $scheme) {
193 2
            $uri = Uri::createFromString($uri->getPath());
194 2
            $scheme = $uri->getScheme();
195
        }
196
197 14
        if (in_array($scheme, self::WHATWG_SPECIAL_SCHEMES, true)) {
198 8
            $null = self::emptyComponentValue($uri);
199
200 8
            return (string) $uri->withFragment($null)->withQuery($null)->withPath('')->withUserInfo($null, null);
201
        }
202
203 6
        return null;
204
    }
205
}
206