fractal
fractal copied to clipboard
Cannot appear to have object as collection root
Please let me know if I am being ignorant here...is it the case that a Collection cannot have an object at the root level? I thought Serializers existed so that the output format could be manipulated but it seems that the Scope class will only play with arrays.
Specifically, I was a trying to create a custom serializer to output GeoJSON, a standard in which collections are objects at the root level, e.g.:
class GeoJsonSerializer extends SerializerAbstract
{
public function collection($resourceKey, array $data)
{
$featureCollection = new \StdClass();
$featureCollection->type = 'FeatureCollection';
$featureCollection->features = ["foo","bar"]
return $featureCollection; //does not work
//return [$featureCollection]; //does work but wraps FeatureCollection object in array
}
I am not using this with pagination or meta data so I could in theory strip the square brackets from the string after doing $manager->toJson() but it seem like that defeats the point of having a serializer. Please is there some elegant way around this that I am overlooking?
I got it working by returning an assoc array:
return [
'type' => "FeatureCollection",
'features' => []
];
Dear @PeteDevoy ,
i would try to use the JsonApiSerializer
for this purpose. From the structure of your GeoJson format, this may be quite similar.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.