联盟\分形变换项目在数组内

时间:2016-04-14 11:47:49

标签: php api laravel transformer

需要对League \ Fractal有点帮助,我试图创建对象来模仿GoogleMapApi DataLayer的FeatureCollection

FeatureCollection
+ type
+ features => [
  Feature
  {
     type,
     geometry => 
          {
          type,
          coordinates => [latitude, longitudes]
          }
   }
]

我已成功为Geometry和Feature创建Transformer,但是FeatureCollection存在问题,因为属性功能是一个元素为Feature的数组。

{
    "type": "FeatureCollection",
    "features": [
        [],
        [],
    ]
}

如何正确转换要素元素的内部?

1 个答案:

答案 0 :(得分:0)

不知怎的,我设法理解这个概念。所以基本上你需要使用ObjectTransformer本身返回 集合 ,而不仅仅是Object。

只需要在FeatureCollectionTransformerClass

中添加它
protected $defaultIncludes =[
    'features'
];

public function includeFeatures(FeatureCollection $collection){
    return $this->collection($collection->features, new FeatureTransformer());
}