Skip to content

Commit

Permalink
added Request and Response wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
hiqsol committed Jan 18, 2017
1 parent 95bd315 commit ce8fb9d
Show file tree
Hide file tree
Showing 2 changed files with 154 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/Request.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
/**
* Tools to use API as ActiveRecord for Yii2
*
* @link https://github.com/hiqdev/yii2-hiart
* @package yii2-hiart
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\hiart;

use GuzzleHttp\Psr7\ServerRequest;

class Request
{
/**
* @var ServerRequest
*/
protected $worker;

/**
* @var Query
*/
protected $query;

public function __construct(ServerRequest $worker, Query $query)
{
$this->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']);
}
}
83 changes: 83 additions & 0 deletions src/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* Tools to use API as ActiveRecord for Yii2
*
* @link https://github.com/hiqdev/yii2-hiart
* @package yii2-hiart
* @license BSD-3-Clause
* @copyright Copyright (c) 2015-2016, HiQDev (http://hiqdev.com/)
*/

namespace hiqdev\hiart;

use Psr\Http\Message\ResponseInterface;
use yii\helpers\Json;

class Response
{
/**
* @var ResponseInterface
*/
protected $worker;

/**
* @var Request
*/
protected $request;

/**
* @var mixed response data
*/
protected $data;

public function __construct(ResponseInterface $worker, Request $request)
{
$this->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();
}

}

0 comments on commit ce8fb9d

Please sign in to comment.