Completed
Push — master ( 0cbce9...ce80be )
by ignace nyamagana
8s
created

AbstractHierarchicalUri::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 1

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
cc 1
eloc 16
nc 1
nop 7
crap 1
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.1.1
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 548
    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 548
        $this->scheme = $scheme;
70 548
        $this->userInfo = $userInfo;
71 548
        $this->host = $host;
72 548
        $this->port = $port;
73 548
        $this->path = $path;
74 548
        $this->query = $query;
75 548
        $this->fragment = $fragment;
76 548
        $this->assertValidObject();
77 506
    }
78
79
    /**
80
     * Create a new instance from a string
81
     *
82
     * @param string $uri
83
     *
84
     * @return static
85
     */
86 548
    public static function createFromString($uri = '')
87
    {
88 548
        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 548
    public static function createFromComponents(array $components)
99
    {
100 548
        $components = self::normalizeUriHash($components);
101
102 548
        return new static(
103 548
            new Scheme($components['scheme']),
104 548
            new UserInfo(new User($components['user']), new Pass($components['pass'])),
105 548
            new Host($components['host']),
106 548
            new Port($components['port']),
107 548
            new Path($components['path']),
108 548
            new Query($components['query']),
109 548
            new Fragment($components['fragment'])
110 365
        );
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