Skip to content

Commit dcad8fd

Browse files
committed
Add includes and default data
1 parent 24d90da commit dcad8fd

3 files changed

Lines changed: 61 additions & 25 deletions

File tree

Application/Content/Delivery.php

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,18 @@ public function generate(ApiRequest $apiRequest, ApiEntity $apiEntity)
4646
*
4747
* @param ApiRequest $apiRequest
4848
* @param ApiEntity $apiEntity
49+
* @param bool $withRelationships
4950
* @return Data
5051
*/
51-
private function getEntityData(ApiRequest $apiRequest, ApiEntity $apiEntity)
52-
{
52+
private function getEntityData(
53+
ApiRequest $apiRequest,
54+
ApiEntity $apiEntity,
55+
$withRelationships = true
56+
) {
5357
return $this->getFormattedEntityData(
5458
$apiEntity,
5559
$apiRequest->getFilter($apiEntity->getResourceType()),
56-
$apiRequest->getIncludes()
60+
$withRelationships
5761
);
5862
}
5963

@@ -62,10 +66,14 @@ private function getEntityData(ApiRequest $apiRequest, ApiEntity $apiEntity)
6266
*
6367
* @param ApiRequest $apiRequest
6468
* @param ApiEntity $apiEntity
69+
* @param bool $withRelationships
6570
* @return array
6671
*/
67-
private function getIncludedData(ApiRequest $apiRequest, ApiEntity $apiEntity)
68-
{
72+
private function getIncludedData(
73+
ApiRequest $apiRequest,
74+
ApiEntity $apiEntity,
75+
$withRelationships = true
76+
) {
6977
if (!$apiRequest->hasIncludes()) {
7078
return [];
7179
}
@@ -76,7 +84,8 @@ private function getIncludedData(ApiRequest $apiRequest, ApiEntity $apiEntity)
7684
$apiRequest,
7785
$apiEntity,
7886
$entityToInclude,
79-
$result
87+
$result,
88+
$withRelationships
8089
);
8190
}
8291

@@ -100,30 +109,48 @@ private function getMeta(ApiRequest $apiRequest, ApiEntity $apiEntity)
100109
* @param ApiRequest $apiRequest
101110
* @param ApiEntity $apiEntity
102111
* @param $entityToInclude
103-
* @param $result
112+
* @param array $result
113+
* @param $withRelationships
104114
* @return array
105115
*/
106116
private function getFormattedIncludedData(
107117
ApiRequest $apiRequest,
108118
ApiEntity $apiEntity,
109119
$entityToInclude,
110-
array $result
120+
array $result,
121+
$withRelationships
111122
) {
112123
$entities = $apiEntity->{$entityToInclude}();
113124
if (!is_array($entities)) {
114125
$entities = [$entities];
115126
}
116127

128+
$toIncludes = $apiRequest->getIncludes($entityToInclude);
129+
117130
foreach ($entities as $entity) {
118131
if (!is_a($entity, 'GoIntegro\Bundle\EndPointBundle\Application\Model\ApiEntity')) {
119132
continue;
120133
}
121134

135+
136+
if (!empty($toIncludes)) {
137+
foreach ($toIncludes as $toInclude) {
138+
$includes = [];
139+
$includes = $this->getFormattedIncludedData($apiRequest, $entity, $toInclude, $includes, $withRelationships);
140+
if (!empty($includes)) {
141+
foreach ($includes as $include) {
142+
$result[] = $include;
143+
}
144+
}
145+
}
146+
}
147+
148+
122149
$type = $entity->getResourceType();
123150
$result[] = $this->getFormattedEntityData(
124151
$entity,
125152
$apiRequest->getFilter($type),
126-
$apiRequest->getIncludes($entityToInclude)
153+
$withRelationships
127154
);
128155
}
129156

@@ -135,11 +162,11 @@ private function getFormattedIncludedData(
135162
*
136163
* @param ApiEntity $apiEntity
137164
* @param $filter
138-
* @param $include
165+
* @param $withRelationships
139166
* @return Data
140167
*/
141-
private function getFormattedEntityData(ApiEntity $apiEntity, $filter, $include)
168+
private function getFormattedEntityData(ApiEntity $apiEntity, $filter, $withRelationships)
142169
{
143-
return $this->contentFormatter->getFormattedEntityData($apiEntity, $filter, $include);
170+
return $this->contentFormatter->getFormattedEntityData($apiEntity, $filter, $withRelationships);
144171
}
145172
}

Application/Model/ApiEntity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@ public function getData(array $fields);
3535
* @return int|null
3636
*/
3737
public function getRelationshipsMapperType($entity);
38+
39+
public function getRelationships();
40+
41+
public function hasRelationships();
3842
}

Infrastructure/Application/Content/ApiFormatter.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,37 @@ class ApiFormatter implements Formatter
1818
* return the formatted response from and entity and relationships
1919
*
2020
* @param Data $entity
21-
* @param Data[] $relatedEntities
21+
* @param array|\GoIntegro\Bundle\EndPointBundle\Application\Content\Data[] $includedEntities
2222
* @param array $extra
2323
* @return array
2424
*/
25-
public function response(Data $entity, array $relatedEntities, array $extra = [])
25+
public function response(Data $entity, array $includedEntities, array $extra = [])
2626
{
27-
return [
28-
$entity->getType() => $entity->toArray(),
29-
'linked' => $this->getFormattedIncludedData($relatedEntities),
30-
'meta' => $extra,
31-
];
27+
$response[$entity->getType()] = $entity->toArray();
28+
if (!empty($includedEntities)) {
29+
$response['linked'] = $this->getFormattedIncludedData($includedEntities);
30+
}
31+
32+
if (!empty($extra)) {
33+
$response['meta'] = $extra;
34+
}
35+
36+
return $response;
3237
}
3338

3439
/**
3540
* @param ApiEntity $entity
3641
* @param $fields
37-
* @param $includes
42+
* @param bool $withRelationships
3843
* @return Data
3944
*/
40-
public function getFormattedEntityData(ApiEntity $entity, $fields, $includes)
45+
public function getFormattedEntityData(ApiEntity $entity, $fields, $withRelationships = true)
4146
{
4247
$response = $entity->getData($fields);
43-
if (!empty($includes)) {
48+
if ($withRelationships && $entity->hasRelationships()) {
4449
$response['links'] = [];
45-
foreach ($includes as $include) {
46-
$response['links'][$include] = $this->getFormattedRelationshipData($entity, $include);
50+
foreach ($entity->getRelationships() as $relationships) {
51+
$response['links'][$relationships] = $this->getFormattedRelationshipData($entity, $relationships);
4752
}
4853
}
4954

@@ -76,7 +81,7 @@ private function getFormattedIncludedData(array $linkedData)
7681
* @param $include
7782
* @return array|int|string
7883
*/
79-
private function getFormattedRelationshipData(ApiEntity $entity, $include)
84+
public function getFormattedRelationshipData(ApiEntity $entity, $include)
8085
{
8186
$content = $entity->{$include}();
8287
if (empty($content)) {

0 commit comments

Comments
 (0)