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());