Completed
Pull Request — master (#68)
by Julien
17:36
created

ClassMetadata::getDefaultSerializedModel()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 8.7624
cc 6
eloc 12
nc 4
nop 0
1
<?php
2
3
namespace Mapado\RestClientSdk\Mapping;
4
5
/**
6
 * Class ClassMetadata
7
 * @author Julien Deniau <[email protected]>
8
 */
9
class ClassMetadata
10
{
11
    /**
12
     * Model name (entity class with full namespace, ie: "Foo\Entity\Article").
13
     *
14
     * @var string
15
     * @access private
16
     */
17
    private $modelName;
18
19
    /**
20
     * Model key, used as path prefix for API calls.
21
     *
22
     * @var string
23
     * @access private
24
     */
25
    private $key;
26
27
    /**
28
     * Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository").
29
     *
30
     * @var string
31
     * @access private
32
     */
33
    private $repositoryName;
34
35
    /**
36
     * attributeList
37
     *
38
     * @var Attribute[]
39
     * @access private
40
     */
41
    private $attributeList;
42
43
    /**
44
     * relationList
45
     *
46
     * @var Relation[]
47
     * @access private
48
     */
49
    private $relationList;
50
51
    /**
52
     * identifierAttribute
53
     *
54
     * @var Attribute
55
     * @access private
56
     */
57
    private $identifierAttribute;
58
59
    /**
60
     * Constructor.
61
     *
62
     * @param string $key
63
     * @param string $modelName
64
     * @param string $repositoryName
65
     * @access public
66
     */
67
    public function __construct($key, $modelName, $repositoryName)
68
    {
69
        $this->key = $key;
70
        $this->modelName = $modelName;
71
        $this->repositoryName = $repositoryName;
72
    }
73
74
    /**
75
     * Getter for modelName
76
     *
77
     * @return string
78
     */
79
    public function getModelName()
80
    {
81
        return $this->modelName;
82
    }
83
84
    /**
85
     * Setter for modelName
86
     *
87
     * @param string $modelName
88
     * @return ClassMetadata
89
     */
90
    public function setModelName($modelName)
91
    {
92
        $this->modelName = $modelName;
93
        return $this;
94
    }
95
96
    /**
97
     * Getter for key
98
     *
99
     * @return string
100
     */
101
    public function getKey()
102
    {
103
        return $this->key;
104
    }
105
106
    /**
107
     * Setter for key
108
     *
109
     * @param string $key
110
     * @return ClassMetadata
111
     */
112
    public function setKey($key)
113
    {
114
        $this->key = $key;
115
116
        return $this;
117
    }
118
119
    /**
120
     * getAttribute
121
     *
122
     * @param string $name
123
     * @access public
124
     * @return Attribute
125
     */
126
    public function getAttribute($name)
127
    {
128
        return isset($this->attributeList[$name]) ? $this->attributeList[$name] : null;
129
    }
130
131
    /**
132
     * getIdentifierAttribute
133
     *
134
     * @access public
135
     * @return Attribute
136
     */
137
    public function getIdentifierAttribute()
138
    {
139
        return $this->identifierAttribute;
140
    }
141
142
    /**
143
     * Getter for attributeList
144
     *
145
     * @return Attribute[]
146
     */
147
    public function getAttributeList()
148
    {
149
        return $this->attributeList;
150
    }
151
152
    /**
153
     * Setter for attributeList
154
     *
155
     * @param Attribute[] $attributeList
156
     * @return ClassMetadata
157
     */
158
    public function setAttributeList($attributeList)
159
    {
160
        $this->attributeList = [];
161
        foreach ($attributeList as $attribute) {
162
            $this->attributeList[$attribute->getSerializedKey()] = $attribute;
163
164
            if ($attribute->isIdentifier()) {
165
                $this->identifierAttribute = $attribute;
166
            }
167
        }
168
        return $this;
169
    }
170
171
    /**
172
     * Getter for relationList
173
     *
174
     * @return Relation[]
175
     */
176
    public function getRelationList()
177
    {
178
        return $this->relationList;
179
    }
180
181
    /**
182
     * Setter for relationList
183
     *
184
     * @param Relation[] $relationList
185
     * @return ClassMetadata
186
     */
187
    public function setRelationList($relationList)
188
    {
189
        $this->relationList = $relationList;
190
        return $this;
191
    }
192
193
    /**
194
     * getRelation
195
     *
196
     * @param string $key
197
     * @access public
198
     * @return Relation|null
199
     */
200
    public function getRelation($key)
201
    {
202
        if (!empty($this->relationList)) {
203
            foreach ($this->relationList as $relation) {
204
                if ($relation->getSerializedKey() == $key) {
205
                    return $relation;
206
                }
207
            }
208
        }
209
    }
210
211
    /**
212
     * Getter for repositoryName
213
     *
214
     * @return string
215
     */
216
    public function getRepositoryName()
217
    {
218
        return $this->repositoryName;
219
    }
220
221
    /**
222
     * Setter for repositoryName
223
     *
224
     * @param string $repositoryName
225
     * @return ClassMetadata
226
     */
227
    public function setRepositoryName($repositoryName)
228
    {
229
        $this->repositoryName = $repositoryName;
230
        return $this;
231
    }
232
233
    public function getIdGetter()
234
    {
235
        return 'get' . ucfirst($this->getIdKey());
236
    }
237
238
    public function getIdSerializeKey()
239
    {
240
        if ($this->getIdentifierAttribute()) {
241
            $idAttr = $this->getIdentifierAttribute()
242
                ->getSerializedKey();
243
            return $idAttr;
244
        } else {
245
            return 'id';
246
        }
247
    }
248
249
    /**
250
     * return default serialize model with null value or empty array on relations
251
     *
252
     * @return array
253
     */
254
    public function getDefaultSerializedModel()
255
    {
256
        $out = [];
257
        $attributeList = $this->getAttributeList();
258
        if ($attributeList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attributeList of type Mapado\RestClientSdk\Mapping\Attribute[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
259
            foreach ($attributeList as $attribute) {
260
                $out[$attribute->getSerializedKey()] = null;
261
            }
262
        }
263
264
        $relationList = $this->getRelationList();
265
        if ($relationList) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $relationList of type Mapado\RestClientSdk\Mapping\Relation[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
266
            foreach ($relationList as $relation) {
267
                if ($relation->isOneToMany()) {
268
                    $out[$relation->getSerializedKey()] = [];
269
                }
270
            }
271
        }
272
273
        return $out;
274
    }
275
276
    private function getIdKey()
277
    {
278
        if ($this->getIdentifierAttribute()) {
279
            $idAttr = $this->getIdentifierAttribute()
280
                ->getAttributeName();
281
            return $idAttr;
282
        } else {
283
            return 'id';
284
        }
285
    }
286
}
287