PHP code example of stiction / safe-params

1. Go to this page and download the library: Download stiction/safe-params library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

stiction / safe-params example snippets


use Stiction\SafeParams\SafeParamsParser;

$parser = new SafeParamsParser;
$age = $parser->parse(' 29  ', 'int');

/*
int(29)
*/
var_dump($age);

use Stiction\SafeParams\SafeParamsParser;

$data = [
    'name' => '   jack   ',
    'id' => 29,
    'hobbies' => [21, 'baseball', 3.14],
    'address' => 'a beautiful village',
];
$spec = ['name' => 'string.trim', 'id' => 'uint64', 'hobbies' => 'array.int'];
$parser = new SafeParamsParser;
$safeData = $parser->parse($data, $spec);

/*
array(3) {
  ["name"]=>
  string(4) "jack"
  ["id"]=>
  string(2) "29"
  ["hobbies"]=>
  array(3) {
    [0]=>
    int(21)
    [1]=>
    int(0)
    [2]=>
    int(3)
  }
}
*/
var_dump($safeData);