webino /
request
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Webino™ (http://webino.sk) |
||
| 4 | * |
||
| 5 | * @link https://github.com/webino/request |
||
| 6 | * @copyright Copyright (c) 2019 Webino, s.r.o. (http://webino.sk) |
||
| 7 | * @author Peter Bačinský <[email protected]> |
||
| 8 | * @license BSD-3-Clause |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace Webino; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * Class HttpRequest |
||
| 15 | * @package request |
||
| 16 | */ |
||
| 17 | class HttpRequest extends AbstractRequest implements |
||
| 18 | HttpRequestInterface, |
||
| 19 | InstanceFactoryMethodInterface |
||
| 20 | { |
||
| 21 | /** |
||
| 22 | * @var BasePathInterface |
||
| 23 | */ |
||
| 24 | protected $basePath; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var RequestUri |
||
| 28 | */ |
||
| 29 | protected $requestUri; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @param CreateInstanceEventInterface $event |
||
| 33 | * @return HttpRequest |
||
| 34 | */ |
||
| 35 | public static function create(CreateInstanceEventInterface $event): HttpRequest |
||
| 36 | { |
||
| 37 | $container = $event->getContainer(); |
||
| 38 | $params = $event->getParameters(); |
||
| 39 | $request = new static($params[0] ?? []); |
||
| 40 | $requestUri = $container->make(RequestUriInterface::class, $request); |
||
| 41 | $basePath = $container->make(BasePathInterface::class, $request, $requestUri); |
||
| 42 | $request->setUri($requestUri); |
||
| 43 | $request->setBasePath($basePath); |
||
| 44 | return $request; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Returns request defaults |
||
| 49 | * |
||
| 50 | * @param array $overrides |
||
| 51 | * @return array |
||
| 52 | */ |
||
| 53 | public static function defaults(array $overrides = []): array |
||
| 54 | { |
||
| 55 | return $overrides + [ |
||
| 56 | HttpRequest::TIME => HttpRequest::TIME_DEFAULT, |
||
| 57 | HttpRequest::SCRIPT_NAME => HttpRequest::SCRIPT_NAME_DEFAULT, |
||
| 58 | HttpRequest::SCRIPT_FILENAME => HttpRequest::SCRIPT_FILENAME_DEFAULT, |
||
| 59 | HttpRequest::GATEWAY_INTERFACE => HttpRequest::GATEWAY_INTERFACE_DEFAULT, |
||
| 60 | HttpRequest::SCHEME => HttpRequest::SCHEME_HTTPS, |
||
| 61 | HttpRequest::HOST => HttpRequest::HOST_LOCAL, |
||
| 62 | HttpRequest::HOST_IP => HttpRequest::HOST_IP_LOCAL, |
||
| 63 | HttpRequest::PORT => HttpRequest::PORT_HTTPS, |
||
| 64 | HttpRequest::METHOD => HttpRequest::METHOD_POST, |
||
| 65 | HttpRequest::URI => HttpRequest::URI_DEFAULT, |
||
| 66 | HttpRequest::QUERY_STRING => HttpRequest::QUERY_STRING_DEFAULT, |
||
| 67 | HttpRequest::SERVER_SOFTWARE => HttpRequest::SERVER_SOFTWARE_APACHE, |
||
| 68 | HttpRequest::ACCEPT => HttpRequest::ACCEPT_HTML, |
||
| 69 | HttpRequest::ACCEPT_LANGUAGE => HttpRequest::ACCEPT_LANGUAGE_DEFAULT, |
||
| 70 | HttpRequest::ACCEPT_CHARSET => HttpRequest::ACCEPT_CHARSET_DEFAULT, |
||
| 71 | HttpRequest::ACCEPT_ENCODING => HttpRequest::ACCEPT_ENCODING_DEFAULT, |
||
| 72 | HttpRequest::USER_AGENT => HttpRequest::USER_AGENT_DEFAULT, |
||
| 73 | HttpRequest::REFERER => HttpRequest::REFERER_DEFAULT, |
||
| 74 | HttpRequest::REMOTE_IP => HttpRequest::REMOTE_IP_LOCAL, |
||
| 75 | HttpRequest::REMOTE_PORT => HttpRequest::REMOTE_PORT_DEFAULT, |
||
| 76 | ]; |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Returns HTTP root |
||
| 81 | * |
||
| 82 | * @return BasePathInterface |
||
| 83 | */ |
||
| 84 | public function getBasePath(): BasePathInterface |
||
| 85 | { |
||
| 86 | return $this->basePath; |
||
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @param BasePathInterface $basePath |
||
| 91 | * @return void |
||
| 92 | */ |
||
| 93 | protected function setBasePath(BasePathInterface $basePath): void |
||
| 94 | { |
||
| 95 | $this->basePath = $basePath; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns request URI |
||
| 100 | * |
||
| 101 | * @return RequestUriInterface |
||
| 102 | */ |
||
| 103 | public function getUri(): RequestUriInterface |
||
| 104 | { |
||
| 105 | return $this->requestUri; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Set request URI |
||
| 110 | * |
||
| 111 | * @param RequestUri $requestUri |
||
| 112 | * @return void |
||
| 113 | */ |
||
| 114 | protected function setUri(RequestUri $requestUri): void |
||
| 115 | { |
||
| 116 | $this->requestUri = $requestUri; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Returns request route path |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getRoutePath(): string |
||
| 125 | { |
||
| 126 | $basePath = (string) $this->getBasePath(); |
||
| 127 | $requestUri = (string) $this->getUri(); |
||
| 128 | $requestQuery = trim(substr($requestUri, strlen($basePath)), '/?&'); |
||
| 129 | return explode('&', $requestQuery ?? '', 2)[0] ?? ''; |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Returns executed script name. |
||
| 134 | * |
||
| 135 | * @return string |
||
| 136 | */ |
||
| 137 | public function getScriptName(): string |
||
| 138 | { |
||
| 139 | return $this[$this::SCRIPT_NAME] ?? ''; |
||
| 140 | } |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns server gateway interface |
||
| 144 | * |
||
| 145 | * @return string |
||
| 146 | */ |
||
| 147 | public function getGatewayInterface(): string |
||
| 148 | { |
||
| 149 | return $this[$this::GATEWAY_INTERFACE] ?? ''; |
||
| 150 | } |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Returns HTTP request method |
||
| 154 | * |
||
| 155 | * @return string |
||
| 156 | */ |
||
| 157 | public function getMethod(): string |
||
| 158 | { |
||
| 159 | return strtoupper($this[$this::METHOD]); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Returns HTTP host name |
||
| 164 | * |
||
| 165 | * @return string |
||
| 166 | */ |
||
| 167 | public function getHost(): string |
||
| 168 | { |
||
| 169 | return strtolower($this['SERVER_NAME'] ?? explode(':', $this[$this::HOST] ?? '')[0]); |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns HTTP host IP address |
||
| 174 | * |
||
| 175 | * @return string |
||
| 176 | */ |
||
| 177 | public function getHostIP(): string |
||
| 178 | { |
||
| 179 | return $this[$this::HOST_IP] ?? ''; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Returns HTTP request scheme |
||
| 184 | * |
||
| 185 | * @return string |
||
| 186 | */ |
||
| 187 | public function getScheme(): string |
||
| 188 | { |
||
| 189 | if (empty($this[$this::SCHEME])) { |
||
| 190 | $isHttps = 0 === strpos($this::SCHEME_HTTPS, strtolower($this['SERVER_PROTOCOL'])); |
||
| 191 | return strtolower($isHttps ? $this::SCHEME_HTTPS : $this::SCHEME_HTTP); |
||
| 192 | } |
||
| 193 | return strtolower($this[$this::SCHEME]); |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Returns true when request scheme is HTTPS |
||
| 198 | * |
||
| 199 | * @return bool |
||
| 200 | */ |
||
| 201 | public function isHttps(): bool |
||
| 202 | { |
||
| 203 | return $this->getScheme() === $this::SCHEME_HTTPS; |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Returns HTTP request port |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getPort(): string |
||
| 212 | { |
||
| 213 | return $this[$this::PORT] ?? $this::PORT_HTTP; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Returns HTTP request query string |
||
| 218 | * |
||
| 219 | * @return string |
||
| 220 | */ |
||
| 221 | public function getQueryString(): string |
||
| 222 | { |
||
| 223 | return $this[$this::QUERY_STRING] ?? ''; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Returns server software identifier |
||
| 228 | * |
||
| 229 | * @return string |
||
| 230 | */ |
||
| 231 | public function getServerSoftware(): string |
||
| 232 | { |
||
| 233 | return $this[$this::SERVER_SOFTWARE]; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 234 | } |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Returns HTTP accept header value |
||
| 238 | * |
||
| 239 | * @return string |
||
| 240 | */ |
||
| 241 | public function getAccept(): string |
||
| 242 | { |
||
| 243 | return $this[$this::ACCEPT] ?? ''; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Returns HTTP accept language header value. |
||
| 248 | * |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | public function getAcceptLanguage(): string |
||
| 252 | { |
||
| 253 | return $this[$this::ACCEPT_LANGUAGE] ?? ''; |
||
| 254 | } |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Returns HTTP accept charset header value |
||
| 258 | * |
||
| 259 | * @return string |
||
| 260 | */ |
||
| 261 | public function getAcceptCharset(): string |
||
| 262 | { |
||
| 263 | return $this[$this::ACCEPT_CHARSET] ?? ''; |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Returns HTTP accept encoding header value |
||
| 268 | * |
||
| 269 | * @return string |
||
| 270 | */ |
||
| 271 | public function getAcceptEncoding(): string |
||
| 272 | { |
||
| 273 | return $this[$this::ACCEPT_ENCODING] ?? ''; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Returns HTTP request user agent. |
||
| 278 | * |
||
| 279 | * @return string |
||
| 280 | */ |
||
| 281 | public function getUserAgent(): string |
||
| 282 | { |
||
| 283 | return $this[$this::USER_AGENT] ?? ''; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Returns HTTP referer header value |
||
| 288 | * |
||
| 289 | * @return string |
||
| 290 | */ |
||
| 291 | public function getReferer(): string |
||
| 292 | { |
||
| 293 | return $this[$this::REFERER] ?? ''; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns HTTP remote IP address. |
||
| 298 | * |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getRemoteIP(): string |
||
| 302 | { |
||
| 303 | return $this[$this::REMOTE_IP] ?? ''; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Returns HTTP remote port. |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | public function getRemotePort(): string |
||
| 312 | { |
||
| 313 | return $this[$this::REMOTE_PORT] ?? ''; |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Returns true when request was made by Ajax. |
||
| 318 | * |
||
| 319 | * @return bool |
||
| 320 | */ |
||
| 321 | public function isAjax(): bool |
||
| 322 | { |
||
| 323 | return $this[$this::REQUESTED_WITH] === $this::REQUESTED_WITH_AJAX; |
||
| 324 | } |
||
| 325 | } |
||
| 326 |