kokoroe/mapper

A PHP5.5+ mapper to convert array of data into nested PHP objects

v1.0.3 2016-08-08 08:40 UTC

This package is not auto-updated.

Last update: 2024-04-27 17:20:30 UTC


README

Build Status SensioLabs Insight Coveralls HHVM Packagist

Install

Add kokoroe/mapper to your composer.json:

% php composer.phar require kokoroe/mapper:~1.0

Usage

<?php

require __DIR__ . '/vendor/autoload.php';

class Article
{
    protected $title;

    protected $content;

    protected $author;

    protected $tags = [];

    public function setTitle($title)
    {
        $this->title = $title;
    }

    public function setContent($content)
    {
        $this->content = $content;
    }

    public function setAuthor(User $author)
    {
        $this->author = $author;
    }

    public function setTags(array $tags)
    {
        $this->tags = $tags;
    }
}

class User
{
    protected $id;

    protected $name;

    public function setId($id)
    {
        $this->id = $id;
    }

    public function setName($name)
    {
        $this->name = $name;
    }
}


$article = Mapper::hydrate([
    'title' => 'test',
    'content' => 'foo',
    'author' => [
        'id' => 1,
        'name' => 'Axel'
    ],
    'tags' => ['test', 'mapping']
], Article::class);

var_dump($article);

License

mapper-php is licensed under the MIT license.