jerodev/data-mapper

Maps raw data to a typed PHP object

1.2.1 2024-01-07 11:54 UTC

This package is auto-updated.

Last update: 2024-10-15 22:00:40 UTC


README

run-tests Latest Stable Version

This package will map any raw data into a predefined strong-typed PHP object.

Installation

The mapper has no external dependencies apart from PHP8.1 or higher. It can be installed using composer:

composer require jerodev/data-mapper

Basic mapping

Let's start with the basics. The mapper will map data directly to public properties on objects. If these properties have types defined either using types introduced in PHP7.4 or through PHPDoc, the mapper will attempt to cast the data to these types.

For example: imagine having an Entity class with the public properties $id and $name:

class User
{
    public int $id;
    public string $name;
}

To map data from an array we simply pass the class name and an array with data to the mapper.

$mapper = new \Jerodev\DataMapper\Mapper();
$entity = $mapper->map(User::class, [
    'id' => '5',
    'name' => 'John Doe',
]);

//    User {
//        +id: 5,
//        +name: "John Doe",
//    }

This is a simple example, but the mapper can also map nested objects, arrays of objects, keyed arrays, and even multi-level arrays.

Documentation

More information about mapping, configuration and best practices can be found in the documentation.

License

This library is licensed under the MIT License (MIT). Please see License File for more information.