Hateoas
Hateoas copied to clipboard
Associative Array created in Collection Representation
Hi,
when in PagerfantaFactory::createRepresentation() a new CollectionRepresentation for the current results is created.
If the current results are a Traversabe Object then it will use the function iterator_to_array -> which used the second default parameter true to include the object keys.
{
"total": 3,
"page": 1,
"pages": 1,
"limit": 100,
"_embedded": {
"items": {
"document_id_1": "serialized document 1",
"document_id_2": "serialized document 2",
"document_id_3": "serialized document 3"
}
}
}
whereas I expected it to return the following:
{
"total": 3,
"page": 1,
"pages": 1,
"limit": 100,
"_embedded": {
"items": [
"serialized document 1",
"serialized document 2",
"serialized document 3"
]
}
}
change would be fairly simple:
// Hateoas/Representation/CollectionRepresentation.php:60-62
if ($resources instanceof \Traversable) {
$resources = iterator_to_array($resources, false);
}
I agree with this suggestion.
:+1:
I think the current behaviour is fine. If you pass an associate array, you get an json object.
If you need a json array, you can do array_values(iterator_to_array($collection))
.
For example, I think this will break the code of people using pomm https://github.com/chanmix51/Pomm/blob/master/Pomm/Object/BaseObject.php
Ok, then the other way round would be to check in PagerfantaFactory:
// Hateoas/Representation/Factory/PagerfantaFactory.php:40-42
if (null === $inline) {
$data = $pager->getCurrentPageResults();
if ($data instanceof \Traversable) {
$data = iterator_to_array($data, false);
}
$inline = new CollectionRepresentation($data);
}
Has there been any progress on this issue? I'm hitting this problem at the moment. Is there a good workaround?