From 43692060533c24cd50d59aff526520f8c2927f44 Mon Sep 17 00:00:00 2001 From: Andrii Vasyliev Date: Thu, 19 Jan 2017 16:14:59 +0000 Subject: [PATCH] + serialization in Request --- src/Request.php | 157 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 136 insertions(+), 21 deletions(-) diff --git a/src/Request.php b/src/Request.php index 043a057..8955c2f 100644 --- a/src/Request.php +++ b/src/Request.php @@ -12,8 +12,10 @@ use GuzzleHttp\Psr7\Request as Worker; -class Request +class Request implements \Serializable { + protected $builder; + /** * @var Worker */ @@ -24,14 +26,65 @@ class Request */ protected $query; - public function __construct(Worker $worker, Query $query) + /** + * @var string Connection name + */ + protected $dbname; + + /** + * @var array request method + */ + protected $method; + protected $uri; + protected $headers = []; + protected $body; + protected $version; + + public function __construct(QueryBuilder $builder, Query $query) { - $this->worker = $worker; + $this->builder = $builder; $this->query = $query; } + public function getDbname() + { + return $this->dbname; + } + + public function getMethod() + { + return $this->method; + } + + public function getUri() + { + return $this->uri; + } + + public function getHeaders() + { + return $this->headers; + } + + public function getBody() + { + return $this->body; + } + + public function getVersion() + { + return $this->version; + } + public function getWorker() { + if ($this->worker === null) { + if (!empty($this->query)) { + $this->updateFromQuery(); + } + $this->worker = $this->createWorker(); + } + return $this->worker; } @@ -39,40 +92,102 @@ public function getQuery() { return $this->query; } - public static function fromData(array $data) + + protected function updateFromQuery() + { + $this->builder->prepare($this->query); + + $this->buildDbname(); + $this->buildAuth(); + $this->buildMethod(); + $this->buildUri(); + $this->buildQueryParams(); + $this->buildHeaders(); + $this->buildBody(); + $this->buildFormParams(); + $this->buildProtocolVersion(); + } + + protected function createWorker() { - $uri = $data['Uri']; + return new Worker($this->method, $this->uri, $this->headers, $this->body, $this->version); + } - $params = $data['QueryParams']; + protected function buildDbname() + { + $this->dbname = $this->builder->db->name; + } + + protected function buildAuth() + { + $this->builder->buildAuth($this->query); + } + + protected function buildMethod() + { + $this->method = $this->builder->buildMethod($this->query) ?: 'GET'; + } + + protected function buildUri() + { + $this->uri = $this->builder->buildUri($this->query); + } + + protected function buildQueryParams() + { + $params = $this->builder->buildQueryParams($this->query); if (is_array($params)) { $params = http_build_query($params, '', '&'); } if (!empty($params)) { - $uri .= '?' . $params; + $this->uri .= '?' . $params; } + } - $headers = $data['headers']; - $body = $data['Body']; + protected function buildHeaders() + { + $this->headers = $this->builder->buildHeaders($this->query); + } - $formParams = $data['FormParams']; - if (is_array($formParams)) { - $body = http_build_query($formParams, '', '&'); - $headers['Content-Type'] = 'application/x-www-form-urlencoded'; - } + protected function buildBody() + { + $this->body = $this->builder->buildBody($this->query); + } - $version = empty($data['ProtocolVersion']) ? '1.1' : $data['ProtocolVersion']; + protected function buildFormParams() + { + $this->setFormParams($this->builder->buildFormParams($this->query)); + } - $request = new Worker($data['Method'], $uri, $headers, $body, $version); + protected function setFormParams($params) + { + if (!empty($params)) { + $this->body = is_array($params) ? http_build_query($params, '', '&') : $params; + $this->headers['Content-Type'] = 'application/x-www-form-urlencoded'; + } + } - return new static($request, $data['query']); + protected function buildProtocolVersion() + { + $this->version = $this->builder->buildProtocolVersion($this->query) ?: '1.1'; } - public function getProfile() + public function serialize() { - /// TODO serialize request object for profile - $request = $this->worker; + $this->getWorker(); + $data = []; + foreach (['dbname', 'method', 'uri', 'headers', 'body', 'version'] as $key) { + $data[$key] = $this->{$key}; + } - return $request->getMethod() . ' ' . $request->getUri() . '#' . $request->getBody(); + return serialize($data); + } + + public function unserialize($string) + { + foreach (unserialize($string) as $key => $value) { + $this->{$key} = $value; + } } public function isRaw()