|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* Copyright (c) 2011-2015, Celestino Diaz <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
7
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
8
|
|
|
* in the Software without restriction, including without limitation the rights |
|
9
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
10
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
11
|
|
|
* furnished to do so, subject to the following conditions: |
|
12
|
|
|
* |
|
13
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
14
|
|
|
* all copies or substantial portions of the Software. |
|
15
|
|
|
* |
|
16
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
17
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
18
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
19
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
20
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
21
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
22
|
|
|
* THE SOFTWARE. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Brickoo\Component\Http; |
|
26
|
|
|
|
|
27
|
|
|
use Brickoo\Component\Http\Exception\StatusCodeUnknownException; |
|
28
|
|
|
use Brickoo\Component\Common\Assert; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* HttpStatusCode |
|
32
|
|
|
* |
|
33
|
|
|
* Implements the http status codes and phrases. |
|
34
|
|
|
* @author Celestino Diaz <[email protected]> |
|
35
|
|
|
*/ |
|
36
|
|
|
class HttpStatusCode { |
|
37
|
|
|
|
|
38
|
|
|
const CODE_CONTINUE = 100; |
|
39
|
|
|
const CODE_SWITCHING_PROTOCOLS = 101; |
|
40
|
|
|
const CODE_OK = 200; |
|
41
|
|
|
const CODE_CREATED = 201; |
|
42
|
|
|
const CODE_ACCEPTED = 202; |
|
43
|
|
|
const CODE_NON_AUTHORITATIVE_INFORMATION = 203; |
|
44
|
|
|
const CODE_NO_CONTENT = 204; |
|
45
|
|
|
const CODE_RESET_CONTENT = 205; |
|
46
|
|
|
const CODE_PARTIAL_CONTENT = 206; |
|
47
|
|
|
const CODE_MULTIPLE_CHOICES = 300; |
|
48
|
|
|
const CODE_MOVED_PERMANENTLY = 301; |
|
49
|
|
|
const CODE_FOUND = 302; |
|
50
|
|
|
const CODE_SEE_OTHER = 303; |
|
51
|
|
|
const CODE_NOT_MODIFIED = 304; |
|
52
|
|
|
const CODE_USE_PROXY = 305; |
|
53
|
|
|
const CODE_TEMPORARY_REDIRECT = 307; |
|
54
|
|
|
const CODE_PERMANENT_REDIRECT = 308; |
|
55
|
|
|
const CODE_BAD_REQUEST = 400; |
|
56
|
|
|
const CODE_UNAUTHORIZED = 401; |
|
57
|
|
|
const CODE_PAYMENT_REQUIRED = 402; |
|
58
|
|
|
const CODE_FORBIDDEN = 403; |
|
59
|
|
|
const CODE_NOT_FOUND = 404; |
|
60
|
|
|
const CODE_METHOD_NOT_ALLOWED = 405; |
|
61
|
|
|
const CODE_NOT_ACCEPTABLE = 406; |
|
62
|
|
|
const CODE_PROXY_AUTHENTICATION_REQUIRED = 407; |
|
63
|
|
|
const CODE_REQUEST_TIME_OUT = 408; |
|
64
|
|
|
const CODE_CONFLICT = 409; |
|
65
|
|
|
const CODE_GONE = 410; |
|
66
|
|
|
const CODE_LENGTH_REQUIRED = 411; |
|
67
|
|
|
const CODE_PRECONDITION_FAILED = 412; |
|
68
|
|
|
const CODE_REQUEST_ENTITY_TOO_LARGE = 413; |
|
69
|
|
|
const CODE_REQUEST_URI_TOO_LARGE = 414; |
|
70
|
|
|
const CODE_UNSUPPORTED_MEDIA_TYPE = 415; |
|
71
|
|
|
const CODE_REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
72
|
|
|
const CODE_EXPECTATION_FAILED = 417; |
|
73
|
|
|
const CODE_INTERNAL_SERVER_ERROR = 500; |
|
74
|
|
|
const CODE_NOT_IMPLEMENTED = 501; |
|
75
|
|
|
const CODE_BAD_GATEWAY = 502; |
|
76
|
|
|
const CODE_SERVICE_UNAVAILABLE = 503; |
|
77
|
|
|
const CODE_GATEWAY_TIME_OUT = 504; |
|
78
|
|
|
const CODE_HTTP_VERSION_NOT_SUPPORTED = 505; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Holds the corresponding status code phrases. |
|
82
|
|
|
* 1xx: Informational - Request received, continuing process |
|
83
|
|
|
* 2xx: Success - The action was successfully received, understood, and accepted |
|
84
|
|
|
* 3xx: Redirection - Further action must be taken in order to complete the request |
|
85
|
|
|
* 4xx: Client Error - The request contains bad syntax or cannot be fulfilled |
|
86
|
|
|
* 5xx: Server Error - The server failed to fulfill an apparently valid request |
|
87
|
|
|
* @link http://tools.ietf.org/html/rfc2616#page-40 |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected $statusPhrases = [ |
|
91
|
|
|
self::CODE_CONTINUE => "Continue", |
|
92
|
|
|
self::CODE_SWITCHING_PROTOCOLS => "Switching Protocols", |
|
93
|
|
|
self::CODE_OK => "OK", |
|
94
|
|
|
self::CODE_CREATED => "Created", |
|
95
|
|
|
self::CODE_ACCEPTED => "Accepted", |
|
96
|
|
|
self::CODE_NON_AUTHORITATIVE_INFORMATION => "Non-Authoritative Information", |
|
97
|
|
|
self::CODE_NO_CONTENT => "No Content", |
|
98
|
|
|
self::CODE_RESET_CONTENT => "Reset Content", |
|
99
|
|
|
self::CODE_PARTIAL_CONTENT => "Partial Content", |
|
100
|
|
|
self::CODE_MULTIPLE_CHOICES => "Multiple Choices", |
|
101
|
|
|
self::CODE_MOVED_PERMANENTLY => "Moved Permanently", |
|
102
|
|
|
self::CODE_FOUND => "Found", |
|
103
|
|
|
self::CODE_SEE_OTHER => "See Other", |
|
104
|
|
|
self::CODE_NOT_MODIFIED => "Not Modified", |
|
105
|
|
|
self::CODE_USE_PROXY => "Use Proxy", |
|
106
|
|
|
self::CODE_TEMPORARY_REDIRECT => "Temporary Redirect", |
|
107
|
|
|
self::CODE_PERMANENT_REDIRECT => "Permanent Redirect", |
|
108
|
|
|
self::CODE_BAD_REQUEST => "Bad Request", |
|
109
|
|
|
self::CODE_UNAUTHORIZED => "Unauthorized", |
|
110
|
|
|
self::CODE_PAYMENT_REQUIRED => "Payment Required", |
|
111
|
|
|
self::CODE_FORBIDDEN => "Forbidden", |
|
112
|
|
|
self::CODE_NOT_FOUND => "Not Found", |
|
113
|
|
|
self::CODE_METHOD_NOT_ALLOWED => "Method Not Allowed", |
|
114
|
|
|
self::CODE_NOT_ACCEPTABLE => "Not Acceptable", |
|
115
|
|
|
self::CODE_PROXY_AUTHENTICATION_REQUIRED => "Proxy Authentication Required", |
|
116
|
|
|
self::CODE_REQUEST_TIME_OUT => "Request Time-out", |
|
117
|
|
|
self::CODE_CONFLICT => "Conflict", |
|
118
|
|
|
self::CODE_GONE => "Gone", |
|
119
|
|
|
self::CODE_LENGTH_REQUIRED => "Length Required", |
|
120
|
|
|
self::CODE_PRECONDITION_FAILED => "Precondition Failed", |
|
121
|
|
|
self::CODE_REQUEST_ENTITY_TOO_LARGE => "Request Entity Too Large", |
|
122
|
|
|
self::CODE_REQUEST_URI_TOO_LARGE => "Request-URI Too Large", |
|
123
|
|
|
self::CODE_UNSUPPORTED_MEDIA_TYPE => "Unsupported Media Type", |
|
124
|
|
|
self::CODE_REQUESTED_RANGE_NOT_SATISFIABLE => "Requested range not satisfiable", |
|
125
|
|
|
self::CODE_EXPECTATION_FAILED => "Expectation Failed", |
|
126
|
|
|
self::CODE_INTERNAL_SERVER_ERROR => "Internal Server Error", |
|
127
|
|
|
self::CODE_NOT_IMPLEMENTED => "Not Implemented", |
|
128
|
|
|
self::CODE_BAD_GATEWAY => "Bad Gateway", |
|
129
|
|
|
self::CODE_SERVICE_UNAVAILABLE => "Service Unavailable", |
|
130
|
|
|
self::CODE_GATEWAY_TIME_OUT => "Gateway Time-out", |
|
131
|
|
|
self::CODE_HTTP_VERSION_NOT_SUPPORTED => "HTTP Version not supported" |
|
132
|
|
|
]; |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* Returns the phrase for the status code. |
|
136
|
|
|
* @param integer $statusCode |
|
137
|
|
|
* @throws \Brickoo\Component\Http\Exception\StatusCodeUnknownException |
|
138
|
|
|
* @return string the status code phrase |
|
139
|
|
|
*/ |
|
140
|
2 |
|
public function getPhrase($statusCode) { |
|
141
|
2 |
|
Assert::isInteger($statusCode); |
|
142
|
|
|
|
|
143
|
2 |
|
if (!$this->hasPhrase($statusCode)) { |
|
144
|
1 |
|
throw new StatusCodeUnknownException($statusCode); |
|
145
|
|
|
} |
|
146
|
1 |
|
return $this->statusPhrases[$statusCode]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Checks if the status code has a phrase. |
|
151
|
|
|
* @param integer $statusCode |
|
152
|
|
|
* @return boolean check result |
|
153
|
|
|
*/ |
|
154
|
3 |
|
public function hasPhrase($statusCode) { |
|
155
|
3 |
|
return array_key_exists($statusCode, $this->statusPhrases); |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
} |
|
159
|
|
|
|