1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
<?php
namespace ICanBoogie\HTTP;
use ICanBoogie\Accessor\AccessorTrait;
class Status
{
use AccessorTrait;
const CONTINUE_ = 100;
const SWITCHING_PROTOCOLS = 101;
const OK = 200;
const CREATED = 201;
const ACCEPTED = 202;
const NON_AUTHORITATIVE_INFORMATION = 203;
const NO_CONTENT = 204;
const RESET_CONTENT = 205;
const PARTIAL_CONTENT = 206;
const MULTIPLE_CHOICES = 300;
const MOVED_PERMANENTLY = 301;
const FOUND = 302;
const SEE_OTHER = 303;
const NOT_MODIFIED = 304;
const USE_PROXY = 305;
const TEMPORARY_REDIRECT = 307;
const BAD_REQUEST = 400;
const UNAUTHORIZED = 401;
const PAYMENT_REQUIRED = 402;
const FORBIDDEN = 403;
const NOT_FOUND = 404;
const METHOD_NOT_ALLOWED = 405;
const NOT_ACCEPTABLE = 406;
const PROXY_AUTHENTICATION_REQUIRED = 407;
const REQUEST_TIMEOUT = 408;
const CONFLICT = 409;
const GONE = 410;
const LENGTH_REQUIRED = 411;
const PRECONDITION_FAILED = 412;
const REQUEST_ENTITY_TOO_LARGE = 413;
const REQUEST_URI_TOO_LONG = 414;
const UNSUPPORTED_MEDIA_TYPE = 415;
const REQUESTED_RANGE_NOT_SATISFIABLE = 416;
const EXPECTATION_FAILED = 417;
const I_M_A_TEAPOT = 418;
const INTERNAL_SERVER_ERROR = 500;
const NOT_IMPLEMENTED = 501;
const BAD_GATEWAY = 502;
const SERVICE_UNAVAILABLE = 503;
const GATEWAY_TIMEOUT = 504;
const HTTP_VERSION_NOT_SUPPORTED = 505;
static public $codes_and_messages = [
100 => "Continue",
101 => "Switching Protocols",
200 => "OK",
201 => "Created",
202 => "Accepted",
203 => "Non-Authoritative Information",
204 => "No Content",
205 => "Reset Content",
206 => "Partial Content",
300 => "Multiple Choices",
301 => "Moved Permanently",
302 => "Found",
303 => "See Other",
304 => "Not Modified",
305 => "Use Proxy",
307 => "Temporary Redirect",
400 => "Bad Request",
401 => "Unauthorized",
402 => "Payment Required",
403 => "Forbidden",
404 => "Not Found",
405 => "Method Not Allowed",
406 => "Not Acceptable",
407 => "Proxy Authentication Required",
408 => "Request Timeout",
409 => "Conflict",
410 => "Gone",
411 => "Length Required",
412 => "Precondition Failed",
413 => "Request Entity Too Large",
414 => "Request-URI Too Long",
415 => "Unsupported Media Type",
416 => "Requested Range Not Satisfiable",
417 => "Expectation Failed",
418 => "I'm a teapot",
500 => "Internal Server Error",
501 => "Not Implemented",
502 => "Bad Gateway",
503 => "Service Unavailable",
504 => "Gateway Timeout",
505 => "HTTP Version Not Supported"
];
static public function from($status)
{
if ($status instanceof self)
{
return $status;
}
$message = null;
if (is_array($status))
{
list($code, $message) = $status;
}
elseif (is_numeric($status))
{
$code = (int) $status;
}
else
{
if (!preg_match('/^(\d{3})\s+(.+)$/', $status, $matches))
{
throw new \InvalidArgumentException("Invalid status: $status.");
}
list(, $code, $message) = $matches;
}
return new static($code, $message);
}
static private function assert_code_is_valid($code)
{
if ($code >= 100 && $code < 600)
{
return;
}
throw new StatusCodeNotValid($code);
}
private $code;
protected function set_code($code)
{
self::assert_code_is_valid($code);
$this->code = $code;
}
protected function get_code()
{
return $this->code;
}
protected function get_is_valid()
{
return $this->code >= 100 && $this->code < 600;
}
protected function get_is_informational()
{
return $this->code >= 100 && $this->code < 200;
}
protected function get_is_successful()
{
return $this->code >= 200 && $this->code < 300;
}
protected function get_is_redirect()
{
return $this->code >= 300 && $this->code < 400;
}
protected function get_is_client_error()
{
return $this->code >= 400 && $this->code < 500;
}
protected function get_is_server_error()
{
return $this->code >= 500 && $this->code < 600;
}
protected function get_is_ok()
{
return $this->code == self::OK;
}
protected function get_is_forbidden()
{
return $this->code == self::FORBIDDEN;
}
protected function get_is_not_found()
{
return $this->code == self::NOT_FOUND;
}
protected function get_is_empty()
{
static $range = [
self::CREATED,
self::NO_CONTENT,
self::NOT_MODIFIED
];
return in_array($this->code, $range);
}
protected function get_is_cacheable()
{
static $range = [
self::OK,
self::NON_AUTHORITATIVE_INFORMATION,
self::MULTIPLE_CHOICES,
self::MOVED_PERMANENTLY,
self::FOUND,
self::NOT_FOUND,
self::GONE
];
return in_array($this->code, $range);
}
private $message;
protected function set_message($message)
{
$this->message = $message;
}
protected function get_message()
{
$message = $this->message;
$code = $this->code;
if (!$message && $code)
{
$message = self::$codes_and_messages[$code];
}
return $message;
}
public function __construct($code = self::OK, $message = null)
{
self::assert_code_is_valid($code);
$this->code = $code;
$this->message = $message ?: self::$codes_and_messages[$code];
}
public function __toString()
{
return "$this->code " . $this->get_message();
}
}