fivelab/object-mapper

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

Map array data to objects

v1.0 2015-08-31 09:05 UTC

This package is not auto-updated.

Last update: 2017-08-16 12:52:02 UTC


README

With this package, you can map array data to object instances.

Installation

Add FiveLab/ObjectMapper in your composer.json:

{
    "require": {
        "fivelab/object-mapper": "~1.0"
    }
}

Now tell composer to download the library by running the command:

$ php composer.phar update fivelab/object-mapper

Basic usage

Before use ObjectMapper you must configure instance:

  1. Create a metadata factory for loads metadata from objects
  2. Create a strategy manager
use Doctrine\Common\Annotations\AnnotationReader;
use FiveLab\Component\ObjectMapper\Strategy\StrategyManager;
use FiveLab\Component\ObjectMapper\Metadata\MetadataFactory;
use FiveLab\Component\ObjectMapper\Metadata\Loader\AnnotationLoader;
use FiveLab\Component\ObjectMapper\ObjectMapper;
use FiveLab\Component\ObjectMapper\Metadata\ObjectMetadata;

$strategyManager = StrategyManager::createDefault();

$annotationLoader = new AnnotationLoader(new AnnotationReader());
$metadataFactory = new MetadataFactory($annotationLoader);

$objectMapper = new ObjectMapper($metadataFactory, $strategyManager);

// Or create default object mapper
$objectMapper = ObjectMapper::createDefault(); // Used AnnotationLoader for load metadata

Attention: now supported only annotation metadata loader.

After configure and create instance, you can use ObjectMapper map function.

Example object for maps:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property()
     */
    public $name;
}

And map array data:

$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'name' => 'Foo Bar'
]);

If you want map all properties in object, you can set attribute allProperties for @Object, then this indicate for load all properties from class.

use FiveLab\Component\ObjectMapper\Annotation\Object;

/**
 * @Object(allProperties=true)
 */
class MyClass
{
    public $id;
    public $name;
}

If key of array not equals to property name of object, you can set attribute fieldName for @Property

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

/**
 * @Object()
 */
class MyClass
{
    /**
     * @Property()
     */
    public $id;

    /**
     * @Property(fieldName="first_name")
     */
    public $firstName;
}

$object = new MyClass();
$objectMapper->map($object, [
    'id' => 1,
    'first_name' => 'Foo Bar'
]);

Recursive mapping

You can recursive map data to object.

With simple object:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        'name' => 'Foo Bar'
    ]
]);

With collection:

use FiveLab\Component\ObjectMapper\Annotation\Object;
use FiveLab\Component\ObjectMapper\Annotation\Property;

class MyClass
{
    /**
     * @DataMapping\Property(collection=true, class="Tag")
     */
    protected $tag;
}

/**
 * @DataMapping\Object(allProperties=true)
 */
class Tag
{
    protected $name;
}

$object = new MyClass();
$objectMapper->map($object, [
    'tag' => [
        ['name' => 'Foo Bar'],
        ['name' => 'Bar Foo']
    ]
]);

And you can set the collection class if necessary, to attribute collection collection="MyCollectionClass" All keys as default will be saved.