renanbr/param-juggler-middleware

1.0.0 2016-01-21 05:52 UTC

This package is auto-updated.

Last update: 2020-10-05 10:24:52 UTC


README

Build Status

This middleware converts request parameters types.

Type Auto converted values Forced + Mismatch
bool 'true', 'yes', 'false' and 'no' 't', 'y', '1': true, everything else: false
int '1', '-8', ... native casting
float '1.0', '-.2', '1.2e3', '-7E-10', ... native casting
DateTime '2010-11-12 13:14:15', '2010-11-12 13:14', '2010-11-12', ... null

This project is a framework-agnostic middleware built on PSR-7:

It may make your life easer to build strict type-checking applications, or just to stop casting parameters.

Usage

class SomeAction
{
    public function __invoke($request, $response)
    {
        // numeric digits
        $id = $request->getQueryParams()['id'];
        is_integer($id); // true

        // 'yes', 'true', 'no' or 'false'
        $confirm = $request->getQueryParams()['confirm'];
        is_bool($confirm); // true

        // '2010-11-12'
        $date = $request->getQueryParams()['schedule_date'];
        is_object($date); // true
        get_class($date); // \DateTime
    }
}

Install and Configuration

Add this project as dependency.

composer require renanbr/param-juggler-middleware

There are some options available, all of them are optional and their usage depends to the framework. You may...

  • ignore parameters conversion by name;
  • force parameters to be always converted;
  • select types to convert: bool, int, float and datetime;
  • select sources to be read: get, post, cookie and attribute.

Zend Expressive

To install it application widely, change config/autoload/middleware-pipeline.global.php file.

return [
    'dependencies' => [
        'factories' => [
            /* ... */
            RenanBr\ParamJugglerMiddleware::class => RenanBr\ParamJugglerMiddlewareFactory::class,
        ],
    ],
    'middleware_pipeline' => [
        /* ... */
        Zend\Expressive\Container\ApplicationFactory::ROUTING_MIDDLEWARE,
        /* ... */
        RenanBr\ParamJugglerMiddleware::class, // before dispatch
        Zend\Expressive\Container\ApplicationFactory::DISPATCH_MIDDLEWARE,
        /* ... */
    ],
];

It's possible to run it under some route, see more at Zend Expressive documentation.

For changing the default behavior, you may create and manipulate the config/autoload/param-juggler.global.php file.

return [
    'param_juggler' => [
        'skip' => ['username', 'password'], // defaults to empty array
        'force' => [                        // defaults to empty array
            'startDate' => 'datetime',
            'endDate' => 'datetime',
            'confirm' => 'bool', // when forced, 't', 'y' and '1' are also converted to true
        ],
        'types' => ['bool'],                // defaults to ['bool', 'int', 'float', 'datetime']
        'source' => ['get', 'post'],        // defaults to ['get', 'post', 'cookie', 'attribute']
    ],
];

Slim Framework

To install it application widely, add this middleware to the \Slim\App instance.

$app = new Slim\App();
/* ... */
$app->add(new RenanBr\ParamJugglerMiddleware());
/* ... */
$app->run();

It's possible to run it under some route, see more at Slim Framework documentation

For changing the default behavior, when creating a middleware instance you may configure it through constructor.

new RenanBr\ParamJugglerMiddleware(
    new RenanBr\ParamJugglerMiddlewareConfig(
        ['username', 'password'], // "skip", defaults to empty array
        [                         // "force", defaults to empty array
            'startDate' => 'datetime',
            'endDate' => 'datetime',
            'confirm' => 'bool' // when forced, 't', 'y' and '1' are also converted to true
        ],
        ['bool'],                 // "types", defaults to ['bool', 'int', 'float', 'datetime']
        ['get', 'post']           // "source", defaults to ['get', 'post', 'cookie', 'attribute']
    )
];