diff --git a/src/Request.php b/src/Request.php new file mode 100644 index 0000000..1b0918d --- /dev/null +++ b/src/Request.php @@ -0,0 +1,71 @@ +worker = $worker; + $this->query = $query; + } + + public function getWorker() + { + return $this->worker; + } + + public static function fromData(array $data) + { + $request = new ServerRequest($data['Method'], $data['Uri']); + + if (!empty($data['Headers'])) { + foreach ($data['Headers'] as $header => $value) { + $request = $request->withHeader($header, $value); + } + } + + foreach (['ProtocolVersion', 'UploadedFiles', 'CookieParams', 'QueryParams', 'ParsedBody'] as $name) { + $value = $data[$name]; + if (!empty($value)) { + $request = $request->{'with' . $name}($value); + } + } + + return new static($request, $data['query']); + } + + public function getProfile() + { + /// TODO serialize request object for profile + $request = $this->worker; + $body = array_merge($request->getParsedBody(), $request->getQueryParams()); + + return $request->getMethod() . ' ' . $request->getUri() . '#' . http_build_query($body); + } + + public function isRaw() + { + return !empty($this->query->options['raw']); + } +} diff --git a/src/Response.php b/src/Response.php new file mode 100644 index 0000000..1bd5052 --- /dev/null +++ b/src/Response.php @@ -0,0 +1,83 @@ +worker = $worker; + $this->request = $request; + $this->init(); + } + + public function getWorker() + { + return $this->worker; + } + + public function getRequest() + { + return $this->request; + } + + public function getData() + { + return $this->data; + } + + public function init() + { + $this->data = $this->getBodyContents(); + if (!$this->isRaw() && $this->isJson()) { + $this->data = Json::decode($this->data); + } + } + + public function isRaw() + { + return $this->request->isRaw(); + } + + public function isJson() + { + return preg_grep('|application/json|i', $this->getHeader('Content-Type')); + } + + public function getHeader($name) + { + return $this->worker->getHeader($name); + } + + public function getBodyContents() + { + return $this->worker->getBody()->getContents(); + } + +}