1 | <?php |
||
33 | class ResponseClass |
||
34 | { |
||
35 | const INFORMATIONAL = 'INFORMATIONAL'; |
||
36 | const SUCCESS = 'SUCCESSFUL'; |
||
37 | const REDIRECTION = 'REDIRECTION'; |
||
38 | const CLIENT_ERROR = 'CLIENT_ERROR'; |
||
39 | const SERVER_ERROR = 'SERVER_ERROR'; |
||
40 | |||
41 | /** |
||
42 | * Classes |
||
43 | * |
||
44 | * @var array |
||
45 | * |
||
46 | * @access protected |
||
47 | */ |
||
48 | protected $classes = [ |
||
49 | 1 => self::INFORMATIONAL, |
||
50 | 2 => self::SUCCESS, |
||
51 | 3 => self::REDIRECTION, |
||
52 | 4 => self::CLIENT_ERROR, |
||
53 | 5 => self::SERVER_ERROR, |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Get the classification for an HTTP Status Code |
||
58 | * |
||
59 | * @param int|Response $code HTTP Status Code |
||
60 | * |
||
61 | * @return string |
||
62 | * |
||
63 | * @access public |
||
64 | */ |
||
65 | 2001 | public function getClass($code) |
|
71 | |||
72 | /** |
||
73 | * Get a valid HTTP Status Code |
||
74 | * |
||
75 | * @param Response|int $code HTTP Status Code |
||
76 | * |
||
77 | * @return int |
||
78 | * @throws Exception if invalid HTTP Status Code |
||
79 | * |
||
80 | * @access protected |
||
81 | */ |
||
82 | 2001 | protected function fixCode($code) |
|
83 | { |
||
84 | 2001 | if ($code instanceof Response) { |
|
85 | 1000 | $code = $code->getStatusCode(); |
|
86 | 1000 | } |
|
87 | |||
88 | 2001 | $this->assertValidHttpCode($code); |
|
89 | |||
90 | 2000 | return $code; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * Assert Valid HTTP Code |
||
95 | * |
||
96 | * @param int $code HTTP Status Code |
||
97 | * |
||
98 | * @return null |
||
99 | * @throws Exception if invalid HTTP Status Code; |
||
100 | * |
||
101 | * @access protected |
||
102 | */ |
||
103 | 2001 | protected function assertValidHttpCode($code) |
|
104 | { |
||
105 | 2001 | if (! is_numeric($code) |
|
106 | 2001 | || is_float($code) |
|
107 | 2001 | || $code < 100 |
|
108 | 2001 | || $code >= 600 |
|
109 | 2001 | ) { |
|
110 | |||
111 | 1 | $msg = sprintf( |
|
112 | 'Invalid status code "%s"; ' |
||
113 | 1 | . 'must be an integer between 100 and 599, inclusive', |
|
114 | 1 | (is_scalar($code) ? $code : gettype($code)) |
|
115 | 1 | ); |
|
116 | |||
117 | 1 | throw new Exception($msg); |
|
118 | } |
||
119 | 2000 | } |
|
120 | |||
121 | /** |
||
122 | * Is Response of given class? |
||
123 | * |
||
124 | * @param string $class class to test for |
||
125 | * @param int|Response $code code to test |
||
126 | * |
||
127 | * @return bool |
||
128 | * |
||
129 | * @access public |
||
130 | */ |
||
131 | 1000 | public function isResponse($class, $code) |
|
135 | |||
136 | /** |
||
137 | * Is Status Informational? |
||
138 | * |
||
139 | * @param int|Response $code HTTP Status Code |
||
140 | * |
||
141 | * @return bool |
||
142 | * |
||
143 | * @access public |
||
144 | */ |
||
145 | 1000 | public function isInformational($code) |
|
149 | |||
150 | /** |
||
151 | * Is Status a Success? |
||
152 | * |
||
153 | * @param int|Response $code HTTP Status Code |
||
154 | * |
||
155 | * @return bool |
||
156 | * |
||
157 | * @access public |
||
158 | */ |
||
159 | 1000 | public function isSuccess($code) |
|
163 | |||
164 | /** |
||
165 | * Is Status a Redirect? |
||
166 | * |
||
167 | * @param int|Response $code HTTP Status Code |
||
168 | * |
||
169 | * @return bool |
||
170 | * |
||
171 | * @access public |
||
172 | */ |
||
173 | 1000 | public function isRedirection($code) |
|
177 | |||
178 | /** |
||
179 | * Is Status a Client Error? |
||
180 | * |
||
181 | * @param int|Response $code HTTP Status Code |
||
182 | * |
||
183 | * @return bool |
||
184 | * |
||
185 | * @access public |
||
186 | */ |
||
187 | 1000 | public function isClientError($code) |
|
191 | |||
192 | /** |
||
193 | * Is Status a Server Error? |
||
194 | * |
||
195 | * @param int|Response $code HTTP Status Code |
||
196 | * |
||
197 | * @return bool |
||
198 | * |
||
199 | * @access public |
||
200 | */ |
||
201 | 1000 | public function isServerError($code) |
|
205 | } |
||
206 |