PHP code example of igorpocta / data-mapper

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

    

igorpocta / data-mapper example snippets


use Pocta\DataMapper\Mapper;

class User
{
    public function __construct(
        public int $id,
        public string $name,
        public bool $active
    ) {}
}

$mapper = new Mapper();

// JSON → Object
$user = $mapper->fromJson('{"id": 1, "name": "John", "active": true}', User::class);

// Object → JSON
$json = $mapper->toJson($user); // {"id":1,"name":"John","active":true}

// Array → Object
$user = $mapper->fromArray(['id' => 1, 'name' => 'John', 'active' => true], User::class);

// Object → Array
$array = $mapper->toArray($user); // ['id' => 1, 'name' => 'John', 'active' => true]

use Pocta\DataMapper\MapperOptions;

$mapper = new Mapper(MapperOptions::api());        // Recommended for HTTP request bodies (untrusted input)
$mapper = new Mapper(MapperOptions::strict());     // Auto-validation + unknown keys rejected
$mapper = new Mapper(MapperOptions::production()); // Defaults (lenient)

// Presets are adjustable
$mapper = new Mapper(MapperOptions::api()->with(parseFormattedNumbers: true));