PHP code example of sopheos / pebble_dataconverter

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

    

sopheos / pebble_dataconverter example snippets


class Car
{
    public $number;
    public $model;
    public $date;
}

class User
{
    public $name;
    public $birthdate;
    public array $cars = [];
}

$input = json_encode([
    'name' => 'Toto',
    'birthdate' => '1980-10-03',
    'cars' => [
        [
            'number' => 1651984,
            'model' => 'renault',
            'date' => '2000-01-01'
        ],
        [
            'number' => 2061161,
            'model' => 'audi',
            'date' => '2021-08-19'
        ],
    ]
]);

$carConverter = MapConverter::create()
    ->map('model', function ($input) {
        return ucfirst($input);
    })
    ->map('date', DatetimeConverter::create('Y-m-d', 'd/m/Y'))
    ->one(Car::class);

$userConverter = JsonDecodeConverter::create()
    ->one([
        'birthdate' => DatetimeConverter::create('Y-m-d', 'd/m/Y'),
        'cars' => Converter::create()->many($carConverter)
    ])
    ->one(new User);


$output = $userConverter($input);