|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SwaggerGen\Swagger; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Describes a Swagger Response object, containing anything that might be |
|
7
|
|
|
* returned from an API call, including code, description, data and headers. |
|
8
|
|
|
* |
|
9
|
|
|
* @package SwaggerGen |
|
10
|
|
|
* @author Martijn van der Lee <[email protected]> |
|
11
|
|
|
* @copyright 2014-2015 Martijn van der Lee |
|
12
|
|
|
* @license https://opensource.org/licenses/MIT MIT |
|
13
|
|
|
*/ |
|
14
|
|
|
class Response extends AbstractObject |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
const OK = 200; |
|
18
|
|
|
const CREATED = 201; |
|
19
|
|
|
const ACCEPTED = 202; |
|
20
|
|
|
const NON_AUTHORITATIVE_INFORMATION = 203; |
|
21
|
|
|
const NO_CONTENT = 204; |
|
22
|
|
|
const RESET_CONTENT = 205; |
|
23
|
|
|
const PARTIAL_CONTENT = 206; |
|
24
|
|
|
const BAD_REQUEST = 400; |
|
25
|
|
|
const UNAUTHORIZED = 401; |
|
26
|
|
|
const PAYMENT_REQUIRED = 402; |
|
27
|
|
|
const FORBIDDEN = 403; |
|
28
|
|
|
const NOT_FOUND = 404; |
|
29
|
|
|
const METHOD_NOT_ALLOWED = 405; |
|
30
|
|
|
const NOT_ACCEPTABLE = 406; |
|
31
|
|
|
const PROXY_AUTHENTICATION_REQUIRED = 407; |
|
32
|
|
|
const REQUEST_TIMEOUT = 408; |
|
33
|
|
|
const CONFLICT = 409; |
|
34
|
|
|
const GONE = 410; |
|
35
|
|
|
const LENGTH_REQUIRED = 411; |
|
36
|
|
|
const PRECONDITION_FAILED = 412; |
|
37
|
|
|
const REQUEST_ENTITY_TOO_LARGE = 413; |
|
38
|
|
|
const REQUEST_URI_TOO_LONG = 414; |
|
39
|
|
|
const UNSUPPORTED_MEDIA_TYPE = 415; |
|
40
|
|
|
const REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
41
|
|
|
const EXPECTATION_FAILED = 417; |
|
42
|
|
|
const UNPROCESSABLE_ENTITY = 422; |
|
43
|
|
|
const TOO_MANY_REQUESTS = 429; |
|
44
|
|
|
const INTERNAL_SERVER_ERROR = 500; |
|
45
|
|
|
const NOT_IMPLEMENTED = 501; // When method is supported for none of the resources |
|
46
|
|
|
|
|
47
|
|
|
protected static $httpCodes = array( |
|
48
|
|
|
self::OK => "OK", |
|
49
|
|
|
self::CREATED => "Created", |
|
50
|
|
|
self::ACCEPTED => "Accepted", |
|
51
|
|
|
self::NON_AUTHORITATIVE_INFORMATION => "Non-Authoritative Information", |
|
52
|
|
|
self::NO_CONTENT => "No Content", |
|
53
|
|
|
self::RESET_CONTENT => "Reset Content", |
|
54
|
|
|
self::PARTIAL_CONTENT => "Partial Content", |
|
55
|
|
|
self::BAD_REQUEST => "Bad Request", |
|
56
|
|
|
self::UNAUTHORIZED => "Unauthorized", |
|
57
|
|
|
self::PAYMENT_REQUIRED => "Payment Required", |
|
58
|
|
|
self::FORBIDDEN => "Forbidden", |
|
59
|
|
|
self::NOT_FOUND => "Not Found", |
|
60
|
|
|
self::METHOD_NOT_ALLOWED => "Method Not Allowed", |
|
61
|
|
|
self::NOT_ACCEPTABLE => "Not Acceptable", |
|
62
|
|
|
self::PROXY_AUTHENTICATION_REQUIRED => "Proxy Authentication Required", |
|
63
|
|
|
self::REQUEST_TIMEOUT => "Request Timeout", |
|
64
|
|
|
self::CONFLICT => "Conflict", |
|
65
|
|
|
self::GONE => "Gone", |
|
66
|
|
|
self::LENGTH_REQUIRED => 'Length Required', |
|
67
|
|
|
self::PRECONDITION_FAILED => 'Precondition Failed', |
|
68
|
|
|
self::REQUEST_ENTITY_TOO_LARGE => 'Request Entity Too Large', |
|
69
|
|
|
self::REQUEST_URI_TOO_LONG => 'Request-URI Too Long', |
|
70
|
|
|
self::UNSUPPORTED_MEDIA_TYPE => "Unsupported Media Type", |
|
71
|
|
|
self::REQUESTED_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable', |
|
72
|
|
|
self::EXPECTATION_FAILED => 'Expectation Failed', |
|
73
|
|
|
self::UNPROCESSABLE_ENTITY => "Unprocessable Entity", |
|
74
|
|
|
self::TOO_MANY_REQUESTS => "Too Many Requests", |
|
75
|
|
|
self::INTERNAL_SERVER_ERROR => "Internal Server Error", |
|
76
|
|
|
self::NOT_IMPLEMENTED => "Not Implemented", |
|
77
|
|
|
); |
|
78
|
|
|
private $description = ''; |
|
79
|
|
|
private $schema; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* @var Header[] |
|
83
|
|
|
*/ |
|
84
|
|
|
private $Headers = array(); |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* JSON examples |
|
88
|
|
|
* @var array |
|
89
|
|
|
*/ |
|
90
|
|
|
private $examples = array(); |
|
91
|
|
|
|
|
92
|
|
|
public static function getCode($search) |
|
93
|
|
|
{ |
|
94
|
|
|
static $lookup = null; |
|
95
|
|
|
|
|
96
|
|
|
if (is_numeric($search)) { |
|
97
|
|
|
return intval($search); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// build static lookup table |
|
101
|
|
|
if (!$lookup) { |
|
102
|
|
|
$lookup = array(); |
|
103
|
|
|
foreach (self::$httpCodes as $code => $text) { |
|
104
|
|
|
$lookup[preg_replace('/[^a-z]+/', '', strtolower($text))] = $code; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$search = preg_replace('/[^a-z]+/', '', strtolower($search)); |
|
109
|
|
|
return isset($lookup[$search]) ? $lookup[$search] : null; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
public function __construct(AbstractObject $parent, $code, $definition = null, $description = null) |
|
113
|
|
|
{ |
|
114
|
|
|
parent::__construct($parent); |
|
115
|
|
|
|
|
116
|
|
|
if ($definition) { |
|
117
|
|
|
$this->schema = new Schema($this, $definition); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
if (!empty($description)) { |
|
121
|
|
|
$this->description = $description; |
|
122
|
|
|
} elseif (isset(self::$httpCodes[$code])) { |
|
123
|
|
|
$this->description = self::$httpCodes[$code]; |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* @param string $command |
|
129
|
|
|
* @param string $data |
|
130
|
|
|
* @return \SwaggerGen\Swagger\AbstractObject|boolean |
|
131
|
|
|
*/ |
|
132
|
|
|
public function handleCommand($command, $data = null) |
|
133
|
|
|
{ |
|
134
|
|
|
switch (strtolower($command)) { |
|
135
|
|
|
case 'header': |
|
136
|
|
|
$type = self::wordShift($data); |
|
137
|
|
|
if (empty($type)) { |
|
138
|
|
|
throw new \SwaggerGen\Exception("Missing type for header"); |
|
139
|
|
|
} |
|
140
|
|
|
$name = self::wordShift($data); |
|
141
|
|
|
if (empty($name)) { |
|
142
|
|
|
throw new \SwaggerGen\Exception("Missing name for header type '{$type}'"); |
|
143
|
|
|
} |
|
144
|
|
|
$Header = new Header($this, $type, $data); |
|
145
|
|
|
$this->Headers[$name] = $Header; |
|
146
|
|
|
return $Header; |
|
147
|
|
|
|
|
148
|
|
|
case 'example': |
|
149
|
|
|
$name = self::wordShift($data); |
|
150
|
|
|
if (empty($name)) { |
|
151
|
|
|
throw new \SwaggerGen\Exception("Missing name for example"); |
|
152
|
|
|
} |
|
153
|
|
|
if ($data === '') { |
|
154
|
|
|
throw new \SwaggerGen\Exception("Missing content for example `{$name}`"); |
|
155
|
|
|
} |
|
156
|
|
|
$json = preg_replace_callback('/([^{}:]+)/', function($match) { |
|
157
|
|
|
json_decode($match[1]); |
|
158
|
|
|
return json_last_error() === JSON_ERROR_NONE ? $match[1] : json_encode($match[1]); |
|
159
|
|
|
}, trim($data)); |
|
160
|
|
|
$this->examples[$name] = json_decode($json, true); |
|
161
|
|
|
return $this; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
return parent::handleCommand($command, $data); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
public function toArray() |
|
168
|
|
|
{ |
|
169
|
|
|
return self::arrayFilterNull(array_merge(array( |
|
170
|
|
|
'description' => $this->description, |
|
171
|
|
|
'schema' => $this->schema ? $this->schema->toArray() : null, |
|
172
|
|
|
'headers' => self::objectsToArray($this->Headers), |
|
173
|
|
|
'examples' => $this->examples, |
|
174
|
|
|
), parent::toArray())); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
public function __toString() |
|
178
|
|
|
{ |
|
179
|
|
|
return __CLASS__; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|