Completed
Push — master ( 4f406e...293c4f )
by Derek
02:26
created

Authority::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace Subreality\Dilmun\Anshar\Http\UriParts;
3
4
/**
5
 * Class Authority
6
 * @package Subreality\Dilmun\Anshar\Http\UriParts
7
 */
8
class Authority extends AbstractUriPart
9
{
10
    /** @var  UserInfo */
11
    protected $user_info;
12
    /** @var  Host */
13
    protected $host;
14
    /** @var  Port */
15
    protected $port;
16
17
    protected $authority_parts = array(
18
        "user_info" => "",
19
        "host"      => "",
20
        "port"      => null,
21
    );
22
23
    /** @noinspection PhpMissingParentConstructorInspection */
24
    /**
25
     * Authority constructor. Accepts a string representing a URI authority component. Construction will throw an
26
     * exception if the authority is not a string.
27
     *
28
     * Construction splits a given authority string into its component objects, UserInfo, Host, and Port, whose
29
     * individual constructors may throw exceptions provided invalid component strings.
30
     *
31
     * Construction accepts strings for UserInfo and Host that have been percent-encoded as well as strings that have
32
     * not been percent-encoded and will encode invalid characters.
33
     *
34
     * Construction with a string that includes both encoded and decoded characters will be assumed to be an encoded
35
     * string, resulting in double-encoding.
36
     *
37
     * authority = [ userinfo "@" ] host [ ":" port ]
38
     *
39
     * @see UserInfo
40
     * @see Host
41
     * @see Port
42
     * @see https://tools.ietf.org/html/rfc3986#appendix-A
43
     *
44
     * @throws \InvalidArgumentException given a non-string
45
     *
46
     * @param string $authority A string representing a URI authority
47
     */
48 10
    public function __construct($authority)
49
    {
50 10
        if (!is_string($authority)) {
51 6
            throw new \InvalidArgumentException("Authority must be a string");
52
        } else {
53 4
            $exploded_authority = $this->explodeAuthority($authority);
54
55 4
            $this->authority_parts = array_merge($this->authority_parts, $exploded_authority);
56
57 4
            $this->user_info = new UserInfo($this->authority_parts["user_info"]);
58 4
            $this->host      = new Host($this->authority_parts["host"]);
59 4
            $this->port      = new Port($this->authority_parts["port"]);
60
        }
61 3
    }
62
63
    /**
64
     * Returns the authority's UserInfo object
65
     *
66
     * @return UserInfo
67
     */
68 4
    public function getUserInfo()
69
    {
70 4
        return $this->user_info;
71
    }
72
73
    /**
74
     * Returns the authority's Host object
75
     *
76
     * @return Host
77
     */
78 3
    public function getHost()
79
    {
80 3
        return $this->host;
81
    }
82
83
    /**
84
     * Returns the authority's Port object
85
     *
86
     * @return Port
87
     */
88 4
    public function getPort()
89
    {
90 4
        return $this->port;
91
    }
92
93
    /**
94
     * Returns a string representation of the authority
95
     *
96
     * @return string   A string representation of the authority
97
     */
98 4
    public function __toString()
99
    {
100 4
        return $this->user_info->toUriString() . $this->host->toUriString() . $this->port->toUriString();
101
    }
102
103
    /**
104
     * Splits URI authority data into user info, host, and port data, returning an array with named keys.
105
     *
106
     * For the host component, it will capture everything within brackets to support ipv6 or match all characters until
107
     * it finds a colon indicating the start of the port component.
108
     *
109
     * @param string $authority     The authority part of a URI to be decomposed
110
     * @return mixed[]              An array with named keys containing the component parts of the supplied
0 ignored issues
show
Documentation introduced by
Should the return type not be array<*,string|integer|null>?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
111
     *                              authority
112
     */
113 4 View Code Duplication
    private function explodeAuthority($authority)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
114
    {
115 4
        $reg_start      = '/^';
116 4
        $user_info_part = '(?:(?\'user_info\'.+)@)?';
117 4
        $host_part      = '(?\'host\'\[.+\]|.[^:]+)';
118 4
        $port_part      = '(?::(?\'port\'[0-9]+))?';
119 4
        $reg_end        = '/';
120
121 4
        $authority_syntax = $reg_start . $user_info_part . $host_part . $port_part . $reg_end;
122
123 4
        preg_match($authority_syntax, $authority, $authority_parts);
124
125 4
        if (isset($authority_parts["port"])) {
126 2
            $authority_parts["port"] = (int) $authority_parts["port"];
127 2
        }
128
129 4
        return $authority_parts;
130
    }
131
}
132