Skip to content

Commit 91bb937

Browse files
author
Kirill Nesmeyanov
committed
Add "skip when" logic
1 parent 8594b21 commit 91bb937

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/Type/ObjectType.php

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,23 +86,38 @@ protected function normalizeObject(object $object, LocalContext $context): array
8686
foreach ($this->metadata->getProperties() as $meta) {
8787
$context->enter(new ObjectPropertyEntry($meta->getName()));
8888

89+
90+
// Skip the property when not readable
8991
if (!$this->accessor->isReadable($object, $meta)) {
9092
continue;
9193
}
9294

93-
$info = $meta->findTypeInfo();
9495

96+
// Assert that type is present
97+
$info = $meta->findTypeInfo();
9598
if ($info === null) {
9699
throw MissingFieldTypeException::createFromContext(
97100
field: $meta->getName(),
98101
context: $context,
99102
);
100103
}
101104

102-
$type = $info->getType();
103105

104106
$fieldValue = $this->accessor->getValue($object, $meta);
105107

108+
109+
// Skip the property when condition is matched
110+
$skip = $meta->findSkipCondition();
111+
if ($skip !== null) {
112+
$condition = $skip->getType();
113+
114+
// Skip when condition is matched
115+
if ($condition->match($fieldValue, $context)) {
116+
continue;
117+
}
118+
}
119+
120+
$type = $info->getType();
106121
try {
107122
$result[$meta->getExportName()] = $type->cast($fieldValue, $context);
108123
} catch (FieldExceptionInterface|MappingExceptionInterface $e) {
@@ -183,13 +198,15 @@ private function denormalizeObject(array $value, object $object, LocalContext $c
183198
foreach ($this->metadata->getProperties() as $meta) {
184199
$context->enter(new ObjectPropertyEntry($meta->getExportName()));
185200

201+
// Skip the property when not writable
186202
if (!$this->accessor->isWritable($object, $meta)) {
187203
continue;
188204
}
189205

190206
switch (true) {
191207
// In case of value has been passed
192208
case \array_key_exists($meta->getExportName(), $value):
209+
// Assert that type is present
193210
$info = $meta->findTypeInfo();
194211

195212
if ($info === null) {
@@ -199,8 +216,8 @@ private function denormalizeObject(array $value, object $object, LocalContext $c
199216
);
200217
}
201218

202-
$type = $info->getType();
203219
$fieldValue = $value[$meta->getExportName()];
220+
$type = $info->getType();
204221

205222
try {
206223
$propertyValue = $type->cast($fieldValue, $context);

0 commit comments

Comments
 (0)