Passed
Pull Request — master (#164)
by ignace nyamagana
02:10
created

UriInfo::getOrigin()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 4

Importance

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