PHP code example of alexpts / php-typecast

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

    

alexpts / php-typecast example snippets


$body = [
    'title' => 'Some',
    'user' => [
        'name' => 'Alex'
        'age' => '29',
        'isAdmin' = 'false',
    ],
    'friendsIds' => ['1', '2', '3', 4],
    'date' => '11-12-2017',
    'date2' => '11-12-2017',
];

$caster = new TypeCast(new DeepArray);

// shot format
$data = $caster->cast($body, [
    'friendsIds' => ['array', ['each' => ['int']]],
    'title' => ['string'],
    'user' => ['array'],
    'user.name' => ['string'],
    'user.age' => ['int'],
    'user.isAdmin' => ['bool'],
    'date' => ['datetime'],
    'date2' => ['datetime', ['datetimeFormat' => ['Y-m-d']]], // full format
]);

/*
$data ==== [
    'title' => 'Some',
    'user' => [
        'name' => 'Alex'
        'age' => 29,
        'isAdmin' = false,
    ],
    'friendsIds' => [1, 2, 3, 4],
    'date' => new \DateTime('11-12-2017'),
    'date2' => '2017-12-11',
];
*/

public function registerType(string $name, callable $handler): self