mike-roetgers / data-mapper
Simple data mapper
Installs: 22 215
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-11-09 16:00:57 UTC
README
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().