Completed
Push — master ( 58b8f7...b71da8 )
by Asmir
23s queued 20s
created

PropertyMetadata::setUnionDiscriminator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 4
rs 10
ccs 0
cts 0
cp 0
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer\Metadata;
6
7
use JMS\Serializer\Exception\InvalidMetadataException;
8
use JMS\Serializer\Expression\Expression;
9
use Metadata\PropertyMetadata as BasePropertyMetadata;
10
11
class PropertyMetadata extends BasePropertyMetadata
12
{
13
    public const ACCESS_TYPE_PROPERTY = 'property';
14
    public const ACCESS_TYPE_PUBLIC_METHOD = 'public_method';
15
16
    /**
17
     * @var string|null
18
     */
19
    public $sinceVersion;
20
21
    /**
22
     * @var string|null
23
     */
24
    public $untilVersion;
25
26
    /**
27
     * @var string[]|null
28
     */
29
    public $groups;
30
31
    /**
32
     * @var string|null
33
     */
34
    public $serializedName;
35
36
    /**
37
     * @var string|null
38
     */
39
    public $unionDiscriminatorField;
40
41
    /**
42
     * @var array<string, string>|null
43
     */
44
    public $unionDiscriminatorMap;
45
46 280
    /**
47
     * @var array|null
48 280
     */
49
    public $type;
50
51 280
    /**
52 280
     * @var bool
53
     */
54
    public $xmlCollection = false;
55 280
56
    /**
57 280
     * @var bool
58
     */
59 280
    public $xmlCollectionInline = false;
60
61
    /**
62 260
     * @var bool
63
     */
64 260
    public $xmlCollectionSkipWhenEmpty = true;
65 14
66
    /**
67 14
     * @var string|null
68 10
     */
69 6
    public $xmlEntryName;
70 4
71 1
    /**
72 3
     * @var string|null
73 1
     */
74
    public $xmlEntryNamespace;
75 2
76
    /**
77
     * @var string|null
78
     */
79 12
    public $xmlKeyAttribute;
80 6
81 5
    /**
82
     * @var bool
83 1
     */
84
    public $xmlAttribute = false;
85
86
    /**
87
     * @var bool
88 257
     */
89 257
    public $xmlValue = false;
90 257
91
    /**
92 217
     * @var string|null
93
     */
94 217
    public $xmlNamespace;
95 217
96
    /**
97 17
     * @var bool
98
     */
99 17
    public $xmlKeyValuePairs = false;
100 17
101 17
    /**
102 17
     * @var bool
103
     */
104
    public $xmlElementCData = true;
105 17
106
    /**
107 17
     * @var string|null
108 17
     */
109 17
    public $getter;
110 17
111
    /**
112
     * @var string|null
113 1
     */
114
    public $setter;
115 1
116 1
    /**
117 1
     * @var bool
118 1
     */
119 1
    public $inline = false;
120 1
121 1
    /**
122 1
     * @var bool
123 1
     */
124 1
    public $skipWhenEmpty = false;
125 1
126 1
    /**
127 1
     * @var bool
128 1
     */
129 1
    public $readOnly = false;
130 1
131 1
    /**
132 1
     * @var bool
133 1
     */
134 1
    public $xmlAttributeMap = false;
135 1
136 1
    /**
137 1
     * @var int|null
138 1
     */
139 1
    public $maxDepth = null;
140 1
141 1
    /**
142
     * @var string|Expression|null
143
     */
144
    public $excludeIf = null;
145 1
146
    /**
147 1
     * @var bool|null
148
     */
149 1
    public $hasDefault;
150 1
151 1
    /**
152 1
     * @var mixed|null
153 1
     */
154 1
    public $defaultValue;
155 1
156 1
    /**
157 1
     * @internal
158 1
     *
159 1
     * @var bool
160 1
     */
161 1
    public $forceReflectionAccess = false;
162 1
163 1
    public function __construct(string $class, string $name)
164 1
    {
165 1
        parent::__construct($class, $name);
166 1
167 1
        try {
168 1
            $class = $this->getReflection()->getDeclaringClass();
169
            $this->forceReflectionAccess = $class->isInternal() || $class->getProperty($name)->isStatic();
170 1
        } catch (\ReflectionException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
171
        }
172 1
    }
173
174
    private function getReflection(): \ReflectionProperty
175 1
    {
176 1
        return new \ReflectionProperty($this->class, $this->name);
177
    }
178 1
179
    public function setAccessor(string $type, ?string $getter = null, ?string $setter = null): void
180
    {
181 1
        if (self::ACCESS_TYPE_PUBLIC_METHOD === $type) {
182 1
            $class = $this->getReflection()->getDeclaringClass();
183
184 1
            if (empty($getter)) {
185 1
                if ($class->hasMethod('get' . $this->name) && $class->getMethod('get' . $this->name)->isPublic()) {
186
                    $getter = 'get' . $this->name;
187
                } elseif ($class->hasMethod('is' . $this->name) && $class->getMethod('is' . $this->name)->isPublic()) {
188 1
                    $getter = 'is' . $this->name;
189 1
                } elseif ($class->hasMethod('has' . $this->name) && $class->getMethod('has' . $this->name)->isPublic()) {
190
                    $getter = 'has' . $this->name;
191
                } else {
192
                    throw new InvalidMetadataException(sprintf('There is neither a public %s method, nor a public %s method, nor a public %s method in class %s. Please specify which public method should be used for retrieving the value of the property %s.', 'get' . ucfirst($this->name), 'is' . ucfirst($this->name), 'has' . ucfirst($this->name), $this->class, $this->name));
193
                }
194
            }
195
196
            if (empty($setter) && !$this->readOnly) {
197
                if ($class->hasMethod('set' . $this->name) && $class->getMethod('set' . $this->name)->isPublic()) {
198
                    $setter = 'set' . $this->name;
199
                } else {
200
                    throw new InvalidMetadataException(sprintf('There is no public %s method in class %s. Please specify which public method should be used for setting the value of the property %s.', 'set' . ucfirst($this->name), $this->class, $this->name));
201
                }
202
            }
203
        }
204
205
        $this->getter = $getter;
206
        $this->setter = $setter;
207
    }
208
209
    public function setUnionDiscriminator(string $field, array $map): void
210
    {
211
        $this->unionDiscriminatorField = $field;
212
        $this->unionDiscriminatorMap = $map;
213
    }
214
215
    public function setType(array $type): void
216
    {
217
        $this->type = $type;
218
    }
219
220
    public static function isCollectionList(?array $type = null): bool
221
    {
222
        return is_array($type)
223
            && 'array' === $type['name']
224
            && isset($type['params'][0])
225
            && !isset($type['params'][1]);
226
    }
227
228
    public static function isCollectionMap(?array $type = null): bool
229
    {
230
        return is_array($type)
231
            && 'array' === $type['name']
232
            && isset($type['params'][0])
233
            && isset($type['params'][1]);
234
    }
235
236
    protected function serializeToArray(): array
237
    {
238
        return [
239
            $this->sinceVersion,
240
            $this->untilVersion,
241
            $this->groups,
242
            $this->serializedName,
243
            $this->unionDiscriminatorField,
244
            $this->unionDiscriminatorMap,
245
            $this->type,
246
            $this->xmlCollection,
247
            $this->xmlCollectionInline,
248
            $this->xmlEntryName,
249
            $this->xmlKeyAttribute,
250
            $this->xmlAttribute,
251
            $this->xmlValue,
252
            $this->xmlNamespace,
253
            $this->xmlKeyValuePairs,
254
            $this->xmlElementCData,
255
            $this->getter,
256
            $this->setter,
257
            $this->inline,
258
            $this->readOnly,
259
            $this->xmlAttributeMap,
260
            $this->maxDepth,
261
            $this->xmlEntryNamespace,
262
            $this->xmlCollectionSkipWhenEmpty,
263
            $this->excludeIf,
264
            $this->skipWhenEmpty,
265
            $this->forceReflectionAccess,
266
            $this->hasDefault,
267
            $this->defaultValue,
268
            parent::serializeToArray(),
269
        ];
270
    }
271
272
    protected function unserializeFromArray(array $data): void
273
    {
274
        [
275
            $this->sinceVersion,
276
            $this->untilVersion,
277
            $this->groups,
278
            $this->serializedName,
279
            $this->unionDiscriminatorField,
280
            $this->unionDiscriminatorMap,
281
            $this->type,
282
            $this->xmlCollection,
283
            $this->xmlCollectionInline,
284
            $this->xmlEntryName,
285
            $this->xmlKeyAttribute,
286
            $this->xmlAttribute,
287
            $this->xmlValue,
288
            $this->xmlNamespace,
289
            $this->xmlKeyValuePairs,
290
            $this->xmlElementCData,
291
            $this->getter,
292
            $this->setter,
293
            $this->inline,
294
            $this->readOnly,
295
            $this->xmlAttributeMap,
296
            $this->maxDepth,
297
            $this->xmlEntryNamespace,
298
            $this->xmlCollectionSkipWhenEmpty,
299
            $this->excludeIf,
300
            $this->skipWhenEmpty,
301
            $this->forceReflectionAccess,
302
            $this->hasDefault,
303
            $this->defaultValue,
304
            $parentData,
305
        ] = $data;
306
307
        parent::unserializeFromArray($parentData);
308
    }
309
}
310