ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilLTIConsumerServiceResponse.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
31 {
33  private int $code;
34 
36  private string $reason;
37 
39  private string $requestmethod;
40 
42  private string $accept;
43 
45  private string $contenttype;
46 
48  private string $data;
49 
51  private string $body;
52 
54  private array $responsecodes;
55 
57  private array $additionalheaders;
58 
62  public function __construct()
63  {
64  $this->code = 200;
65  $this->reason = '';
66  $this->requestmethod = $_SERVER['REQUEST_METHOD'];
67  $this->accept = '';
68  $this->contenttype = '';
69  $this->data = '';
70  $this->body = '';
71  $this->responsecodes = array(
72  200 => 'OK',
73  201 => 'Created',
74  202 => 'Accepted',
75  300 => 'Multiple Choices',
76  400 => 'Bad Request',
77  401 => 'Unauthorized',
78  402 => 'Payment Required',
79  403 => 'Forbidden',
80  404 => 'Not Found',
81  405 => 'Method Not Allowed',
82  406 => 'Not Acceptable',
83  415 => 'Unsupported Media Type',
84  500 => 'Internal Server Error',
85  501 => 'Not Implemented'
86  );
87  $this->additionalheaders = array();
88  }
89 
93  public function getCode(): int
94  {
95  return $this->code;
96  }
97 
101  public function setCode(int $code): void
102  {
103  $this->code = $code;
104  $this->reason = '';
105  }
106 
110  public function getReason(): string
111  {
112  $code = $this->code;
113  if (($code < 200) || ($code >= 600)) {
114  $code = 500; // Status code must be between 200 and 599.
115  }
116  if (empty($this->reason) && array_key_exists($code, $this->responsecodes)) {
117  $this->reason = $this->responsecodes[$code];
118  }
119  // Use generic reason for this category (based on first digit) if a specific reason is not defined.
120  if (empty($this->reason)) {
121  $this->reason = $this->responsecodes[intval($code / 100) * 100];
122  }
123  return $this->reason;
124  }
125 
129  public function setReason(string $reason): void
130  {
131  $this->reason = $reason;
132  }
133 
137  public function getRequestMethod(): string
138  {
139  return $this->requestmethod;
140  }
141 
145  public function getAccept(): string
146  {
147  return $this->accept;
148  }
149 
153  public function setAccept(string $accept): void
154  {
155  $this->accept = $accept;
156  }
157 
161  public function getContentType(): string
162  {
163  return $this->contenttype;
164  }
165 
169  public function setContentType(string $contenttype): void
170  {
171  $this->contenttype = $contenttype;
172  }
173 
177  public function getRequestData(): string
178  {
179  return $this->data;
180  }
181 
185  public function setRequestData(string $data): void
186  {
187  $this->data = $data;
188  }
189 
193  public function setBody(string $body): void
194  {
195  $this->body = $body;
196  }
197 
201  /*
202  public function add_additional_header(string $header): void {
203  $this->additionalheaders[] = $header;
204  }
205  */
209  public function send($debug = true): void
210  {
211  header("HTTP/1.0 {$this->code} {$this->getReason()}");
212  foreach ($this->additionalheaders as $header) {
213  header($header);
214  }
215  if ($debug) {
216  if ($this->code >= 200 && $this->code < 400) {
217  ilObjLTIConsumer::getLogger()->debug("$this->code {$this->getReason()}");
218  } else {
219  ilObjLTIConsumer::getLogger()->error("$this->code {$this->getReason()}");
220  }
221  }
222  if ((($this->code >= 200) && ($this->code < 300)) || !empty($this->body)) {
223  if (!empty($this->contenttype)) {
224  header("Content-Type: $this->contenttype; charset=utf-8");
225  }
226  if (!empty($this->body)) {
227  echo $this->body;
228  }
229  } elseif ($this->code >= 400) {
230  header("Content-Type: application/json; charset=utf-8");
231  $body = new stdClass();
232  $body->status = $this->code;
233  $body->reason = $this->getReason();
234  $body->request = new stdClass();
235  $body->request->method = $_SERVER['REQUEST_METHOD'];
236  $body->request->url = $_SERVER['REQUEST_URI'];
237  if (isset($_SERVER['HTTP_ACCEPT'])) {
238  $body->request->accept = $_SERVER['HTTP_ACCEPT'];
239  }
240  if (isset($_SERVER['CONTENT_TYPE'])) {
241  $body->request->contentType = explode(';', $_SERVER['CONTENT_TYPE'], 2)[0];
242  }
243  echo json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
244  }
245  }
246 }
setBody(string $body)
Set the response body.
getAccept()
Get the request accept header.
string $accept
HTTP request accept header.
$_SERVER['HTTP_HOST']
Definition: raiseError.php:10
array $additionalheaders
HTTP additional headers.
getContentType()
Get the response content type.
setCode(int $code)
Set the response code.
setReason(string $reason)
Set the response reason.
send($debug=true)
Add an additional header.
setAccept(string $accept)
Set the request accept header.
setRequestData(string $data)
Set the response body.
setContentType(string $contenttype)
Set the response content type.
string $contenttype
HTTP response content type.