Permalink
Browse files

improve resource linkage

  • Loading branch information...
1 parent f962e25 commit ea84908a3371577d6c64f23e205556ad5f3ce8f6 @tuyakhov committed Dec 23, 2016
Showing with 41 additions and 62 deletions.
  1. +9 −6 src/Serializer.php
  2. +32 −56 tests/SerializerTest.php
View
@@ -96,6 +96,7 @@ protected function serializeModel(ResourceInterface $model)
'attributes' => $model->getResourceAttributes($attributes),
]);
+ $included = $this->getIncluded();
$relationships = $model->getResourceRelationships();
if (!empty($relationships)) {
foreach ($relationships as $name => $items) {
@@ -111,12 +112,14 @@ protected function serializeModel(ResourceInterface $model)
}
if (!empty($relationship)) {
- $data['relationships'][$name]['data'] = $relationship;
- if ($model instanceof LinksInterface) {
- $links = $model->getRelationshipLinks($name);
- if (!empty($links)) {
- $data['relationships'][$name]['links'] = Link::serialize($links);
- }
+ if (in_array($name, $included)) {
+ $data['relationships'][$name]['data'] = $relationship;
+ }
+ }
+ if ($model instanceof LinksInterface) {
+ $links = $model->getRelationshipLinks($name);
+ if (!empty($links)) {
+ $data['relationships'][$name]['links'] = Link::serialize($links);
}
}
}
@@ -80,93 +80,71 @@ public function testSerializeModelData()
]
]
], $serializer->serialize($model));
-
- ResourceModel::$fields = ['field1'];
- ResourceModel::$extraFields = ['field2'];
- $this->assertSame([
- 'data' => [
- 'id' => '123',
- 'type' => 'resource-models',
- 'attributes' => [
- 'field1' => 'test',
- ],
- 'links' => [
- 'self' => ['href' => 'http://example.com/resource/123']
- ]
- ]
- ], $serializer->serialize($model));
}
public function testExpand()
{
$serializer = new Serializer();
- $serializedModel = [
+ $compoundModel = $includedModel = [
'id' => '123',
'type' => 'resource-models',
'attributes' => [
'field1' => 'test',
'field2' => 2,
],
- 'relationships' => [
- 'extraField1' => [
- 'data' => ['id' => '123', 'type' => 'resource-models'],
- 'links' => [
- 'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1'],
- 'related' => ['href' => 'http://example.com/resource/123/extraField1'],
- ]
+ ];
+ $includedModel['relationships'] = [
+ 'extraField1' => [
+ 'links' => [
+ 'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1'],
+ 'related' => ['href' => 'http://example.com/resource/123/extraField1'],
+ ]
+ ]
+ ];
+ $compoundModel['relationships'] = [
+ 'extraField1' => [
+ 'data' => ['id' => '123', 'type' => 'resource-models'],
+ 'links' => [
+ 'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1'],
+ 'related' => ['href' => 'http://example.com/resource/123/extraField1'],
]
- ],
- 'links' => [
- 'self' => ['href' => 'http://example.com/resource/123']
]
];
+ $compoundModel['links'] = $includedModel['links'] = [
+ 'self' => ['href' => 'http://example.com/resource/123']
+ ];
$model = new ResourceModel();
ResourceModel::$fields = ['field1', 'field2'];
ResourceModel::$extraFields = ['extraField1'];
$model->extraField1 = new ResourceModel();
- $this->assertSame([
- 'data' => $serializedModel
- ], $serializer->serialize($model));
\Yii::$app->request->setQueryParams(['include' => 'extraField1']);
$this->assertSame([
- 'data' => $serializedModel,
+ 'data' => $compoundModel,
'included' => [
- [
- 'id' => '123',
- 'type' => 'resource-models',
- 'attributes' => [
- 'field1' => 'test',
- 'field2' => 2,
- ],
- 'links' => [
- 'self' => ['href' => 'http://example.com/resource/123']
- ]
- ]
+ $includedModel
]
], $serializer->serialize($model));
\Yii::$app->request->setQueryParams(['include' => 'extraField1,extraField2']);
$this->assertSame([
- 'data' => $serializedModel,
+ 'data' => $compoundModel,
'included' => [
- [
- 'id' => '123',
- 'type' => 'resource-models',
- 'attributes' => [
- 'field1' => 'test',
- 'field2' => 2,
- ],
- 'links' => [
- 'self' => ['href' => 'http://example.com/resource/123']
- ]
- ]
+ $includedModel
]
], $serializer->serialize($model));
\Yii::$app->request->setQueryParams(['include' => 'field1,extraField2']);
+ $compoundModel['relationships'] = [
+ 'extraField1' => [
+ 'links' => [
+ 'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1'],
+ 'related' => ['href' => 'http://example.com/resource/123/extraField1'],
+ ]
+ ]
+ ];
$this->assertSame([
- 'data' => $serializedModel
+ 'data' => $compoundModel
], $serializer->serialize($model));
}
@@ -179,7 +157,6 @@ public function dataProviderSerializeDataProvider()
'attributes' => ['username' => 'Bob'],
'links' => ['self' => ['href' => 'http://example.com/resource/123']],
'relationships' => ['extraField1' => [
- 'data' => ['id' => '123', 'type' => 'resource-models'],
'links' => [
'related' => ['href' => 'http://example.com/resource/123/extraField1'],
'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1']
@@ -193,7 +170,6 @@ public function dataProviderSerializeDataProvider()
'attributes' => ['username' => 'Tom'],
'links' => ['self' => ['href' => 'http://example.com/resource/123']],
'relationships' => ['extraField1' => [
- 'data' => ['id' => '123', 'type' => 'resource-models'],
'links' => [
'related' => ['href' => 'http://example.com/resource/123/extraField1'],
'self' => ['href' => 'http://example.com/resource/123/relationships/extraField1']

0 comments on commit ea84908

Please sign in to comment.