Completed
Push — master ( e3dc4f...792697 )
by ignace nyamagana
03:05
created

functions.php ➔ uri_getinfo()   C

Complexity

Conditions 7
Paths 5

Size

Total Lines 38
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 7

Importance

Changes 0
Metric Value
cc 7
eloc 21
nc 5
nop 1
dl 0
loc 38
ccs 21
cts 21
cp 1
crap 7
rs 6.7272
c 0
b 0
f 0
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2016 Ignace Nyamagana Butera
8
 * @license   https://github.com/thephpleague/uri/blob/master/LICENSE (MIT License)
9
 * @version   4.2.0
10
 * @link      https://github.com/thephpleague/uri/
11
 */
12
namespace League\Uri;
13
14
use InvalidArgumentException;
15
use League\Uri\Interfaces\Uri;
16
use League\Uri\Modifiers\Normalize;
17
use Psr\Http\Message\UriInterface;
18
19
/**
20
 * A function to give information about URI Reference
21
 *
22
 * This function returns an associative array representing the URI Reference information:
23
 * each key represents a given state and each value is a boolean to indicate the current URI
24
 * status against the declared state.
25
 *
26
 * <ul>
27
 * <li>absolute_uri: Tell whether the URI is absolute
28
 * <li>network_path: Tell whether the URI is a network-path relative reference
29
 * <li>absolute_path: Tell whether the URI is a absolute-path relative reference
30
 * <li>relative_path: Tell whether the URI is a relative-path relative reference
31
 * <li>same_document: Tell whether the URI is a same-document relative reference
32
 * </ul>
33
 *
34
 * @link https://tools.ietf.org/html/rfc3986#section-4.2
35
 * @link https://tools.ietf.org/html/rfc3986#section-4.3
36
 * @link https://tools.ietf.org/html/rfc3986#section-4.4
37
 *
38
 * @since 4.2.0
39
 *
40
 * @param Uri|UriInterface      $uri      The uri to get reference info from
41
 * @param Uri|UriInterface|null $base_uri The base uri to use to get same document reference info
42
 *
43
 * @throws InvalidArgumentException if the submitted Uri is invalid
44
 *
45
 * @return array
46
 */
47
function uri_reference($uri, $base_uri = null)
48
{
49 258
    if (!$uri instanceof Uri && !$uri instanceof UriInterface) {
50 3
        throw new InvalidArgumentException(
51 1
            'URI passed must implement PSR-7 UriInterface or League\Uri Uri interface'
52 2
        );
53
    }
54
55 255
    if (null !== $base_uri && (!$base_uri instanceof Uri && !$base_uri instanceof UriInterface)) {
56 3
        throw new InvalidArgumentException(
57 1
            'The base URI passed must implement PSR-7 UriInterface or League\Uri Uri interface'
58 2
        );
59
    }
60
61
    $infos = [
62 252
        'absolute_uri' => false,
63 168
        'network_path' => false,
64 168
        'absolute_path' => false,
65 168
        'relative_path' => false,
66 168
        'same_document' => false,
67 168
    ];
68
69 252
    static $normalizer;
70 252
    if (null === $normalizer) {
71 3
        $normalizer = new Normalize();
72 2
    }
73
74 252
    if (null !== $base_uri) {
75 6
        $uri_string = (string) $normalizer($uri)->withFragment('');
76 6
        $base_uri_string = (string) $normalizer($base_uri)->withFragment('');
77 6
        $infos['same_document'] = $uri_string === $base_uri_string;
78 4
    }
79
80 252
    if ('' !== $uri->getScheme()) {
81 111
        $infos['absolute_uri'] = true;
82
83 111
        return $infos;
84
    }
85
86 159
    if ('' !== $uri->getAuthority()) {
87 9
        $infos['network_path'] = true;
88
89 9
        return $infos;
90
    }
91
92 150
    $path = $uri->getPath();
93 150
    if (isset($path[0]) && '/' === $path[0]) {
94 12
        $infos['absolute_path'] = true;
95
96 12
        return $infos;
97
    }
98
99 138
    $infos['relative_path'] = true;
100
101 138
    return $infos;
102
}
103