PHP Populator (Array-to-Object)
, (*1)
PHP Populator is a simple Array-To-Object library which transforms arrays into a given object with a few additions with Annotations.
This is not a deserializer. If you're looking for a serializer/deserializer, I advise you to use jms/serializer., (*2)
The simple goal of this library is to use a lightweight (still object oriented) solution to hydrate an object from a given array without the overkill of a full serializer.
Plus, behaviour can be easily modified (decoupled), and it's unit tested!, (*3)
 Documentation Summary
Installation
Installation with composer
The easiest way to use this library is to use Composer
Just add the following lines into your composer.json
, (*4)
{
"require": {
"touki/populator": "~1.0.0"
}
}
And run, (*5)
composer update
Setup
Basic setup
This library uses the PSR-0 autoloading mechanism, if you're using composer, there is nothing to do, the class should be autolaoded already., (*6)
In order to start using the populator, you just need to create an instance of it., (*7)
However if you're getting an error, (*8)
[Semantical Error] The annotation "..." does not exist, or could not be auto-loaded., (*9)
You should add the following lines at the top of your application.
This allows doctrine to know how to import your annotations from its own autoloader., (*10)
And, that's it! You can already already start using the library. See Usage for more informations, (*11)
Advanced setup
The populator is magically creating two instances in its constructor if none are given.
It needs an instance of HydratorInterface
(Will hydrate the object on a given context) and an instance of HydratorContextFactoryInterface
(Will create a context based on reflection of a given object), (*12)
To reproduce the default behaviour you can do something like this, (*13)
Usage
Simple usage
Say with have a Foo
class, (*14)
bar = $bar;
}
public function getBar()
{
return $this->bar;
}
public function setPublicWithSetter($var)
{
$this->publicWithSetter = $var;
}
}
$data = array(
'bar' => 'Foobaz!',
'public' => 'Public!'
'publicWithSetter' => 'BySetter'
);
/**
* You can give either classname or an instance
*/
$foo = new Acme\Model\Foo;
$foo = 'Acme\Model\Foo';
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getBar(); // Foobaz!
echo $newFoo->public; // Public!
echo $newFoo->publicWithSetter; // BySetter
?>
Annotations
Along examples, we assume each protected property has its setter and its getter, (*15)
@Populator\Ignore
This annotation skips the setting of the property, (*16)
'Foobaz!'
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getBar(); // NULL
?>
@Populator\Setter
This annotation allows property to define its own class' setter, (*17)
bar = $value;
}
}
$data = array(
'bar' => 'Foobaz!'
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getBar(); // Foobaz!
?>
@Populator\Alias
This annotation adds an alias to match on a property, (*18)
foo = $value;
}
}
$data = array(
'bar' => 'Foobaz!'
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getFoo(); // Foobaz!
$data = array(
'another' => 'Foobaz!'
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getFoo(); // Foobaz!
?>
@Populator\Aliases
This annotation sets and replaces aliases, (*19)
foo = $value;
}
}
$data = array(
'bar' => 'Foobaz!'
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getFoo(); // Foobaz!
?>
 @Populator\Deep
This annotations lets you have a deeper object, (*20)
bar = $bar;
}
}
class Bar
{
protected $baz;
public function setBaz($bar)
{
$this->baz = $baz;
}
}
$data = array(
'bar' => array(
'baz' => 'DeepBaz!'
)
);
$newFoo = $populator->populate($data, $foo);
echo $newFoo->getBar()->getBaz(); // DeepBaz!
?>