Completed
Push — master ( 62827a...ab2585 )
by Philip
01:55
created

Metadata/PropertyMetadata.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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