mike-roetgers/data-mapper

Simple data mapper

0.1.0 2014-07-01 12:16 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:15:01 UTC


README

Build Status

A simple data mapper that can support you mapping entities to other formats like arrays or JSON and the other way around.

Usage

$data = '{"id": 1, "name": "Mike"}';

$mapper = new GenericMapper(new EntityAutoMapper(), '\\My\\Namespace\\TestEntity');
$entity = $mapper->mapJsonToEntity($data);

echo $entity->getId(); // 1
echo $entity->getName(); // Mike

The mapper can translate attribute names between the entity and other formats.

$data = '{"user_id": 23, "user_name": "Jonathan"}';

$mapper = new GenericMapper(new EntityAutoMapper(), '\\My\\Namespace\\TestEntity', array('id' => 'user_id', 'name' => 'user_name'));
$entity = $mapper->mapJsonToEntity($data);

echo $entity->getId(); // 23
echo $entity->getName(); // Jonathan

Requirements

The data mapper expects your entities to have setters and getters, e.g. $yourEntity->setName('Name') or $yourEntity->getName().