php-arsenal/symfony-request-param-bagger

Collects sent parameters from Symfony Request and outputs an array.

0.5.5 2023-07-21 08:25 UTC

This package is auto-updated.

Last update: 2024-10-21 11:12:07 UTC


README

Collects sent parameters from Request and returns them in an array.

Features:

  • assign default values
  • cast to a specific type
  • returns an array

Usage

Request example

{
    "type": "gold",
    "size": "20",
    "amount": "1.23"
}

Parse example

<?php 
    use PhpArsenal\SymfonyRequestParamBagger\RequestParamBagger;

    // ...
    
    #[Route('/api', methods: ['POST'], format: 'json')]
    public function postSomething(
        Request $request
    ): JsonResponse {
        $params = RequestParamBagger::build($request, [
            'type' => null,
            'size' => null,
            'amount' => null,
        ], [
            'type' => 'string',
            'size' => 'int',
            'amount' => 'float',
        ]);
        
        var_dump($params);
        
        // ...

Output

array(3) {
  ["type"]=>
  string(4) "gold"
  ["size"]=>
  int(20)
  ["amount"]=>
  float(1.23)
}