GithubHelp home page GithubHelp logo

akrabat / rka-content-type-renderer Goto Github PK

View Code? Open in Web Editor NEW
41.0 41.0 10.0 144 KB

Render an array(or HAL object) to a JSON/XML/HTML PSR-7 Response based on a PSR-7 Request's Accept header.

License: BSD 3-Clause "New" or "Revised" License

PHP 100.00%

rka-content-type-renderer's Introduction

Render output based on content-type

Render an array (or HAL object) to a JSON/XML/HTML PSR-7 Response based on a PSR-7 Request's Accept header.

Build status

Installation

composer require akrabat/rka-content-type-renderer

Usage

// 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);

The constructor takes a parameter, $pretty that defaults to true. Set to false to disable pretty printing.

HalRenderer

This component also supports nocarrier/hal objects with the HalRenderer:

$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);

ApiRenderer

This component also supports crell/ApiProblem objects with the ApiProblemRenderer:

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

Arrays of objects

If you have an array of objects, then the renderer will still work as long as the objects implement PHP's JsonSerializable interface.

Testing

  • Code style: $ phpcs
  • Unit tests: $ phpunit
  • Code coverage: $ phpunit --coverage-html ./build

rka-content-type-renderer's People

Contributors

akrabat avatar cruzcraul avatar stefanotorresi avatar tscheckenbach avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

rka-content-type-renderer's Issues

Where $renderer->render

Where is the best location to use the render? In the routes file or a controller or model or ??

Null support on json rendering

like below

class Renderer
{
    protected $mediaSubtypesToAllowedDataTypesMap = [
        'json' => ['scalar','array', 'JsonSerializable', 'null'],
    ];

    protected function isDataValidForMediaType($mediaSubType, $data)
    {
        $allwedDataTypes = $this->mediaSubtypesToAllowedDataTypesMap[$mediaSubType];

        foreach ($allwedDataTypes as $allowedDataType) {
            switch ($allowedDataType) {
                case 'null':
                    if (null === $data) {
                        return true;
                    }
                    break;
            }
        }

        return false;
    }
}

Possible type of response by query param in url

Hi, I'm working on simple API and I would like give the freedom to set the response type throught ?format=@format query url parameter, maybe set a mapper of submedia types like this:

protected $mediaSubtypesToMediaTypesMap = [
    'json' => 'application/json',
    'xml' => 'application/xml',
    'html' => 'text/html'
];

And later in render determinate by something like this:

//more code
$mediaTypeHeader= $request->getHeaderLine('Accept');
$subMediaTypeQuery = $request->getQueryParam('format');

$mediaTypeAccept = $this->mediaSubtypesToMediaTypesMap[$subMediaTypeQuery] ?? $mediaTypeHeader;

$mediaType = $this->determineMediaType($mediaTypeAccept);
// more code

What do you think about this feature ?

JSON_PRETTY_PRINT as an option

It seems that for the application/json output format, the flag JSON_PRETTY_PRINT is always used.

When the output of an API is big, it would be extremely useful to have some option to disable this behaviour and remove that flag.

If it makes sense, can you please add this feature ?

JSONP

Hi,

Is there some way to implement JSONP too with your renderer ?

Thanks a lot

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.