Skip to content

Commit 0545d5d

Browse files
authored
fix: company service serializer expand/fields/relations logic fix (#254)
Signed-off-by: romanetar <[email protected]>
1 parent 9712c05 commit 0545d5d

File tree

2 files changed

+20
-16
lines changed

2 files changed

+20
-16
lines changed

app/ModelSerializers/Marketplace/CompanyServiceSerializer.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class CompanyServiceSerializer extends SilverStripeSerializer
3434
];
3535

3636
protected static $allowed_relations = [
37+
'company',
3738
'reviews',
39+
'type',
3840
];
3941

4042
/**
@@ -53,28 +55,30 @@ public function serialize($expand = null, array $fields = [], array $relations =
5355
if (!empty($expand)) {
5456
$exp_expand = explode(',', $expand);
5557
foreach ($exp_expand as $relation) {
56-
switch (trim($relation)) {
57-
case 'company': {
58-
unset($values['company_id']);
59-
$values['company'] = SerializerRegistry::getInstance()->getSerializer($company_service->getCompany())->serialize(null, [], ['none']);;
60-
}
61-
break;
62-
case 'type': {
63-
unset($values['type_id']);
64-
$values['type'] = SerializerRegistry::getInstance()->getSerializer($company_service->getType())->serialize(null, [], ['none']);;
65-
}
66-
break;
58+
$relation = trim($relation);
59+
if (!in_array($relation, $relations)) continue;
60+
switch ($relation) {
61+
case 'company':
62+
{
63+
unset($values['company_id']);
64+
$values['company'] = SerializerRegistry::getInstance()->getSerializer($company_service->getCompany())->serialize(null, [], ['none']);
65+
}
66+
break;
67+
case 'type':
68+
{
69+
unset($values['type_id']);
70+
$values['type'] = SerializerRegistry::getInstance()->getSerializer($company_service->getType())->serialize(null, [], ['none']);
71+
}
72+
break;
6773
case 'reviews':
68-
{
69-
if(in_array('reviews', $relations)){
74+
{
7075
$reviews = [];
7176
foreach ($company_service->getApprovedReviews() as $r) {
7277
$reviews[] = SerializerRegistry::getInstance()->getSerializer($r)->serialize();
7378
}
7479
$values['reviews'] = $reviews;
7580
}
76-
}
77-
break;
81+
break;
7882
}
7983
}
8084
}

app/Models/Foundation/Main/Companies/Company.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ public function getSponsorships(){
634634
/**
635635
* @return string
636636
*/
637-
public function getUrlSegment(): string{
637+
public function getUrlSegment(): ?string{
638638
return $this->url_segment;
639639
}
640640

0 commit comments

Comments
 (0)