-
Notifications
You must be signed in to change notification settings - Fork 0
Using Specific Interfaces
The library brings two interfaces to use when create array or object, can use one of them or both. Interfaces are used to override the way that some object are parsed. Can use object with interfaces and without them, can mix these objects or create relations between them. The interface only are used on objects implementing it, otherwise the library try to parse the object using the given context.
This interface is used to create a array from a object. Implements this interface on desired object and return the array in the format that you need.
class Player implements Object2ArrayInterface
{
//....
/**
* @inheritDoc
*/
public function __toArray()
{
return [
'urn:PlayerName' => $this->getName(),
'urn:PlayerNumber' => $this->getNumber(),
'urn:Awards' => $this->getAwards(),
];
}
}
NOTE: In case of return a object inside the array or array of ojects, these will be parsed recursively using interface in case of have or automatically using the Object2Array tool and the current context.
These interface is used to populate a object with given data and override the default way to do this.
class Player implements Array2ObjectInterface
{
//....
/**
* @inheritDoc
*/
public function __populate($data)
{
$this->setName($data['urn:PlayerName']);
$this->setNumber($data['urn:PlayerNumber']);
foreach ($data['urn:Awards'] as $award){
$this->awards[] = Array2ObjectBuilder::create()->build()->createObject(Award::class, $award);
}
}
}