Passed
Pull Request — master (#1549)
by
unknown
02:43
created

getRequireAllRequiredProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace JMS\Serializer;
6
7
use JMS\Serializer\Exception\NonVisitableTypeException;
8
use JMS\Serializer\Exception\RuntimeException;
9
use JMS\Serializer\Metadata\ClassMetadata;
10
use JMS\Serializer\Metadata\PropertyMetadata;
11
use JMS\Serializer\Visitor\DeserializationVisitorInterface;
12
13
use function is_float;
14
use function is_int;
15
use function is_string;
16
17
final class JsonDeserializationStrictVisitor extends AbstractVisitor implements DeserializationVisitorInterface
18
{
19
    /** @var JsonDeserializationVisitor */
20
    private $wrappedDeserializationVisitor;
21
22
    /**
23
     * THIS IS ONLY USED FOR UNION DESERIALIZATION WHICH IS NOT SUPPORTED IN XML
24
     *
25
     * @var bool
26
     */
27
    private $requireAllRequiredProperties = false;
28
29
    public function __construct(
30
        int $options = 0,
31
        int $depth = 512
32
    ) {
33
        $this->wrappedDeserializationVisitor = new JsonDeserializationVisitor($options, $depth);
34
    }
35
36
    public function setRequireAllRequiredProperties(bool $requireAllRequiredProperties): void
37
    {
38
        $this->requireAllRequiredProperties = $requireAllRequiredProperties;
39
    }
40
41
    public function getRequireAllRequiredProperties(): bool
42
    {
43
        return $this->requireAllRequiredProperties;
44
    }
45
46
    public function setNavigator(GraphNavigatorInterface $navigator): void
47
    {
48
        parent::setNavigator($navigator);
49
50
        $this->wrappedDeserializationVisitor->setNavigator($navigator);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function visitNull($data, array $type)
57
    {
58
        return null;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64
    public function visitString($data, array $type): ?string
65
    {
66
        if (null === $data) {
67
            return null;
68
        }
69
70
        if (! is_string($data)) {
71
            throw NonVisitableTypeException::fromDataAndType($data, $type);
72
        }
73
74
        return $data;
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function visitBoolean($data, array $type): ?bool
81
    {
82
        if (null === $data) {
83
            return null;
84
        }
85
86
        if (! is_bool($data)) {
87
            throw NonVisitableTypeException::fromDataAndType($data, $type);
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96
    public function visitInteger($data, array $type): ?int
97
    {
98
        if (null === $data) {
99
            return null;
100
        }
101
102
        if (! is_int($data)) {
103
            throw NonVisitableTypeException::fromDataAndType($data, $type);
104
        }
105
106
        return $data;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function visitDouble($data, array $type): ?float
113
    {
114
        if (null === $data) {
115
            return null;
116
        }
117
118
        if (! is_float($data)) {
119
            throw NonVisitableTypeException::fromDataAndType($data, $type);
120
        }
121
122
        return $data;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function visitArray($data, array $type): array
129
    {
130
        try {
131
            return $this->wrappedDeserializationVisitor->visitArray($data, $type);
132
        } catch (RuntimeException $e) {
133
            throw NonVisitableTypeException::fromDataAndType($data, $type, $e);
134
        }
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     */
140
    public function visitDiscriminatorMapProperty($data, ClassMetadata $metadata): string
141
    {
142
        return $this->wrappedDeserializationVisitor->visitDiscriminatorMapProperty($data, $metadata);
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function startVisitingObject(ClassMetadata $metadata, object $object, array $type): void
149
    {
150
        $this->wrappedDeserializationVisitor->startVisitingObject($metadata, $object, $type);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function visitProperty(PropertyMetadata $metadata, $data)
157
    {
158
        return $this->wrappedDeserializationVisitor->visitProperty($metadata, $data);
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164
    public function endVisitingObject(ClassMetadata $metadata, $data, array $type): object
165
    {
166
        return $this->wrappedDeserializationVisitor->endVisitingObject($metadata, $data, $type);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172
    public function getResult($data)
173
    {
174
        return $this->wrappedDeserializationVisitor->getResult($data);
175
    }
176
177
    public function getCurrentObject(): ?object
178
    {
179
        return $this->wrappedDeserializationVisitor->getCurrentObject();
180
    }
181
182
    /**
183
     * {@inheritdoc}
184
     */
185
    public function prepare($data)
186
    {
187
        return $this->wrappedDeserializationVisitor->prepare($data);
188
    }
189
}
190