Url::getFragment()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Saxulum\HttpClient;
4
5
class Url
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $scheme;
11
12
    const SCHEME_HTTP = 'http';
13
    const SCHEME_HTTPS = 'https';
14
15
    /**
16
     * @var string
17
     */
18
    protected $hostName;
19
20
    /**
21
     * @var int
22
     */
23
    protected $port;
24
25
    const PORT_HTTP = 80;
26
    const PORT_HTTPS = 443;
27
28
    /**
29
     * @var string|null
30
     */
31
    protected $user;
32
33
    /**
34
     * @var string|null
35
     */
36
    protected $password;
37
38
    /**
39
     * @var string
40
     */
41
    protected $path;
42
43
    /**
44
     * @var string|null
45
     */
46
    protected $query;
47
48
    /**
49
     * @var string|null
50
     */
51
    protected $fragment;
52
53
    /**
54
     * @param string $url
55
     */
56
    public function __construct($url)
57
    {
58
        $urlParts = @parse_url($url);
59
60
        if (false === $urlParts) {
61
            throw new \InvalidArgumentException(sprintf('The url "%s" is invalid.', $url));
62
        }
63
64
        $this->setUrlParts($urlParts);
65
    }
66
67
    /**
68
     * @param array $urlParts
69
     */
70
    protected function setUrlParts(array $urlParts)
71
    {
72
        $this->setUrlPart($urlParts, 'scheme', Url::SCHEME_HTTP);
73
        $this->setUrlPart($urlParts, 'host', function () {
74
            throw new \InvalidArgumentException('Url has to contain a host!');
75
        }, 'hostName');
76
        $this->setUrlPart($urlParts, 'port', function (array $urlParts) {
77
            if (isset($urlParts['scheme']) && Url::SCHEME_HTTPS === $urlParts['scheme']) {
78
                return Url::PORT_HTTPS;
79
            }
80
81
            return Url::PORT_HTTP;
82
        });
83
        $this->setUrlPart($urlParts, 'user');
84
        $this->setUrlPart($urlParts, 'pass', null, 'password');
85
        $this->setUrlPart($urlParts, 'path', '/');
86
        $this->setUrlPart($urlParts, 'query');
87
        $this->setUrlPart($urlParts, 'fragment');
88
    }
89
90
    /**
91
     * @param array                    $urlParts
92
     * @param string                   $partName
93
     * @param \Closure|string|int|null $default
94
     * @param string|null              $property
95
     */
96
    protected function setUrlPart($urlParts, $partName, $default = null, $property = null)
97
    {
98
        $property = null !== $property ? $property : $partName;
99
100
        if (isset($urlParts[$partName])) {
101
            $this->$property = $urlParts[$partName];
102
103
            return;
104
        }
105
106
        if ($default instanceof \Closure) {
107
            $default = $default($urlParts);
108
        }
109
110
        $this->$property = $default;
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    public function getScheme()
117
    {
118
        return $this->scheme;
119
    }
120
121
    /**
122
     * @return string
123
     */
124
    public function getHostName()
125
    {
126
        return $this->hostName;
127
    }
128
129
    /**
130
     * @return int
131
     */
132
    public function getPort()
133
    {
134
        return $this->port;
135
    }
136
137
    /**
138
     * @return string|null
139
     */
140
    public function getUser()
141
    {
142
        return $this->user;
143
    }
144
145
    /**
146
     * @return string|null
147
     */
148
    public function getPassword()
149
    {
150
        return $this->password;
151
    }
152
153
    /**
154
     * @return string
155
     */
156
    public function getPath()
157
    {
158
        return $this->path;
159
    }
160
161
    /**
162
     * @return string|null
163
     */
164
    public function getQuery()
165
    {
166
        return $this->query;
167
    }
168
169
    /**
170
     * @return string|null
171
     */
172
    public function getFragment()
173
    {
174
        return $this->fragment;
175
    }
176
177
    /**
178
     * @return string
179
     */
180
    public function getHost()
181
    {
182
        $host = $this->getScheme() . '://';
183
        if (null !== $this->getUser()) {
184
            $host .= $this->getUser() . ':' . $this->getPassword() . '@';
185
        }
186
        $host .= $this->getHostName();
187
        $host .= ':' . $this->getPort();
188
189
        return $host;
190
    }
191
192
    /**
193
     * @return string
194
     */
195
    public function getResource()
196
    {
197
        $resource = $this->getPath();
198
        if (null !== $this->getQuery()) {
199
            $resource .= '?' . $this->getQuery();
200
        }
201
        if (null !== $this->getFragment()) {
202
            $resource .= '#' . $this->getFragment();
203
        }
204
205
        return $resource;
206
    }
207
208
    /**
209
     * @return string
210
     */
211
    public function __toString()
212
    {
213
        return $this->getHost() . $this->getResource();
214
    }
215
}
216