Giter Club home page Giter Club logo

input-converter-php's Introduction

PHPUnit PHPStan PHPCS-Fixer

sbsedv/input-converter

A minimal PHP component to nativly support user input parsing on http methods other than POST.

PHP natively only supports the parsing of multipart/form-data and application/x-www-form-urlencoded on POST http requests.

Many modern web applications also want use / support a) other http methods like PUT or PATCH and b) other content encodings like JSON or XML.

This component provides a very simple and extensible object oriented api to support just that.

Internally this component uses the PHP native functions json_decode and parse_str (multpart/form-data gets "translated" to x-www-form-urlencoded) and therefore complex data structures (arrays and objects) are only limited by what those functions support.
This effectifly means that HTMLForms like the following are FULLY supported.

<form method="PUT">
    <select name="select[]" multiple>
        ...
    </select>

    <input type="text" name="text" />

    <input type="text" name="obj[key1]" />
    <input type="text" name="obj[key2]" />
    <select name="obj[key3][]" multiple>
        ...
    </select>
</form>

How it Works

You should instantiate and call this component as early in your app lifecycle as possible.

You MUST either pass a PSR-7 or HTTP-Foundation request wrapper object to the "convert" method.

<?php declare(strict_types=1);

use SBSEDV\InputConverter\InputConverter;
use SBSEDV\InputConverter\Request\HttpFoundationRequest;
use SBSEDV\InputConverter\Request\Psr7Request;

$request = new HttpFoundationRequest($request);
// $request = new Psr7Request($request);

try {
    (new InputConverter())
        ->addConverter(...) // your converters
        ->convert($request);
} catch (MalformedContentException $e) {
    // a converter supported the request
    // but encountered an error while parsing

    http_status_code(400);
    exit();
} catch (UnsupportedRequestException) {
    // no converter supported the request
}

The decoded body data is automatically added to the underlying Psr7 or Http-Foundation request object.

Caution: Psr7 request are immutable. You can get the new object by calling $request->getRequest().


Converters

The actual parsing is handled by converter classes that implement SBSEDV\InputConverter\Converter\ConverterInterface.

You can always implement your own converter.

By default we support three customisable converters:

SBSEDV\InputConverter\Converter\UrlEncodedConverter

Via its constructor you can influence which http methods it supports.

public function __construct(
    array $methods = ['PUT', 'PATCH', 'DELETE']
);

SBSEDV\InputConverter\Converter\JsonConverter

Via its constructor you can influence which content types and http methods it supports.

public function __construct(
    array $contentTypes = ['application/json'],
    array $methods = ['POST', 'PUT', 'PATCH', 'DELETE']
);

SBSEDV\InputConverter\Converter\FormDataConverter

Via its constructor you can influence which content types and http methods it supports.

Internally this uses the riverline/multipart-parser library for parsing.

public function __construct(
    array $methods = ['PUT', 'PATCH', 'DELETE'],
    bool $fileSupport = false
);

input-converter-php's People

Contributors

danielburger1337 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

paggy-project

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.