Skip to content

Commit

Permalink
+ serialization in Request
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 19, 2017
1 parent aba9fca commit 4369206
Showing 1 changed file with 136 additions and 21 deletions.
157 changes: 136 additions & 21 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@

use GuzzleHttp\Psr7\Request as Worker;

class Request
class Request implements \Serializable
{
protected $builder;

/**
* @var Worker
*/
Expand All @@ -24,55 +26,168 @@ 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;
}

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()
Expand Down

0 comments on commit 4369206

Please sign in to comment.