Skip to content

Commit 0340bb1

Browse files
committed
chore: refactor ConsultantSerializer
1 parent 860a9e3 commit 0340bb1

3 files changed

Lines changed: 60 additions & 63 deletions

File tree

app/ModelSerializers/Marketplace/ConsultantSerializer.php

Lines changed: 58 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* limitations under the License.
1313
**/
1414
use App\Models\Foundation\Marketplace\Consultant;
15+
use Libs\ModelSerializers\Many2OneExpandSerializer;
1516
use ModelSerializers\SerializerRegistry;
1617
/**
1718
* Class ConsultantSerializer
@@ -41,75 +42,75 @@ public function serialize($expand = null, array $fields = [], array $relations =
4142
$consultant = $this->object;
4243
if(!$consultant instanceof Consultant) return [];
4344
$values = parent::serialize($expand, $fields, $relations, $params);
44-
45-
if(in_array('offices', $relations)){
46-
$res = [];
47-
foreach ($consultant->getOffices() as $office){
48-
$res[] = SerializerRegistry::getInstance()
49-
->getSerializer($office)
50-
->serialize($expand);
51-
}
52-
$values['offices'] = $res;
53-
}
54-
55-
if(in_array('clients', $relations)){
56-
$res = [];
57-
foreach ($consultant->getClients() as $client){
58-
$res[] = SerializerRegistry::getInstance()
59-
->getSerializer($client)
60-
->serialize($expand);
45+
if(in_array('offices', $relations) && !isset($values['offices'])) {
46+
$offices = [];
47+
foreach ($consultant->getOffices() as $e) {
48+
$offices[] = $e->getId();
6149
}
62-
$values['clients'] = $res;
50+
$values['offices'] = $offices;
6351
}
64-
65-
if(in_array('spoken_languages', $relations)){
66-
$res = [];
67-
foreach ($consultant->getSpokenLanguages() as $lang){
68-
$res[] = SerializerRegistry::getInstance()
69-
->getSerializer($lang)
70-
->serialize($expand);
52+
if(in_array('clients', $relations) && !isset($values['clients'])) {
53+
$clients = [];
54+
foreach ($consultant->getClients() as $e) {
55+
$clients[] = $e->getId();
7156
}
72-
$values['spoken_languages'] = $res;
57+
$values['clients'] = $clients;
7358
}
74-
75-
if(in_array('configuration_management_expertise', $relations)){
76-
$res = [];
77-
foreach ($consultant->getConfigurationManagementExpertise() as $exp){
78-
$res[] = SerializerRegistry::getInstance()
79-
->getSerializer($exp)
80-
->serialize($expand);
59+
if(in_array('spoken_languages', $relations) && !isset($values['spoken_languages'])) {
60+
$spoken_languages = [];
61+
foreach ($consultant->getSpokenLanguages() as $e) {
62+
$spoken_languages[] = $e->getId();
8163
}
82-
$values['configuration_management_expertise'] = $res;
64+
$values['spoken_languages'] = $spoken_languages;
8365
}
84-
85-
if(in_array('expertise_areas', $relations)){
86-
$res = [];
87-
foreach ($consultant->getExpertiseAreas() as $area){
88-
$res[] = SerializerRegistry::getInstance()
89-
->getSerializer($area)
90-
->serialize($expand);
66+
if(in_array('configuration_management_expertise', $relations) && !isset($values['configuration_management_expertise'])) {
67+
$configuration_management_expertise = [];
68+
foreach ($consultant->getConfigurationManagementExpertise() as $e) {
69+
$configuration_management_expertise[] = $e->getId();
9170
}
92-
$values['expertise_areas'] = $res;
71+
$values['configuration_management_expertise'] = $configuration_management_expertise;
9372
}
94-
95-
if(in_array('services_offered', $relations)){
96-
$res = [];
97-
foreach ($consultant->getServicesOffered() as $service){
98-
$res[] = SerializerRegistry::getInstance()
99-
->getSerializer($service)
100-
->serialize($expand);
73+
if(in_array('expertise_areas', $relations) && !isset($values['expertise_areas'])) {
74+
$expertise_areas = [];
75+
foreach ($consultant->getExpertiseAreas() as $e) {
76+
$expertise_areas[] = $e->getId();
10177
}
102-
$values['services_offered'] = $res;
78+
$values['expertise_areas'] = $expertise_areas;
10379
}
104-
105-
if (!empty($expand)) {
106-
$exp_expand = explode(',', $expand);
107-
foreach ($exp_expand as $relation) {
108-
switch (trim($relation)) {
109-
110-
}
80+
if(in_array('services_offered', $relations) && !isset($values['services_offered'])) {
81+
$services_offered = [];
82+
foreach ($consultant->getServicesOffered() as $e) {
83+
$services_offered[] = $e->getId();
11184
}
85+
$values['services_offered'] = $services_offered;
11286
}
11387
return $values;
11488
}
89+
90+
protected static $expand_mappings = [
91+
'offices' => [
92+
'type' => Many2OneExpandSerializer::class,
93+
'getter' => 'getOffices',
94+
],
95+
'clients' => [
96+
'type' => Many2OneExpandSerializer::class,
97+
'getter' => 'getClients',
98+
],
99+
'spoken_languages' => [
100+
'type' => Many2OneExpandSerializer::class,
101+
'getter' => 'getSpokenLanguages',
102+
],
103+
'configuration_management_expertise' => [
104+
'type' => Many2OneExpandSerializer::class,
105+
'getter' => 'getConfigurationManagementExpertise',
106+
],
107+
'expertise_areas' => [
108+
'type' => Many2OneExpandSerializer::class,
109+
'getter' => 'getExpertiseAreas',
110+
],
111+
'services_offered' => [
112+
'type' => Many2OneExpandSerializer::class,
113+
'getter' => 'getServicesOffered',
114+
],
115+
];
115116
}

app/ModelSerializers/Marketplace/ConsultantServiceOfferedTypeSerializer.php

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@
2020
*/
2121
final class ConsultantServiceOfferedTypeSerializer extends SilverStripeSerializer
2222
{
23-
/**
24-
* @var array
25-
*/
26-
protected static $array_mappings = [
27-
28-
];
2923

3024
protected static $allowed_relations = [
3125
'service_offered_type',

app/Models/Foundation/Marketplace/ConsultantServiceOfferedType.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
class ConsultantServiceOfferedType extends BaseEntity
2323
{
24+
2425
/**
2526
* @ORM\ManyToOne(targetEntity="Consultant", inversedBy="services_offered")
2627
* @ORM\JoinColumn(name="ConsultantID", referencedColumnName="ID")
@@ -65,4 +66,5 @@ public function getRegion()
6566
{
6667
return $this->region;
6768
}
69+
6870
}

0 commit comments

Comments
 (0)