|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Helper class for dealing with HTTP status codes. |
| 4 | + * |
| 5 | + * @package Requests\Utilities |
| 6 | + */ |
| 7 | + |
| 8 | +namespace WpOrg\Requests\Utility; |
| 9 | + |
| 10 | +use WpOrg\Requests\Exception\InvalidArgument; |
| 11 | + |
| 12 | +/** |
| 13 | + * HTTP status codes helper class. |
| 14 | + * |
| 15 | + * @package Requests\Utilities |
| 16 | + */ |
| 17 | +final class HttpStatus { |
| 18 | + const STATUS_MESSAGE_100 = 'Continue'; |
| 19 | + const STATUS_MESSAGE_101 = 'Switching Protocols'; |
| 20 | + const STATUS_MESSAGE_200 = 'OK'; |
| 21 | + const STATUS_MESSAGE_201 = 'Created'; |
| 22 | + const STATUS_MESSAGE_202 = 'Accepted'; |
| 23 | + const STATUS_MESSAGE_203 = 'Non-Authoritative Information'; |
| 24 | + const STATUS_MESSAGE_204 = 'No Content'; |
| 25 | + const STATUS_MESSAGE_205 = 'Reset Content'; |
| 26 | + const STATUS_MESSAGE_206 = 'Partial Content'; |
| 27 | + const STATUS_MESSAGE_300 = 'Multiple Choices'; |
| 28 | + const STATUS_MESSAGE_301 = 'Moved Permanently'; |
| 29 | + const STATUS_MESSAGE_302 = 'Found'; |
| 30 | + const STATUS_MESSAGE_303 = 'See Other'; |
| 31 | + const STATUS_MESSAGE_304 = 'Not Modified'; |
| 32 | + const STATUS_MESSAGE_305 = 'Use Proxy'; |
| 33 | + const STATUS_MESSAGE_306 = '(Unused)'; |
| 34 | + const STATUS_MESSAGE_307 = 'Temporary Redirect'; |
| 35 | + const STATUS_MESSAGE_400 = 'Bad Request'; |
| 36 | + const STATUS_MESSAGE_401 = 'Unauthorized'; |
| 37 | + const STATUS_MESSAGE_402 = 'Payment Required'; |
| 38 | + const STATUS_MESSAGE_403 = 'Forbidden'; |
| 39 | + const STATUS_MESSAGE_404 = 'Not Found'; |
| 40 | + const STATUS_MESSAGE_405 = 'Method Not Allowed'; |
| 41 | + const STATUS_MESSAGE_406 = 'Not Acceptable'; |
| 42 | + const STATUS_MESSAGE_407 = 'Proxy Authentication Required'; |
| 43 | + const STATUS_MESSAGE_408 = 'Request Timeout'; |
| 44 | + const STATUS_MESSAGE_409 = 'Conflict'; |
| 45 | + const STATUS_MESSAGE_410 = 'Gone'; |
| 46 | + const STATUS_MESSAGE_411 = 'Length Required'; |
| 47 | + const STATUS_MESSAGE_412 = 'Precondition Failed'; |
| 48 | + const STATUS_MESSAGE_413 = 'Request Entity Too Large'; |
| 49 | + const STATUS_MESSAGE_414 = 'Request-URI Too Long'; |
| 50 | + const STATUS_MESSAGE_415 = 'Unsupported Media Type'; |
| 51 | + const STATUS_MESSAGE_416 = 'Requested Range Not Satisfiable'; |
| 52 | + const STATUS_MESSAGE_417 = 'Expectation Failed'; |
| 53 | + const STATUS_MESSAGE_418 = 'I\'m a teapot'; |
| 54 | + const STATUS_MESSAGE_428 = 'Precondition Required'; |
| 55 | + const STATUS_MESSAGE_429 = 'Too Many Requests'; |
| 56 | + const STATUS_MESSAGE_431 = 'Request Header Fields Too Large'; |
| 57 | + const STATUS_MESSAGE_500 = 'Internal Server Error'; |
| 58 | + const STATUS_MESSAGE_501 = 'Not Implemented'; |
| 59 | + const STATUS_MESSAGE_502 = 'Bad Gateway'; |
| 60 | + const STATUS_MESSAGE_503 = 'Service Unavailable'; |
| 61 | + const STATUS_MESSAGE_504 = 'Gateway Timeout'; |
| 62 | + const STATUS_MESSAGE_505 = 'HTTP Version Not Supported'; |
| 63 | + const STATUS_MESSAGE_511 = 'Network Authentication Required'; |
| 64 | + |
| 65 | + const STATUS_100 = '100 ' . self::STATUS_MESSAGE_100; |
| 66 | + const STATUS_101 = '101 ' . self::STATUS_MESSAGE_101; |
| 67 | + const STATUS_200 = '200 ' . self::STATUS_MESSAGE_200; |
| 68 | + const STATUS_201 = '201 ' . self::STATUS_MESSAGE_201; |
| 69 | + const STATUS_202 = '202 ' . self::STATUS_MESSAGE_202; |
| 70 | + const STATUS_203 = '203 ' . self::STATUS_MESSAGE_203; |
| 71 | + const STATUS_204 = '204 ' . self::STATUS_MESSAGE_204; |
| 72 | + const STATUS_205 = '205 ' . self::STATUS_MESSAGE_205; |
| 73 | + const STATUS_206 = '206 ' . self::STATUS_MESSAGE_206; |
| 74 | + const STATUS_300 = '300 ' . self::STATUS_MESSAGE_300; |
| 75 | + const STATUS_301 = '301 ' . self::STATUS_MESSAGE_301; |
| 76 | + const STATUS_302 = '302 ' . self::STATUS_MESSAGE_302; |
| 77 | + const STATUS_303 = '303 ' . self::STATUS_MESSAGE_303; |
| 78 | + const STATUS_304 = '304 ' . self::STATUS_MESSAGE_304; |
| 79 | + const STATUS_305 = '305 ' . self::STATUS_MESSAGE_305; |
| 80 | + const STATUS_306 = '306 ' . self::STATUS_MESSAGE_306; |
| 81 | + const STATUS_307 = '307 ' . self::STATUS_MESSAGE_307; |
| 82 | + const STATUS_400 = '400 ' . self::STATUS_MESSAGE_400; |
| 83 | + const STATUS_401 = '401 ' . self::STATUS_MESSAGE_401; |
| 84 | + const STATUS_402 = '402 ' . self::STATUS_MESSAGE_402; |
| 85 | + const STATUS_403 = '403 ' . self::STATUS_MESSAGE_403; |
| 86 | + const STATUS_404 = '404 ' . self::STATUS_MESSAGE_404; |
| 87 | + const STATUS_405 = '405 ' . self::STATUS_MESSAGE_405; |
| 88 | + const STATUS_406 = '406 ' . self::STATUS_MESSAGE_406; |
| 89 | + const STATUS_407 = '407 ' . self::STATUS_MESSAGE_407; |
| 90 | + const STATUS_408 = '408 ' . self::STATUS_MESSAGE_408; |
| 91 | + const STATUS_409 = '409 ' . self::STATUS_MESSAGE_409; |
| 92 | + const STATUS_410 = '410 ' . self::STATUS_MESSAGE_410; |
| 93 | + const STATUS_411 = '411 ' . self::STATUS_MESSAGE_411; |
| 94 | + const STATUS_412 = '412 ' . self::STATUS_MESSAGE_412; |
| 95 | + const STATUS_413 = '413 ' . self::STATUS_MESSAGE_413; |
| 96 | + const STATUS_414 = '414 ' . self::STATUS_MESSAGE_414; |
| 97 | + const STATUS_415 = '415 ' . self::STATUS_MESSAGE_415; |
| 98 | + const STATUS_416 = '416 ' . self::STATUS_MESSAGE_416; |
| 99 | + const STATUS_417 = '417 ' . self::STATUS_MESSAGE_417; |
| 100 | + const STATUS_418 = '418 ' . self::STATUS_MESSAGE_418; |
| 101 | + const STATUS_428 = '428 ' . self::STATUS_MESSAGE_428; |
| 102 | + const STATUS_429 = '429 ' . self::STATUS_MESSAGE_429; |
| 103 | + const STATUS_431 = '431 ' . self::STATUS_MESSAGE_431; |
| 104 | + const STATUS_500 = '500 ' . self::STATUS_MESSAGE_500; |
| 105 | + const STATUS_501 = '501 ' . self::STATUS_MESSAGE_501; |
| 106 | + const STATUS_502 = '502 ' . self::STATUS_MESSAGE_502; |
| 107 | + const STATUS_503 = '503 ' . self::STATUS_MESSAGE_503; |
| 108 | + const STATUS_504 = '504 ' . self::STATUS_MESSAGE_504; |
| 109 | + const STATUS_505 = '505 ' . self::STATUS_MESSAGE_505; |
| 110 | + const STATUS_511 = '511 ' . self::STATUS_MESSAGE_511; |
| 111 | + |
| 112 | + /** |
| 113 | + * Get the status protocol from a status line. |
| 114 | + * |
| 115 | + * Example: |
| 116 | + * HTTP/1.1 404 Not Found |
| 117 | + * \______/ |
| 118 | + * ^--- this is the status protocol. |
| 119 | + * |
| 120 | + * @param string $status_line Status line to parse. |
| 121 | + * @return string Status protocol. |
| 122 | + */ |
| 123 | + public static function get_protocol_from_line($status_line) { |
| 124 | + if (!is_string($status_line)) { |
| 125 | + throw InvalidArgument::create(1, '$status_line', 'string', gettype($status_line)); |
| 126 | + } |
| 127 | + |
| 128 | + $parts = explode(' ', $status_line, 3); |
| 129 | + return $parts[0]; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Get the status code from a status line. |
| 134 | + * |
| 135 | + * Example: |
| 136 | + * HTTP/1.1 404 Not Found |
| 137 | + * \_/ |
| 138 | + * ^--- this is the status code. |
| 139 | + * |
| 140 | + * @param string $status_line Status line to parse. |
| 141 | + * @return int Status code. |
| 142 | + */ |
| 143 | + public static function get_code_from_line($status_line) { |
| 144 | + if (!is_string($status_line)) { |
| 145 | + throw InvalidArgument::create(1, '$status_line', 'string', gettype($status_line)); |
| 146 | + } |
| 147 | + |
| 148 | + $parts = explode(' ', $status_line, 3); |
| 149 | + return (int) $parts[1]; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Get the status message from a status line. |
| 154 | + * |
| 155 | + * Example: |
| 156 | + * HTTP/1.1 404 Not Found |
| 157 | + * \_______/ |
| 158 | + * ^--- this is the status message. |
| 159 | + * |
| 160 | + * @param string $status_line Status line to parse. |
| 161 | + * @return string Status message. |
| 162 | + */ |
| 163 | + public static function get_message_from_line($status_line) { |
| 164 | + if (!is_string($status_line)) { |
| 165 | + throw InvalidArgument::create(1, '$status_line', 'string', gettype($status_line)); |
| 166 | + } |
| 167 | + |
| 168 | + $parts = explode(' ', $status_line, 3); |
| 169 | + return $parts[2]; |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Get the status line from a status code and message. |
| 174 | + * |
| 175 | + * You can also pass in a protocol to use, the default is HTTP/1.1. |
| 176 | + * |
| 177 | + * @param int $code Status code. |
| 178 | + * @param string $message Status message. |
| 179 | + * @param string $protocol Protocol to use (default: HTTP/1.1). |
| 180 | + * @return string Status line. |
| 181 | + */ |
| 182 | + public static function get_line($code, $message, $protocol = 'HTTP/1.1') { |
| 183 | + if (!is_string($code)) { |
| 184 | + throw InvalidArgument::create(1, '$code', 'string', gettype($code)); |
| 185 | + } |
| 186 | + |
| 187 | + if (!is_string($message)) { |
| 188 | + throw InvalidArgument::create(2, '$message', 'string', gettype($message)); |
| 189 | + } |
| 190 | + |
| 191 | + if (!is_string($protocol)) { |
| 192 | + throw InvalidArgument::create(3, '$protocol', 'string', gettype($protocol)); |
| 193 | + } |
| 194 | + |
| 195 | + return "$protocol $code $message"; |
| 196 | + } |
| 197 | + |
| 198 | + /** |
| 199 | + * Get the status message from a status code. |
| 200 | + * |
| 201 | + * @param int $code Status code. |
| 202 | + * @return string Status message. |
| 203 | + */ |
| 204 | + public static function get_message($code) { |
| 205 | + if (!is_int($code)) { |
| 206 | + throw InvalidArgument::create(1, '$code', 'int', gettype($code)); |
| 207 | + } |
| 208 | + |
| 209 | + return constant('self::STATUS_MESSAGE_' . $code); |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + /** |
| 214 | + * Get the status message with including code from a status code. |
| 215 | + * |
| 216 | + * @param int $code Status code. |
| 217 | + * @return string Status. |
| 218 | + */ |
| 219 | + public static function get_message_with_code($code) { |
| 220 | + if (!is_int($code)) { |
| 221 | + throw InvalidArgument::create(1, '$code', 'int', gettype($code)); |
| 222 | + } |
| 223 | + |
| 224 | + return "{$code} " . self::get_message($code); |
| 225 | + } |
| 226 | +} |
0 commit comments