1
|
|
|
<?php namespace Neomerx\Cors\Http; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2015 [email protected] (www.neomerx.com) |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
use \InvalidArgumentException; |
20
|
|
|
use \Neomerx\Cors\Contracts\Http\ParsedUrlInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @package Neomerx\Cors |
24
|
|
|
*/ |
25
|
|
|
class ParsedUrl implements ParsedUrlInterface |
26
|
|
|
{ |
27
|
|
|
/** Key for result from parse_url() function */ |
28
|
|
|
const URL_KEY_SCHEME = 'scheme'; |
29
|
|
|
|
30
|
|
|
/** Key for result from parse_url() function */ |
31
|
|
|
const URL_KEY_HOST = 'host'; |
32
|
|
|
|
33
|
|
|
/** Key for result from parse_url() function */ |
34
|
|
|
const URL_KEY_PORT = 'port'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var string|null |
38
|
|
|
*/ |
39
|
|
|
private $scheme; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var string|null |
43
|
|
|
*/ |
44
|
|
|
private $host; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @var int |
48
|
|
|
*/ |
49
|
|
|
private $port; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @var null|string |
53
|
|
|
*/ |
54
|
|
|
private $urlAsString; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string|array $url |
58
|
|
|
*/ |
59
|
19 |
|
public function __construct($url) |
60
|
|
|
{ |
61
|
19 |
|
assert('is_string($url) || is_array($url)'); |
62
|
|
|
|
63
|
19 |
|
if (is_array($url) === true) { |
64
|
10 |
|
$parsedUrl = $url; |
65
|
10 |
|
} else { |
66
|
18 |
|
$parsedUrl = parse_url($url); |
67
|
|
|
} |
68
|
|
|
|
69
|
19 |
|
if ($parsedUrl === false) { |
70
|
1 |
|
throw new InvalidArgumentException('url'); |
71
|
|
|
} |
72
|
|
|
|
73
|
18 |
|
$this->scheme = $this->getArrayValue($parsedUrl, self::URL_KEY_SCHEME); |
74
|
18 |
|
$this->host = $this->getArrayValue($parsedUrl, self::URL_KEY_HOST); |
75
|
18 |
|
$this->port = (int)$this->getArrayValue($parsedUrl, self::URL_KEY_PORT, self::DEFAULT_PORT); |
76
|
18 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @inheritdoc |
80
|
|
|
*/ |
81
|
6 |
|
public function getScheme() |
82
|
|
|
{ |
83
|
6 |
|
return $this->scheme; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @inheritdoc |
88
|
|
|
*/ |
89
|
14 |
|
public function getHost() |
90
|
|
|
{ |
91
|
14 |
|
return $this->host; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @inheritdoc |
96
|
|
|
*/ |
97
|
15 |
|
public function getPort() |
98
|
|
|
{ |
99
|
15 |
|
return $this->port; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @inheritdoc |
104
|
|
|
*/ |
105
|
13 |
|
public function getOrigin() |
106
|
|
|
{ |
107
|
13 |
|
if ($this->urlAsString === null) { |
108
|
13 |
|
$url = $this->scheme === null ? '' : $this->scheme . ':'; |
109
|
13 |
|
$url .= $this->host === null ? '' : '//' . $this->host; |
110
|
13 |
|
$url .= $this->port === self::DEFAULT_PORT ? '' : ':' . $this->port; |
111
|
|
|
|
112
|
13 |
|
$this->urlAsString = $url; |
113
|
13 |
|
} |
114
|
|
|
|
115
|
13 |
|
return $this->urlAsString; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @inheritdoc |
120
|
|
|
*/ |
121
|
2 |
|
public function isSchemeEqual(ParsedUrlInterface $rhs) |
122
|
|
|
{ |
123
|
2 |
|
return strcasecmp($this->getScheme(), $rhs->getScheme()) === 0; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @inheritdoc |
128
|
|
|
*/ |
129
|
10 |
|
public function isHostEqual(ParsedUrlInterface $rhs) |
130
|
|
|
{ |
131
|
10 |
|
return strcasecmp($this->getHost(), $rhs->getHost()) === 0; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
11 |
|
public function isPortEqual(ParsedUrlInterface $rhs) |
138
|
|
|
{ |
139
|
11 |
|
return $this->getPort() === $rhs->getPort(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @inheritDoc |
144
|
|
|
*/ |
145
|
1 |
|
public function __toString() |
146
|
|
|
{ |
147
|
1 |
|
return $this->getOrigin(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param array $array |
152
|
|
|
* @param string $key |
153
|
|
|
* @param mixed $default |
154
|
|
|
* |
155
|
|
|
* @return mixed |
156
|
|
|
*/ |
157
|
18 |
|
private function getArrayValue(array $array, $key, $default = null) |
158
|
|
|
{ |
159
|
18 |
|
return isset($array[$key]) ? $array[$key] : $default; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|