1 | <?php |
||
10 | class Asset |
||
11 | { |
||
12 | |||
13 | const CODE_ERROR = 500; |
||
14 | public static $CODES_ERROR = [404, 403, 500, 503]; |
||
15 | public static $CODES_WARNING = [301]; |
||
16 | |||
17 | /** |
||
18 | * @var null|string |
||
19 | */ |
||
20 | public $scheme; |
||
21 | |||
22 | /** |
||
23 | * @var null|string |
||
24 | */ |
||
25 | public $host; |
||
26 | |||
27 | /** |
||
28 | * @var int |
||
29 | */ |
||
30 | public $port = 80; |
||
31 | |||
32 | /** |
||
33 | * @var null|string |
||
34 | */ |
||
35 | public $path; |
||
36 | |||
37 | /** |
||
38 | * @var |
||
39 | */ |
||
40 | public $query; |
||
41 | |||
42 | /** |
||
43 | * @var Asset |
||
44 | */ |
||
45 | public $parentPage; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $fullHtml; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $responseCode; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $type; |
||
61 | |||
62 | |||
63 | /** |
||
64 | * Asset constructor. |
||
65 | * |
||
66 | * @param string $url |
||
67 | * @param Asset $parentPage |
||
68 | * @param $fullHtml |
||
69 | * @param $type |
||
70 | */ |
||
71 | public function __construct( |
||
72 | $url, |
||
73 | $parentPage = null, |
||
74 | $fullHtml = '', |
||
75 | $type = 'page' |
||
76 | ) { |
||
77 | $urlProperties = parse_url($url); |
||
78 | |||
79 | foreach (['scheme', 'host', 'path', 'port', 'query'] as $property) { |
||
80 | if (isset($urlProperties[$property])) { |
||
81 | $this->$property = $urlProperties[$property]; |
||
82 | } |
||
83 | } |
||
84 | $this->parentPage = $parentPage; |
||
85 | $this->fullHtml = $fullHtml; |
||
86 | $this->type = $type; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Determine if the url is relative. |
||
91 | * |
||
92 | * @return bool |
||
93 | */ |
||
94 | public function isRelative() |
||
98 | |||
99 | /** |
||
100 | * Determine if the url is protocol independent. |
||
101 | * |
||
102 | * @return bool |
||
103 | */ |
||
104 | public function isProtocolIndependent() |
||
108 | |||
109 | /** |
||
110 | * Determine if this is a mailto-link. |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public function isHttp() |
||
115 | { |
||
116 | // Empty scheme usually means http |
||
117 | return in_array( |
||
118 | $this->scheme, |
||
119 | ['http', 'https', ''] |
||
120 | ); |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Set the scheme. |
||
125 | * |
||
126 | * @param string $scheme |
||
127 | * |
||
128 | * @return $this |
||
129 | */ |
||
130 | public function setScheme($scheme) |
||
131 | { |
||
132 | $this->scheme = $scheme; |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | /** |
||
138 | * Set the host. |
||
139 | * |
||
140 | * @param string $host |
||
141 | * |
||
142 | * @return $this |
||
143 | */ |
||
144 | public function setHost($host) |
||
145 | { |
||
146 | $this->host = $host; |
||
147 | |||
148 | return $this; |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * @param int $port |
||
153 | * |
||
154 | * @return $this |
||
155 | * |
||
156 | */ |
||
157 | public function setPort($port) |
||
158 | { |
||
159 | $this->port = $port; |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return Asset |
||
166 | */ |
||
167 | public function getParentPage() |
||
171 | |||
172 | /** |
||
173 | * @param Asset $parentPage |
||
174 | */ |
||
175 | public function setParentPage($parentPage) |
||
179 | |||
180 | |||
181 | /** |
||
182 | * @return string |
||
183 | */ |
||
184 | public function getResponseCode() |
||
188 | |||
189 | /** |
||
190 | * @param string $responseCode |
||
191 | */ |
||
192 | public function setResponseCode($responseCode) |
||
196 | |||
197 | /** |
||
198 | * Remove the fragment. |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function removeFragment() |
||
203 | { |
||
204 | $this->path = explode('#', $this->path)[0]; |
||
205 | |||
206 | return $this; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @return string |
||
211 | */ |
||
212 | public function getFullHtml() |
||
216 | |||
217 | /** |
||
218 | * @param string $fullHtml |
||
219 | */ |
||
220 | public function setFullHtml($fullHtml) |
||
224 | |||
225 | /** |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getURL() |
||
229 | { |
||
230 | $path = strpos($this->path, '/') === 0 ? |
||
231 | substr($this->path, 1) : $this->path; |
||
232 | |||
233 | $port = ($this->port === 80 ? '' : ":{$this->port}"); |
||
234 | $url = "{$this->scheme}://{$this->host}{$port}/{$path}"; |
||
235 | |||
236 | if ($this->query) { |
||
237 | $url .= "?{$this->query}"; |
||
238 | } |
||
239 | |||
240 | return $url; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Convert the url to string. |
||
245 | * |
||
246 | * @return string |
||
247 | */ |
||
248 | public function __toString() |
||
252 | |||
253 | /** |
||
254 | * @return bool |
||
255 | */ |
||
256 | public function isSuccessful() |
||
260 | |||
261 | /** |
||
262 | * @return bool |
||
263 | */ |
||
264 | public function isError() |
||
268 | |||
269 | /** |
||
270 | * @return bool |
||
271 | */ |
||
272 | public function isWarning() |
||
276 | |||
277 | /** |
||
278 | * @return string |
||
279 | */ |
||
280 | public function getType() |
||
281 | { |
||
282 | return $this->type; |
||
283 | } |
||
284 | |||
285 | /** |
||
286 | * @return null|string |
||
287 | */ |
||
288 | public function getPath() |
||
292 | |||
293 | } |
||
294 |