PHP code example of akrabat / rka-content-type-renderer

1. Go to this page and download the library: Download akrabat/rka-content-type-renderer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

akrabat / rka-content-type-renderer example snippets


// given:
// $request instanceof Psr\Http\Message\RequestInterface
// $response instanceof Psr\Http\Message\ResponseInterface

$data = [
    'items' => [
        [
            'name' => 'Alex',
            'is_admin' => true,
        ],
        [
            'name' => 'Robin',
            'is_admin' => false,
        ],
    ],
];
$renderer = new RKA\ContentTypeRenderer\Renderer();
$response  = $renderer->render($request, $response, $data);
return $response->withStatus(200);

$hal = new Nocarrier\Hal(
    '/foo',
    [
        'items' => [
            [
                'name' => 'Alex',
                'is_admin' => true,
            ],
            [
                'name' => 'Robin',
                'is_admin' => false,
            ],
        ],
    ]
);
$renderer = new RKA\ContentTypeRenderer\HalRenderer();
$response  = $renderer->render($request, $response, $hal);
return $response->withStatus(200);

$problem = new Crell\ApiProblem("Something unexpected happened");
$renderer = new RKA\ContentTypeRenderer\ApiProblemRenderer();
$response  = $renderer->render($request, $response, $problem);
return $response->withStatus(500);