I’m having some difficulty serializing what I call an object tree. I’m using mezzio-hal (v3) with the following HAL config:
[
'__class__' => RouteBasedResourceMetadata::class,
'resource_class' => Handler\PersonEntity::class,
'route' => 'list.persons',
'extractor' => ClassMethodsHydrator::class,
],
[
'__class__' => RouteBasedCollectionMetadata::class,
'collection_class' => Handler\PersonCollection::class,
'collection_relation' => 'persons',
'route' => 'list.persons',
],
This is my collection class:
class PersonCollection extends Paginator
{
}
This is my entity class:
class PersonEntity
{
private $id;
private $name;
public function getId() : int
{
return $this->id;
}
public function setId(int $id) : self
{
$this->id = $id;
return $this;
}
public function getName() : NameEntity
{
if (null === $this->name) {
$this->name = new NameEntity();
$this->name->setFirstName('fn');
$this->name->setLastName('ln');
}
return $this->name;
}
public function setName(NameEntity $name) : self
{
$this->name = $name;
return $this;
}
}
class NameEntity
{
private $firstName;
private $lastName;
public function getFirstName() : string
{
return $this->firstName;
}
public function setFirstName(string $firstName) : self
{
$this->firstName = $firstName;
return $this;
}
public function getLastName() : string
{
return $this->lastName;
}
public function setLastName(string $lastName) : self
{
$this->lastName = $lastName;
return $this;
}
}
This is my HAL building command:
$halResource = $this->getResourceGenerator()->fromObject($personCollection, $request);
And at least this is my output:
{
"_total_items": 1,
"_page": 1,
"_page_count": 1,
"_links": {
"self": {
"href": "/api/persons?page=1"
}
},
"_embedded": {
"persons": [
{
"id": 1,
"name": {},
"_links": {
"self": {
"href": "/api/persons/1"
}
}
}
]
}
}
What I want to know: Why is the name attribute empty? Why is the NameEntity not output correctly? Is this a bug?