PHP code example of serger / typecaster

1. Go to this page and download the library: Download serger/typecaster 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/ */

    

serger / typecaster example snippets


use SergeR\Typecaster\Typecast;

$data = [
    'name'   => '  John Doe  ',
    'age'    => '42',
    'price'  => '19,99',
    'active' => '1',
    'meta'   => '{"role":"admin"}',
];

$result = Typecast::scalarArrayValues($data, [
    'name'   => 'trim',
    'age'    => 'int',
    'price'  => ['type' => 'float', 'precision' => 2, 'min' => 0],
    'active' => 'bool',
    'meta'   => ['type' => 'json', 'as_array' => true],
]);

// [
//     'name'   => 'John Doe',
//     'age'    => 42,
//     'price'  => 19.99,
//     'active' => true,
//     'meta'   => ['role' => 'admin'],
// ]

Typecast::scalarArrayValues($data, [
    'marking'    => ['type' => 'string', 'default' => ''],
    'paid_price' => ['type' => 'float', 'default' => 0.0],
]);