Completed
Pull Request — master (#74)
by Julien
07:02
created

ClassMetadata   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 278
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 278
rs 9.8
c 0
b 0
f 0
wmc 31

18 Methods

Rating   Name   Duplication   Size   Complexity  
A getRelationList() 0 3 1
A getModelName() 0 3 1
A getIdGetter() 0 3 1
A getRepositoryName() 0 3 1
B getDefaultSerializedModel() 0 20 6
A setKey() 0 5 1
A getIdSerializeKey() 0 8 2
A getIdentifierAttribute() 0 3 1
A getIdKey() 0 8 2
A __construct() 0 5 1
A setRelationList() 0 5 1
A getKey() 0 3 1
A getAttributeList() 0 3 1
A setModelName() 0 5 1
A setAttributeList() 0 12 3
A setRepositoryName() 0 5 1
A getAttribute() 0 5 2
A getRelation() 0 6 4
1
<?php
2
3
namespace Mapado\RestClientSdk\Mapping;
4
5
/**
6
 * Class ClassMetadata
7
 *
8
 * @author Julien Deniau <[email protected]>
9
 */
10
class ClassMetadata
11
{
12
    /**
13
     * Model name (entity class with full namespace, ie: "Foo\Entity\Article").
14
     *
15
     * @var string
16
     */
17
    private $modelName;
18
19
    /**
20
     * Model key, used as path prefix for API calls.
21
     *
22
     * @var string
23
     */
24
    private $key;
25
26
    /**
27
     * Repository name (repository class with full namespace, ie: "Foo\Repository\ArticleRepository").
28
     *
29
     * @var string
30
     */
31
    private $repositoryName;
32
33
    /**
34
     * attributeList
35
     *
36
     * @var Attribute[]
37
     */
38
    private $attributeList;
39
40
    /**
41
     * relationList
42
     *
43
     * @var Relation[]
44
     */
45
    private $relationList;
46
47
    /**
48
     * identifierAttribute
49
     *
50
     * @var Attribute
51
     */
52
    private $identifierAttribute;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param string $key
58
     * @param string $modelName
59
     * @param string $repositoryName
60
     */
61
    public function __construct($key, $modelName, $repositoryName)
62
    {
63
        $this->key = $key;
64
        $this->modelName = $modelName;
65
        $this->repositoryName = $repositoryName;
66
    }
67
68
    /**
69
     * Getter for modelName
70
     *
71
     * @return string
72
     */
73
    public function getModelName()
74
    {
75
        return $this->modelName;
76
    }
77
78
    /**
79
     * Setter for modelName
80
     *
81
     * @param string $modelName
82
     *
83
     * @return ClassMetadata
84
     */
85
    public function setModelName($modelName)
86
    {
87
        $this->modelName = $modelName;
88
89
        return $this;
90
    }
91
92
    /**
93
     * Getter for key
94
     *
95
     * @return string
96
     */
97
    public function getKey()
98
    {
99
        return $this->key;
100
    }
101
102
    /**
103
     * Setter for key
104
     *
105
     * @param string $key
106
     *
107
     * @return ClassMetadata
108
     */
109
    public function setKey($key)
110
    {
111
        $this->key = $key;
112
113
        return $this;
114
    }
115
116
    /**
117
     * getAttribute
118
     *
119
     * @param string $name
120
     *
121
     * @return Attribute
122
     */
123
    public function getAttribute($name)
124
    {
125
        return isset($this->attributeList[$name])
126
            ? $this->attributeList[$name]
127
            : null;
128
    }
129
130
    /**
131
     * getIdentifierAttribute
132
     *
133
     * @return Attribute
134
     */
135
    public function getIdentifierAttribute()
136
    {
137
        return $this->identifierAttribute;
138
    }
139
140
    /**
141
     * Getter for attributeList
142
     *
143
     * @return Attribute[]
144
     */
145
    public function getAttributeList()
146
    {
147
        return $this->attributeList;
148
    }
149
150
    /**
151
     * Setter for attributeList
152
     *
153
     * @param Attribute[] $attributeList
154
     *
155
     * @return ClassMetadata
156
     */
157
    public function setAttributeList($attributeList)
158
    {
159
        $this->attributeList = [];
160
        foreach ($attributeList as $attribute) {
161
            $this->attributeList[$attribute->getSerializedKey()] = $attribute;
162
163
            if ($attribute->isIdentifier()) {
164
                $this->identifierAttribute = $attribute;
165
            }
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
     *
186
     * @return ClassMetadata
187
     */
188
    public function setRelationList($relationList)
189
    {
190
        $this->relationList = $relationList;
191
192
        return $this;
193
    }
194
195
    /**
196
     * getRelation
197
     *
198
     * @param string $key
199
     *
200
     * @return Relation|null
201
     */
202
    public function getRelation($key)
203
    {
204
        if (!empty($this->relationList)) {
205
            foreach ($this->relationList as $relation) {
206
                if ($relation->getSerializedKey() == $key) {
207
                    return $relation;
208
                }
209
            }
210
        }
211
    }
212
213
    /**
214
     * Getter for repositoryName
215
     *
216
     * @return string
217
     */
218
    public function getRepositoryName()
219
    {
220
        return $this->repositoryName;
221
    }
222
223
    /**
224
     * Setter for repositoryName
225
     *
226
     * @param string $repositoryName
227
     *
228
     * @return ClassMetadata
229
     */
230
    public function setRepositoryName($repositoryName)
231
    {
232
        $this->repositoryName = $repositoryName;
233
234
        return $this;
235
    }
236
237
    public function getIdGetter()
238
    {
239
        return 'get' . ucfirst($this->getIdKey());
240
    }
241
242
    public function getIdSerializeKey()
243
    {
244
        if ($this->getIdentifierAttribute()) {
245
            $idAttr = $this->getIdentifierAttribute()->getSerializedKey();
246
247
            return $idAttr;
248
        } else {
249
            return 'id';
250
        }
251
    }
252
253
    /**
254
     * return default serialize model with null value or empty array on relations
255
     *
256
     * @return array
257
     */
258
    public function getDefaultSerializedModel()
259
    {
260
        $out = [];
261
        $attributeList = $this->getAttributeList();
262
        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...
263
            foreach ($attributeList as $attribute) {
264
                $out[$attribute->getSerializedKey()] = null;
265
            }
266
        }
267
268
        $relationList = $this->getRelationList();
269
        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...
270
            foreach ($relationList as $relation) {
271
                if ($relation->isOneToMany()) {
272
                    $out[$relation->getSerializedKey()] = [];
273
                }
274
            }
275
        }
276
277
        return $out;
278
    }
279
280
    private function getIdKey()
281
    {
282
        if ($this->getIdentifierAttribute()) {
283
            $idAttr = $this->getIdentifierAttribute()->getAttributeName();
284
285
            return $idAttr;
286
        } else {
287
            return 'id';
288
        }
289
    }
290
}
291