PHP code example of azavyalov / json-mapper

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

    

azavyalov / json-mapper example snippets




port the mapper
use Azavyalov\JsonMapper\JsonMapper;

// Create a class you want to map your data to. Each field must have a type.
final class SomeClass
{
    /**
     * @param string[] $stringArray
     * @param int[] $intArray
     * @param float[] $floatArray
     * @param bool[] $boolArray
     * @param CustomTypeArrayElement[] $customTypeArray
     * @param array<string, bool> $stringToBoolMap
     */
    public function __construct(
        public readonly string $stringField,  // Property names should correspond to JSON field names
        public readonly int $intField,
        public readonly float $floatField,
        public readonly bool $boolField,
        public readonly ?int $nullableField,
        public readonly ?int $missingNullableField,  // A nullable property will be set to `null` if the field is missing in the JSON
        public readonly int $missingNonNullableField,  // If a non-nullable property is missing in the JSON - an exception will be thrown
        public readonly array $stringArray,
        public readonly array $intArray,
        public readonly array $floatArray,
        public readonly array $boolArray,
        public readonly array $customTypeArray,
        public readonly array $stringToBoolMap,
        public readonly NestedObjectClass $nestedObject, // The depth of nested objects is not limited
    ) {}
}

final class CustomTypeArrayElement
{
    public function __construct(
        public readonly string $field_one,
        public readonly bool $field_two,
    ) {}
}

final class NestedObjectClass
{
    public function __construct(
        public readonly int $id,
        public readonly string $name,
        public readonly string $releaseDate,
    ) {}
}

$array = [
    'stringField' => 'Some value',
    'intField' => 123,
    'floatField' => 1.23,
    'boolField' => false,
    'nullableField' => null,
    'missingNonNullableField' => 123,
    'stringArray' => ['String One', 'String Two'],
    'intArray' => [2, 5, 7],
    'floatArray' => [1.2, 2.5, 3.7],
    'boolArray' => [true, false, false],
    'customTypeArray' => [
        ['field_one' => '475754qweqw', 'field_two' => true],
        ['field_one' => '456457mhmty', 'field_two' => false],
    ],
    'nestedObject' => [
        'id' => 567,
        'name' => 'Something Wild',
        'releaseDate' => '1997-11-16',
    ],
    'stringToBoolMap' => [
        '1-2' => true,
        '2-5' => true,
        '5-10' => false,
    ],
];

$mapper = new JsonMapper();

try {
    $result = $mapper->map($array, SomeClass::class);
} catch (\Azavyalov\JsonMapper\Exceptions\JsonMapperException $e) {
    // Something in the provided JSON is unexpected. Handle the exception.
    // $e->getMessage();
    // $e->getClassName();
    // $e->getPropertyName();
    throw $e;
}

print_r($result);


final class SomeClass
{
    /**
     * @param string[] $arrayOfStrings
     * @param CustomTypeArrayElement[] $arrayOfCustomObjects
     * @param array<string, int> $stringToIntMap
     * @param array<int, CustomObjectClass> $intToCustomObjectMap
     */
    public function __construct(
        public readonly array $arrayOfStrings,
        public readonly array $arrayOfCustomObjects,
        public readonly array $stringToIntMap,
        public readonly array $intToCustomObjectMap,
    ) {}
}