2017 © Pedro Peláez
 

library popomapper

Plain old PHP object JSON mapper - Map json or array structures to a POPO

image

brash-creative/popomapper

Plain old PHP object JSON mapper - Map json or array structures to a POPO

  • Thursday, January 22, 2015
  • by brash-creative
  • Repository
  • 1 Watchers
  • 1 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

Plain Old PHP Object JSON mapper

Takes data held in a JSON or array format, and maps it to an application's PHP objects using docblock annotations for parameter types., (*1)

Parameter types include all simple types (string, int, etc...), complex types like ArrayObject & DateTime, and nested application objects., (*2)

Usage

<?php
use Brash\PopoMapper\Mapper;

$mapper     = new Mapper();
$object     = $mapper->map($data, new Object());

The object mapper will detect if the data passed is a single array, or a multi-dimensional array of object data., (*3)

Example - Single data object

JSON for a basic client object, with nested purchases, (*4)

<?php
$data   = '{
    "id": 1,
    "name": "Geoffrey",
    "purchases": [
        {
            "id": 1,
            "name": "Gromit"
        },
        {
            "id": 2,
            "name": "Whatsitsname"
        }
    ]
}';

Client object, (*5)

<?php
class Client {
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @var Purchase[]
     */
    private $purchases;

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param Purchase[] $purchases
     *
     * @return $this
     */
    public function setPurchases($purchases)
    {
        $this->purchases = $purchases;
        return $this;
    }

    /**
     * @return Purchase[]
     */
    public function getPurchases()
    {
        return $this->purchases;
    }
}

Purchase object, (*6)

<?php
class Purchase {
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;

    /**
     * @param int $id
     *
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    /**
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @param string $name
     *
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

Application code, (*7)

<?php
$mapper     = new Mapper();
$client     = $mapper->mapSingle($data, new Client());

Example - Multiple data objects

To return an ArrayObject of multiple mapped objects, simply pass an array of data., (*8)

<?php
$data   = '[
    {
        "id": 1,
        "name": "Client 1"
    },
    {
        "id": 2,
        "name": "Client 2"
    }
]'

Application code, (*9)

<?php
$mapper     = new Mapper();
$client     = $mapper->mapMulti($data, new ArrayObject(), new Client());

The Versions

22/01 2015

dev-master

9999999-dev

Plain old PHP object JSON mapper - Map json or array structures to a POPO

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

22/01 2015

1.2.0

1.2.0.0

Plain old PHP object JSON mapper - Map json or array structures to a POPO

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

22/01 2015

dev-develop

dev-develop

Plain old PHP object JSON mapper - Map json or array structures to a POPO

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

17/11 2014

1.1.1

1.1.1.0

Plain old PHP object mapper

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

07/11 2014

1.1.0

1.1.0.0

Plain old PHP object mapper

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

07/11 2014

1.0.1

1.0.1.0

Plain old PHP object mapper

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley

04/11 2014

1.0.0

1.0.0.0

Plain old PHP object mapper

  Sources   Download

The Requires

  • php >=5.3

 

The Development Requires

by Paul Crashley