Completed
Push — master ( b27094...afbc6d )
by Julien
05:50 queued 01:38
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 getRepositoryName() 0 3 1
A getRelation() 0 6 4
A getModelName() 0 3 1
A setKey() 0 5 1
A __construct() 0 5 1
A getKey() 0 3 1
A setModelName() 0 5 1
A getIdGetter() 0 3 1
B getDefaultSerializedModel() 0 20 6
A getIdSerializeKey() 0 9 2
A getIdentifierAttribute() 0 3 1
A getIdKey() 0 9 2
A setRelationList() 0 5 1
A getAttributeList() 0 3 1
A setAttributeList() 0 12 3
A setRepositoryName() 0 5 1
A getAttribute() 0 3 2
A getRelationList() 0 3 1
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]) ? $this->attributeList[$name] : null;
126
    }
127
128
    /**
129
     * getIdentifierAttribute
130
     *
131
     * @return Attribute
132
     */
133
    public function getIdentifierAttribute()
134
    {
135
        return $this->identifierAttribute;
136
    }
137
138
    /**
139
     * Getter for attributeList
140
     *
141
     * @return Attribute[]
142
     */
143
    public function getAttributeList()
144
    {
145
        return $this->attributeList;
146
    }
147
148
    /**
149
     * Setter for attributeList
150
     *
151
     * @param Attribute[] $attributeList
152
     *
153
     * @return ClassMetadata
154
     */
155
    public function setAttributeList($attributeList)
156
    {
157
        $this->attributeList = [];
158
        foreach ($attributeList as $attribute) {
159
            $this->attributeList[$attribute->getSerializedKey()] = $attribute;
160
161
            if ($attribute->isIdentifier()) {
162
                $this->identifierAttribute = $attribute;
163
            }
164
        }
165
166
        return $this;
167
    }
168
169
    /**
170
     * Getter for relationList
171
     *
172
     * @return Relation[]
173
     */
174
    public function getRelationList()
175
    {
176
        return $this->relationList;
177
    }
178
179
    /**
180
     * Setter for relationList
181
     *
182
     * @param Relation[] $relationList
183
     *
184
     * @return ClassMetadata
185
     */
186
    public function setRelationList($relationList)
187
    {
188
        $this->relationList = $relationList;
189
190
        return $this;
191
    }
192
193
    /**
194
     * getRelation
195
     *
196
     * @param string $key
197
     *
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
     *
226
     * @return ClassMetadata
227
     */
228
    public function setRepositoryName($repositoryName)
229
    {
230
        $this->repositoryName = $repositoryName;
231
232
        return $this;
233
    }
234
235
    public function getIdGetter()
236
    {
237
        return 'get' . ucfirst($this->getIdKey());
238
    }
239
240
    public function getIdSerializeKey()
241
    {
242
        if ($this->getIdentifierAttribute()) {
243
            $idAttr = $this->getIdentifierAttribute()
244
                ->getSerializedKey();
245
246
            return $idAttr;
247
        } else {
248
            return 'id';
249
        }
250
    }
251
252
    /**
253
     * return default serialize model with null value or empty array on relations
254
     *
255
     * @return array
256
     */
257
    public function getDefaultSerializedModel()
258
    {
259
        $out = [];
260
        $attributeList = $this->getAttributeList();
261
        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...
262
            foreach ($attributeList as $attribute) {
263
                $out[$attribute->getSerializedKey()] = null;
264
            }
265
        }
266
267
        $relationList = $this->getRelationList();
268
        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...
269
            foreach ($relationList as $relation) {
270
                if ($relation->isOneToMany()) {
271
                    $out[$relation->getSerializedKey()] = [];
272
                }
273
            }
274
        }
275
276
        return $out;
277
    }
278
279
    private function getIdKey()
280
    {
281
        if ($this->getIdentifierAttribute()) {
282
            $idAttr = $this->getIdentifierAttribute()
283
                ->getAttributeName();
284
285
            return $idAttr;
286
        } else {
287
            return 'id';
288
        }
289
    }
290
}
291