Coder Social home page Coder Social logo

pew-pew-team / http-factory Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 11 KB

Factory for request decoding and response encoding

License: MIT License

PHP 100.00%
accept bundle content-type decoder encoder factory http request response symfony symfony-bundle

http-factory's Introduction

PHP 8.3+ Latest Stable Version Latest Unstable Version License MIT

HTTP Factory

A set of drivers for encoding HTTP responses and decoding HTTP requests.

Installation

PewPew HTTP Factory is available as Composer repository and can be installed using the following command in a root of your project:

$ composer require pew-pew/http-factory

More detailed installation instructions are here.

Usage

Decoder

// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();

// Requests Factory
$requests = new \PewPew\HttpFactory\RequestDecoderFactory([
    new \PewPew\HttpFactory\Driver\JsonDriver(),
]);

$payload = $requests
    ->createDecoder($request) // Detect passed "content-type" header and
                              // create decoder if available.
    ?->decode($request->getContent(true));   // Decode request body.

Encoder

// Symfony Request
$request = new \Symfony\Component\HttpFoundation\Request();

// Responses Factory
$responses = new \PewPew\HttpFactory\ResponseEncoderFactory([
    new \PewPew\HttpFactory\Driver\JsonDriver(),
]);

$response = $responses
    ->createEncoder($request) // Detect passed "accept" header and create
                              // encoder if available.
    ?->encode(['some' => 'any'], 200);  // Encode payload and create response.

Symfony Integration

Add the bundle to your bundles.php file:

// bundles.php
return [
    // ...
    PewPew\HttpFactory\HttpFactoryBundle::class => ['all' => true],
];

Use ResponseEncoderFactoryInterface and RequestDecoderFactoryInterface in your services:

use PewPew\HttpFactory\ResponseEncoderFactoryInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;

final readonly class ExampleController
{
    public function __construct(
        private ResponseEncoderFactoryInterface $responses,
    ) {}
    
    public function someAction(Request $request): Response 
    {
        $encoder = $this->responses->createEncoder($request);
        
        if ($encoder === null) {
            throw new \Symfony\Component\HttpFoundation\Exception\BadRequestException(
                'Unsupported "accept" request header',
            );
        }
        
        return $encoder->encode([
            'status' => 'ok'
        ], Response::HTTP_OK);
    }
}

http-factory's People

Contributors

serafimarts avatar

Stargazers

wilbur.yu avatar guanguans avatar Thanh Trần avatar

Watchers

 avatar

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.