Completed
Push — master ( be4b54...4b1069 )
by ignace nyamagana
04:15
created

AbstractHierarchicalUri::isAuthorityValid()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.2
cc 4
eloc 5
nc 3
nop 0
crap 4
1
<?php
2
/**
3
 * League.Uri (http://uri.thephpleague.com)
4
 *
5
 * @package   League.uri
6
 * @author    Ignace Nyamagana Butera <[email protected]>
7
 * @copyright 2013-2015 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\Schemes\Generic;
13
14
use League\Uri\Components\Fragment;
15
use League\Uri\Components\HierarchicalPath as Path;
16
use League\Uri\Components\Host;
17
use League\Uri\Components\Pass;
18
use League\Uri\Components\Port;
19
use League\Uri\Components\Query;
20
use League\Uri\Components\Scheme;
21
use League\Uri\Components\User;
22
use League\Uri\Components\UserInfo;
23
use League\Uri\Interfaces\Fragment as FragmentInterface;
24
use League\Uri\Interfaces\HierarchicalPath as PathInterface;
25
use League\Uri\Interfaces\Host as HostInterface;
26
use League\Uri\Interfaces\Port as PortInterface;
27
use League\Uri\Interfaces\Query as QueryInterface;
28
use League\Uri\Interfaces\Scheme as SchemeInterface;
29
use League\Uri\Interfaces\UserInfo as UserInfoInterface;
30
use League\Uri\UriParser;
31
32
/**
33
 * Value object representing a Hierarchical URI.
34
 *
35
 * @package League.uri
36
 * @author  Ignace Nyamagana Butera <[email protected]>
37
 * @since   4.0.0
38
 *
39
 * @property-read SchemeInterface   $scheme
40
 * @property-read UserInfoInterface $userInfo
41
 * @property-read HostInterface     $host
42
 * @property-read PortInterface     $port
43
 * @property-read PathInterface     $path
44
 * @property-read QueryInterface    $query
45
 * @property-read FragmentInterface $fragment
46
 */
47
abstract class AbstractHierarchicalUri extends AbstractUri
48
{
49
    /**
50
     * Create a new instance of URI
51
     *
52
     * @param SchemeInterface   $scheme
53
     * @param UserInfoInterface $userInfo
54
     * @param HostInterface     $host
55
     * @param PortInterface     $port
56
     * @param PathInterface     $path
57
     * @param QueryInterface    $query
58
     * @param FragmentInterface $fragment
59
     */
60 563
    public function __construct(
61
        SchemeInterface $scheme,
62
        UserInfoInterface $userInfo,
63
        HostInterface $host,
64
        PortInterface $port,
65
        PathInterface $path,
66
        QueryInterface $query,
67
        FragmentInterface $fragment
68
    ) {
69 563
        $this->scheme = $scheme;
70 563
        $this->userInfo = $userInfo;
71 563
        $this->host = $host;
72 563
        $this->port = $port;
73 563
        $this->path = $path;
74 563
        $this->query = $query;
75 563
        $this->fragment = $fragment;
76 563
        $this->assertValidObject();
77 521
    }
78
79
    /**
80
     * Create a new instance from a string
81
     *
82
     * @param string $uri
83
     *
84
     * @return static
85
     */
86 563
    public static function createFromString($uri = '')
87
    {
88 563
        return static::createFromComponents((new UriParser())->__invoke($uri));
89
    }
90
91
    /**
92
     * Create a new instance from a hash of parse_url parts
93
     *
94
     * @param array $components a hash representation of the URI similar to PHP parse_url function result
95
     *
96
     * @return static
97
     */
98 563
    public static function createFromComponents(array $components)
99
    {
100 563
        $components = self::normalizeUriHash($components);
101
102 563
        return new static(
103 563
            new Scheme($components['scheme']),
104 563
            new UserInfo(new User($components['user']), new Pass($components['pass'])),
105 563
            new Host($components['host']),
106 563
            new Port($components['port']),
107 563
            new Path($components['path']),
108 563
            new Query($components['query']),
109 563
            new Fragment($components['fragment'])
110 375
        );
111
    }
112
113
    /**
114
     * @inheritdoc
115
     */
116 9
    public static function __set_state(array $properties)
117
    {
118 9
        return new static(
119 9
            $properties['scheme'],
120 9
            $properties['userInfo'],
121 9
            $properties['host'],
122 9
            $properties['port'],
123 9
            $properties['path'],
124 9
            $properties['query'],
125 9
            $properties['fragment']
126 6
        );
127
    }
128
129
    /**
130
     * Tell whether URI with an authority are valid
131
     *
132
     * @return bool
133
     */
134 563
    protected function isValidHierarchicalUri()
135
    {
136 563
        $this->assertSupportedScheme();
137
138 533
        return $this->isAuthorityValid();
139
    }
140
141
    /**
142
     * Tell whether the Auth URI is valid
143
     *
144
     * @return bool
145
     */
146 533
    protected function isAuthorityValid()
147
    {
148 533
        $pos = strpos($this->getSchemeSpecificPart(), '//');
149 533
        if ('' !== $this->getScheme() && 0 !== $pos) {
150 6
            return false;
151
        }
152
153 527
        return !('' === $this->getHost() && 0 === $pos);
154
    }
155
}
156