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
1 parent
8921bae
commit da1a27d
Showing
9 changed files
with
94 additions
and
23 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,15 @@ | ||
<?php | ||
|
||
namespace App\Controller\Api\V1\Test; | ||
use App\Controller\Controller; | ||
use Psr\Http\Message\ServerRequestInterface as Request; | ||
use Psr\Http\Message\ResponseInterface as Response; | ||
|
||
class TestController extends Controller | ||
{ | ||
|
||
public function index(Request $request, Response $response, $args) | ||
{ | ||
return $this->jsonApi->render($response,['username' => 'akhgar.net', 'realname' => 'Afsh', 'age' => 32]); | ||
} | ||
} |
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
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,2 @@ | ||
<?php | ||
$route->get('/api/v1/test', \App\Controller\Api\V1\Test\TestController::class.':index')->setName('api_test'); |
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
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
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
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
<?php | ||
<?php | ||
return [ | ||
// Renderer settings | ||
'view' => [ | ||
|
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
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,59 @@ | ||
<?php | ||
namespace Core\Renderers; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
use Slim\Http\Body; | ||
|
||
class JsonApiRenderer | ||
{ | ||
/** | ||
* Bitmask consisting of <b>JSON_HEX_QUOT</b>, | ||
* <b>JSON_HEX_TAG</b>, | ||
* <b>JSON_HEX_AMP</b>, | ||
* <b>JSON_HEX_APOS</b>, | ||
* <b>JSON_NUMERIC_CHECK</b>, | ||
* <b>JSON_PRETTY_PRINT</b>, | ||
* <b>JSON_UNESCAPED_SLASHES</b>, | ||
* <b>JSON_FORCE_OBJECT</b>, | ||
* <b>JSON_UNESCAPED_UNICODE</b>. | ||
* The behaviour of these constants is described on | ||
* the JSON constants page. | ||
* @var int | ||
*/ | ||
/** | ||
* Output rendered template | ||
* | ||
* @param ResponseInterface $response | ||
* @param array $data Associative array of data to be returned | ||
* @param int $status HTTP status code | ||
* @param array $addHeaders Associative array of header to be added | ||
* @return ResponseInterface | ||
*/ | ||
public function render(ResponseInterface $response, array $data = [], $status = 200, $addHeaders = []) | ||
{ | ||
$status = intval($status); | ||
$output = [ | ||
'meta' => ['error' => true, 'status' => $status], | ||
'data' => $data, | ||
]; | ||
$output['meta']['error'] = ($status < 400) ? false : true; | ||
$json = json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); | ||
// Ensure that the json encoding passed successfully | ||
if ($json === false) { | ||
throw new \RuntimeException(json_last_error_msg(), json_last_error()); | ||
} | ||
$body = new Body(fopen('php://temp', 'r+')); | ||
$body->write($json); | ||
$newResponse = $response->withBody($body); | ||
$newResponse = $newResponse->withStatus($status) | ||
->withHeader('Content-Type', 'application/json;charset=utf-8'); | ||
if (count($addHeaders)) { | ||
foreach ($addHeaders as $headerKey => $headerValue) { | ||
if (strtolower($headerKey) != 'content-type') { | ||
$newResponse->withHeader($headerKey, $headerValue); | ||
} | ||
} | ||
} | ||
return $newResponse; | ||
} | ||
} |