opsbears/piccolo-datamapper

This package is abandoned and no longer maintained. No replacement package was suggested.

Data mapping utility

dev-master 2016-09-08 22:25 UTC

This package is not auto-updated.

Last update: 2020-10-30 22:07:58 UTC


README

Abstract

The Piccolo datamapper takes an array and transforms it into an object, or hydrates an existing object using setters. It does its best not to use reflection, which comes at the price of you having to specify the parameters manually.

Installation

This package can be installed using composer:

composer require opsbears/piccolo-datamapper

Usage

The DataMapper is an (almost) reflection-less utility for creating and hydrating objects. It assumes you have your data as a key-value array and want to create or hydrate an object from it. To do that, you will need to specify in which order your array parameters should be passed to the constructor, or to your setters.

class YourClass {
    public function __construct($foo) {
    }

    public function setBar($bar) {
    }
}

$dataMapper = new DataMapper(YourClass::class);
$dataMapper->addConstructorParameter('baz');
$dataMapper->addSetterParameter('bar');

$instance = $dataMapper->create(['baz' => 'Hello world!', 'bar' => 'Why not?']);

You can also hydrate existing objects:

$instance = new YourClass('Hello world!');
$instance = $dataMapper->hydrate($instance, ['bar' => 'Why not?']);