1+ <?php namespace App \ModelSerializers ;
2+ /*
3+ * Copyright 2024 OpenStack Foundation
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ * http://www.apache.org/licenses/LICENSE-2.0
8+ * Unless required by applicable law or agreed to in writing, software
9+ * distributed under the License is distributed on an "AS IS" BASIS,
10+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+ * See the License for the specific language governing permissions and
12+ * limitations under the License.
13+ **/
14+
15+ use App \Models \Foundation \UserStories \UserStory ;
16+ use Libs \ModelSerializers \Many2OneExpandSerializer ;
17+ use Libs \ModelSerializers \One2ManyExpandSerializer ;
18+ use ModelSerializers \SilverStripeSerializer ;
19+
20+ /**
21+ * Class UserStorySerializer
22+ * @package App\ModelSerializers
23+ */
24+ class UserStorySerializer extends SilverStripeSerializer
25+ {
26+ protected static $ array_mappings = [
27+ 'Name ' => 'name:json_string ' ,
28+ 'Description ' => 'description:json_string ' ,
29+ 'ShortDescription ' => 'short_description:json_string ' ,
30+ 'Link ' => 'link:json_string ' ,
31+ 'Active ' => 'active:json_boolean ' ,
32+ 'MillionCoreClub ' => 'is_million_core_club:json_boolean ' ,
33+ 'OrganizationId ' => 'organization_id:json_int ' ,
34+ 'IndustryId ' => 'industry_id:json_int ' ,
35+ 'LocationId ' => 'location_id:json_int ' ,
36+ 'ImageId ' => 'image_id:json_int ' ,
37+ ];
38+
39+ protected static $ allowed_relations = [
40+ 'tags '
41+ ];
42+
43+ /**
44+ * @param null $expand
45+ * @param array $fields
46+ * @param array $relations
47+ * @param array $params
48+ * @return array
49+ */
50+ public function serialize ($ expand = null , array $ fields = [], array $ relations = [], array $ params = [])
51+ {
52+ $ user_story = $ this ->object ;
53+ if (!$ user_story instanceof UserStory) return [];
54+ $ values = parent ::serialize ($ expand , $ fields , $ relations , $ params );
55+
56+ if (in_array ('tags ' , $ relations ) && !isset ($ values ['tags ' ])) {
57+ $ tags = [];
58+ foreach ($ user_story ->getTags () as $ tag ) {
59+ $ tags [] = $ tag ->getId ();
60+ }
61+ $ values ['tags ' ] = $ tags ;
62+ }
63+
64+ return $ values ;
65+ }
66+
67+ protected static $ expand_mappings = [
68+ 'organization ' => [
69+ 'type ' => One2ManyExpandSerializer::class,
70+ 'original_attribute ' => 'organization_id ' ,
71+ 'getter ' => 'getOrganization ' ,
72+ 'has ' => 'hasOrganization '
73+ ],
74+ 'industry ' => [
75+ 'type ' => One2ManyExpandSerializer::class,
76+ 'original_attribute ' => 'industry_id ' ,
77+ 'getter ' => 'getIndustry ' ,
78+ 'has ' => 'hasIndustry '
79+ ],
80+ 'location ' => [
81+ 'type ' => One2ManyExpandSerializer::class,
82+ 'original_attribute ' => 'location_id ' ,
83+ 'getter ' => 'getLocation ' ,
84+ 'has ' => 'hasLocation '
85+ ],
86+ 'image ' => [
87+ 'type ' => One2ManyExpandSerializer::class,
88+ 'original_attribute ' => 'image_id ' ,
89+ 'getter ' => 'getImage ' ,
90+ 'has ' => 'hasImage '
91+ ],
92+ 'tags ' => [
93+ 'type ' => Many2OneExpandSerializer::class,
94+ 'getter ' => 'getTags ' ,
95+ ],
96+ ];
97+ }
0 commit comments