/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $accumulatingContext = $context->duplicate(false); $hasMatch = false; $hasDoubleMatch = false; foreach ($schema->oneOf as $subSchema) { $subContext = $context->duplicate(false); $walker->applyConstraints($instance, $subSchema, $subContext); if ($subContext->countViolations() === 0) { if (!$hasMatch) { $hasMatch = true; } else { $hasDoubleMatch = true; break; } } else { $accumulatingContext->mergeViolations($subContext); } } if (!$hasMatch) { $context->mergeViolations($accumulatingContext); } if (!$hasMatch || $hasDoubleMatch) { $context->addViolation('instance must match exactly one of the schemas listed in oneOf'); } }
/** * {@inheritdoc} */ public function apply($instance, stdClass $schema, Context $context, Walker $walker) { $accumulatingContext = $context->duplicate(); $hasMatch = false; foreach ($schema->anyOf as $subSchema) { $originalCount = $accumulatingContext->countViolations(); $walker->applyConstraints($instance, $subSchema, $accumulatingContext); if ($accumulatingContext->countViolations() === $originalCount) { $hasMatch = true; break; } } if (!$hasMatch) { $context->mergeViolations($accumulatingContext); $context->addViolation('instance must match at least one of the schemas listed in anyOf'); } }