PHP code example of meklis / array-to-object-mapper

1. Go to this page and download the library: Download meklis/array-to-object-mapper 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/ */

    

meklis / array-to-object-mapper example snippets


class ParentClass
{
    /**
     * @var int
     */
    protected $id;

    /**
     * String with camelCases
     * @var string
     */
    protected $testString;

    /**
     * @var string
     */
    protected $variable_with_snake_case;

    /**
     * @var Child
     */
    protected $child;

    /**
     * @var Child[]
     */
    protected $childs;
}

class Child
{
    /**
     * @var int
     */
    protected $id;

    /**
     * @var Child | null
     */
    protected ?Child $child;

    /**
     * @var array
     */
    protected $params = [];
}



use \Meklis\ArrToObjectMapper\Mapper;
use \Meklis\ArrToObjectMapper\example\Classes\ParentClass;

$mapper = new Mapper();
$mapper->setStrict(true);
$data = json_decode(file_get_contents(__DIR__ . '/classes.json'),true);

$mapped = $mapper->map($data, ParentClass::class);

print_r($mapped);

// OUTPUT
/*
Meklis\ArrToObjectMapper\example\Classes\ParentClass Object
(
    [id:protected] => 144
    [testString:protected] => This is test string
    [variable_with_snake_case:protected] => Variable with snake case
    [child:protected] => Meklis\ArrToObjectMapper\example\Classes\Child Object
        (
            [id:protected] => 1
            [child:protected] => 
            [params:protected] => Array
                (
                    [0] => param1
                    [1] => param2
                    [2] => param3
                )

        )

    [childs:protected] => Array
        (
            [0] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 1
                    [child:protected] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                        (
                            [id:protected] => 6
                            [params:protected] => Array
                                (
                                    [0] => param1
                                    [1] => param2
                                    [2] => param3
                                )

                        )

                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

            [1] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 3
                    [child:protected] => 
                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

            [2] => Meklis\ArrToObjectMapper\example\Classes\Child Object
                (
                    [id:protected] => 4
                    [child:protected] => 
                    [params:protected] => Array
                        (
                            [0] => param1
                            [1] => param2
                            [2] => param3
                        )

                )

        )

)
 */