Skip to content

Converts Symfony Request params to a predefined structure array

Notifications You must be signed in to change notification settings

php-arsenal/symfony-request-param-bagger

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Symfony Request Param Bagger

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)
}