function UndefinedConstraint::applyDefaultValues
Apply default values
Parameters
mixed $value:
mixed $schema:
JsonPointer $path:
1 call to UndefinedConstraint::applyDefaultValues()
- UndefinedConstraint::validateCommonProperties in vendor/
justinrainbow/ json-schema/ src/ JsonSchema/ Constraints/ UndefinedConstraint.php - Validates common properties
File
-
vendor/
justinrainbow/ json-schema/ src/ JsonSchema/ Constraints/ UndefinedConstraint.php, line 241
Class
- UndefinedConstraint
- The UndefinedConstraint Constraints
Namespace
JsonSchema\ConstraintsCode
protected function applyDefaultValues(&$value, $schema, $path) {
// only apply defaults if feature is enabled
if (!$this->factory
->getConfig(self::CHECK_MODE_APPLY_DEFAULTS)) {
return;
}
// apply defaults if appropriate
$requiredOnly = $this->factory
->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS);
if (isset($schema->properties) && LooseTypeCheck::isObject($value)) {
// $value is an object or assoc array, and properties are defined - treat as an object
foreach ($schema->properties as $currentProperty => $propertyDefinition) {
$propertyDefinition = $this->factory
->getSchemaStorage()
->resolveRefSchema($propertyDefinition);
if (!LooseTypeCheck::propertyExists($value, $currentProperty) && property_exists($propertyDefinition, 'default') && $this->shouldApplyDefaultValue($requiredOnly, $propertyDefinition, $currentProperty, $schema)) {
// assign default value
if (is_object($propertyDefinition->default)) {
LooseTypeCheck::propertySet($value, $currentProperty, clone $propertyDefinition->default);
}
else {
LooseTypeCheck::propertySet($value, $currentProperty, $propertyDefinition->default);
}
$this->appliedDefaults[] = $currentProperty;
}
}
}
elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
$items = array();
if (LooseTypeCheck::isArray($schema->items)) {
$items = $schema->items;
}
elseif (isset($schema->minItems) && count($value) < $schema->minItems) {
$items = array_fill(count($value), $schema->minItems - count($value), $schema->items);
}
// $value is an array, and items are defined - treat as plain array
foreach ($items as $currentItem => $itemDefinition) {
$itemDefinition = $this->factory
->getSchemaStorage()
->resolveRefSchema($itemDefinition);
if (!array_key_exists($currentItem, $value) && property_exists($itemDefinition, 'default') && $this->shouldApplyDefaultValue($requiredOnly, $itemDefinition)) {
if (is_object($itemDefinition->default)) {
$value[$currentItem] = clone $itemDefinition->default;
}
else {
$value[$currentItem] = $itemDefinition->default;
}
}
$path->setFromDefault();
}
}
elseif ($value instanceof self && property_exists($schema, 'default') && $this->shouldApplyDefaultValue($requiredOnly, $schema)) {
// $value is a leaf, not a container - apply the default directly
$value = is_object($schema->default) ? clone $schema->default : $schema->default;
$path->setFromDefault();
}
}