Completed
Push — master ( c49eb0...d6cf83 )
by Philip
04:31
created

PropertyMetadata::serialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 21
ccs 16
cts 16
cp 1
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 16
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Dontdrinkandroot\RestBundle\Metadata;
4
5
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Method;
6
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Postable;
7
use Dontdrinkandroot\RestBundle\Metadata\Annotation\Puttable;
8
use Metadata\MergeableInterface;
9
use Metadata\PropertyMetadata as BasePropertyMetadata;
10
11
class PropertyMetadata extends BasePropertyMetadata implements MergeableInterface
12
{
13 88
    public function __construct($class, $name)
14
    {
15
        try {
16 88
            parent::__construct($class, $name);
17 32
        } catch (\ReflectionException $e) {
18
            /* Ignore missing property definition as they might just be overridden or virtual and therefore only exist
19
            in the parent class. They will be accessible after merging. */
20
        }
21 88
    }
22
23
    /**
24
     * @var string|null
25
     */
26
    private $type;
27
28
    /**
29
     * @var bool
30
     */
31
    private $excluded;
32
33
    /**
34
     * @var Puttable|null
35
     */
36
    private $puttable;
37
38
    /**
39
     * @var Postable|null
40
     */
41
    private $postable;
42
43
    /**
44
     * @var bool
45
     */
46
    private $includable;
47
48
    /**
49
     * @var string[]|null
50
     */
51
    private $includablePaths;
52
53
    /**
54
     * @var bool
55
     */
56
    private $subResource;
57
58
    /**
59
     * @var Method[]|null
60
     */
61
    private $methods;
62
63
    /**
64
     * @var bool
65
     */
66
    private $association;
67
68
    /**
69
     * @var bool
70
     */
71
    private $collection;
72
73
    /**
74
     * @var bool
75
     */
76
    private $virtual;
77
78
    /**
79
     * @var string|null
80
     */
81
    private $subResourcePath;
82
83 12
    public function isPuttable(): bool
84
    {
85 12
        return null !== $this->puttable;
86
    }
87
88 84
    public function setPuttable(?Puttable $puttable)
89
    {
90 84
        $this->puttable = $puttable;
91 84
    }
92
93 12
    public function getPuttable(): ?Puttable
94
    {
95 12
        return $this->puttable;
96
    }
97
98 14
    public function isPostable(): bool
99
    {
100 14
        return null !== $this->postable;
101
    }
102
103 84
    public function setPostable(?Postable $postable)
104
    {
105 84
        $this->postable = $postable;
106 84
    }
107
108 14
    public function getPostable(): ?Postable
109
    {
110 14
        return $this->postable;
111
    }
112
113 64
    public function isIncludable(): bool
114
    {
115 64
        return $this->getBool($this->includable, false);
116
    }
117
118
    public function isVirtual(): bool
119
    {
120
        return $this->getBool($this->virtual, false);
121
    }
122
123 30
    public function setVirtual(bool $virtual)
124
    {
125 30
        $this->virtual = $virtual;
126 30
    }
127
128 76
    public function setIncludable(bool $includable)
129
    {
130 76
        $this->includable = $includable;
131 76
    }
132
133 12
    public function isSubResource(): bool
134
    {
135 12
        return $this->getBool($this->subResource, false);
136
    }
137
138 72
    public function setSubResource(bool $subResource)
139
    {
140 72
        $this->subResource = $subResource;
141 72
    }
142
143 10
    public function getSubResourcePath(): ?string
144
    {
145 10
        return $this->subResourcePath;
146
    }
147
148
    public function setSubResourcePath(string $subResourcePath)
149
    {
150
        $this->subResourcePath = $subResourcePath;
151
    }
152
153 62
    public function isExcluded(): bool
154
    {
155 62
        return $this->getBool($this->excluded, false);
156
    }
157
158 66
    public function setExcluded(bool $excluded)
159
    {
160 66
        $this->excluded = $excluded;
161 66
    }
162
163
    /**
164
     * @return null|string[]
165
     */
166 48
    public function getIncludablePaths(): ?array
167
    {
168 48
        return $this->includablePaths;
169
    }
170
171
    /**
172
     * @param null|string[] $includablePaths
173
     */
174 76
    public function setIncludablePaths(?array $includablePaths)
175
    {
176 76
        $this->includablePaths = $includablePaths;
177 76
    }
178
179 62
    public function isAssociation(): bool
180
    {
181 62
        return $this->getBool($this->association, false);
182
    }
183
184 72
    public function setAssociation(bool $association)
185
    {
186 72
        $this->association = $association;
187 72
    }
188
189 32
    public function isCollection(): bool
190
    {
191 32
        return $this->getBool($this->collection, false);
192
    }
193
194 72
    public function setCollection(bool $collection)
195
    {
196 72
        $this->collection = $collection;
197 72
    }
198
199 64
    public function getType(): ?string
200
    {
201 64
        return $this->type;
202
    }
203
204 88
    public function setType(?string $type)
205
    {
206 88
        $this->type = $type;
207 88
    }
208
209
    /**
210
     * @param Method[] $methods
211
     */
212 72
    public function setMethods(array $methods)
213
    {
214 72
        $this->methods = $methods;
215 72
    }
216
217 12
    public function merge(MergeableInterface $other)
218
    {
219 12
        if (!$other instanceof PropertyMetadata) {
220
            throw new \InvalidArgumentException('$object must be an instance of PropertyMetadata.');
221
        }
222
223 12
        $this->reflection = $this->mergeField($other->reflection, $this->reflection);
224 12
        $this->type = $this->mergeField($other->type, $this->type);
225 12
        $this->puttable = $this->mergeField($other->puttable, $this->puttable);
226 12
        $this->postable = $this->mergeField($other->postable, $this->postable);
227 12
        $this->excluded = $this->mergeField($other->excluded, $this->excluded);
228 12
        $this->includable = $this->mergeField($other->includable, $this->includable);
229 12
        $this->subResource = $this->mergeField($other->subResource, $this->subResource);
230 12
        $this->includablePaths = $this->mergeField($other->includablePaths, $this->includablePaths);
231 12
        $this->association = $this->mergeField($other->association, $this->association);
232 12
        $this->collection = $this->mergeField($other->collection, $this->collection);
233 12
        $this->subResourcePath = $this->mergeField($other->subResourcePath, $this->subResourcePath);
234 12
        $this->methods = $this->mergeField($other->methods, $this->methods);
235 12
        $this->virtual = $this->mergeField($other->virtual, $this->virtual);
236
237 12
        return $this;
238
    }
239
240 66
    protected function getBool(?bool $value, bool $default)
241
    {
242 66
        if (null === $value) {
243 64
            return $default;
244
        }
245
246 56
        return $value;
247
    }
248
249 12
    private function mergeField($thisValue, $otherValue)
250
    {
251 12
        if (null !== $thisValue) {
252 12
            return $thisValue;
253
        }
254
255 12
        return $otherValue;
256
    }
257
258 38
    public function getMethod(string $methodName): ?Method
259
    {
260 38
        if (null === $this->methods) {
261
            return null;
262
        }
263
264 38
        foreach ($this->methods as $method) {
265 38
            if ($methodName === $method->name) {
266 38
                return $method;
267
            }
268
        }
269
270 10
        return null;
271
    }
272
273
    /**
274
     * {@inheritdoc}
275
     */
276 4
    public function serialize()
277
    {
278 4
        return serialize(
279
            [
280 4
                $this->class,
281 4
                $this->name,
282 4
                $this->type,
283 4
                $this->excluded,
284 4
                $this->puttable,
285 4
                $this->postable,
286 4
                $this->includable,
287 4
                $this->includablePaths,
288 4
                $this->subResource,
289 4
                $this->methods,
290 4
                $this->association,
291 4
                $this->collection,
292 4
                $this->virtual,
293 4
                $this->subResourcePath,
294
            ]
295
        );
296
    }
297
298
    /**
299
     * {@inheritdoc}
300
     */
301 4
    public function unserialize($str)
302
    {
303
        list(
304 4
            $this->class,
305 4
            $this->name,
306 4
            $this->type,
307 4
            $this->excluded,
308 4
            $this->puttable,
309 4
            $this->postable,
310 4
            $this->includable,
311 4
            $this->includablePaths,
312 4
            $this->subResource,
313 4
            $this->methods,
314 4
            $this->association,
315 4
            $this->collection,
316 4
            $this->virtual,
317 4
            $this->subResourcePath
318 4
            ) = unserialize($str);
319
        try {
320 4
            $this->reflection = new \ReflectionProperty($this->class, $this->name);
321 4
            $this->reflection->setAccessible(true);
322 2
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
323
            /* Ignore missing property definition as they might just be overridden or virtual and therefore only exist
324
            in the parent class. They will be accessible after merging. */
325
        }
326 4
    }
327
}
328