Completed
Push — master ( d793b2...4b4337 )
by ignace nyamagana
03:54
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 Psr\Http\Message\UriInterface;
17
18
/**
19
 * A function to give information about URI Reference
20
 *
21
 * This function returns an associative array representing the URI Reference information:
22
 * each key represents a given state and each value is a boolean to indicate the current URI
23
 * status against the declared state. For any given URI only one of the listed state can be valid.
24
 *
25
 * <ul>
26
 * <li>absolute_uri: Tell whether the URI is absolute (ie contains a non_empty scheme)
27
 * <li>network_path: Tell whether the URI is a network_path relative reference
28
 * <li>absolute_path: Tell whether the URI is a absolute_path relative reference
29
 * <li>relative_path: Tell whether the URI is a relative_path relative reference
30
 * </ul>
31
 *
32
 * @link  https://tools.ietf.org/html/rfc3986#section-4.2
33
 * @link  https://tools.ietf.org/html/rfc3986#section-4.3
34
 * @since 4.2.0
35
 *
36
 * @param Uri|UriInterface $uri
37
 *
38
 * @throws InvalidArgumentException if the submitted Uri is invalid
39
 *
40
 * @return array
41
 */
42
function uri_getinfo($uri)
43
{
44 285
    if (!$uri instanceof Uri && !$uri instanceof UriInterface) {
45 3
        throw new InvalidArgumentException(
46 1
            'URI passed must implement PSR_7 UriInterface or League\Uri Uri interface'
47 2
        );
48
    }
49
50
    $infos = [
51 282
        'absolute_uri' => false,
52 188
        'network_path' => false,
53 188
        'absolute_path' => false,
54 188
        'relative_path' => false,
55 188
    ];
56
57 282
    if ('' !== $uri->getScheme()) {
58 138
        $infos['absolute_uri'] = true;
59
60 138
        return $infos;
61
    }
62
63 162
    if ('' !== $uri->getAuthority()) {
64 12
        $infos['network_path'] = true;
65
66 12
        return $infos;
67
    }
68
69 150
    $path = $uri->getPath();
70 150
    if (isset($path[0]) && '/' === $path[0]) {
71 12
        $infos['absolute_path'] = true;
72
73 12
        return $infos;
74
    }
75
76 138
    $infos['relative_path'] = true;
77
78 138
    return $infos;
79
}
80