-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
154 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |