Skip to content

Commit e098ee7

Browse files
committed
Move nested closure into separate method
1 parent e0baac8 commit e098ee7

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

src/JsonSchema/Constraints/UndefinedConstraint.php

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,39 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
186186
}
187187
}
188188

189+
/**
190+
* Check whether a default should be applied for this value
191+
*
192+
* @param mixed $schema
193+
* @param mixed $parentSchema
194+
* @param bool $requiredOnly
195+
*
196+
* @return bool
197+
*/
198+
private function shouldApplyDefaultValue($requiredOnly, $schema, $name = null, $parentSchema = null)
199+
{
200+
// required-only mode is off
201+
if (!$requiredOnly) {
202+
return true;
203+
}
204+
// draft-04 required is set
205+
if (
206+
$name !== null
207+
&& is_object($parentSchema)
208+
&& isset($parentSchema->required)
209+
&& is_array($parentSchema->required)
210+
&& in_array($name, $parentSchema->required)
211+
) {
212+
return true;
213+
}
214+
// draft-03 required is set
215+
if (isset($schema->required) && !is_array($schema->required) && $schema->required) {
216+
return true;
217+
}
218+
// default case
219+
return false;
220+
}
221+
189222
/**
190223
* Apply default values
191224
*
@@ -200,38 +233,15 @@ protected function applyDefaultValues(&$value, $schema, $path)
200233
return;
201234
}
202235

203-
// check whether this default should be applied
204-
$requiredOnly = $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS);
205-
$shouldApply = function ($definition, $name = null) use ($schema, $requiredOnly) {
206-
// required-only mode is off
207-
if (!$requiredOnly) {
208-
return true;
209-
}
210-
// draft-04 required is set
211-
if (
212-
$name !== null
213-
&& isset($schema->required)
214-
&& is_array($schema->required)
215-
&& in_array($name, $schema->required)
216-
) {
217-
return true;
218-
}
219-
// draft-03 required is set
220-
if (isset($definition->required) && !is_array($definition->required) && $definition->required) {
221-
return true;
222-
}
223-
// default case
224-
return false;
225-
};
226-
227236
// apply defaults if appropriate
237+
$requiredOnly = $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS);
228238
if (isset($schema->properties) && LooseTypeCheck::isObject($value)) {
229239
// $value is an object or assoc array, and properties are defined - treat as an object
230240
foreach ($schema->properties as $currentProperty => $propertyDefinition) {
231241
if (
232242
!LooseTypeCheck::propertyExists($value, $currentProperty)
233243
&& isset($propertyDefinition->default)
234-
&& $shouldApply($propertyDefinition, $currentProperty)
244+
&& $this->shouldApplyDefaultValue($requiredOnly, $propertyDefinition, $currentProperty, $schema)
235245
) {
236246
// assign default value
237247
if (is_object($propertyDefinition->default)) {
@@ -245,7 +255,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
245255
} elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
246256
// $value is an array, and items are defined - treat as plain array
247257
foreach ($schema->items as $currentItem => $itemDefinition) {
248-
if (!isset($value[$currentItem]) && isset($itemDefinition->default) && $shouldApply($itemDefinition)) {
258+
if (
259+
!isset($value[$currentItem])
260+
&& isset($itemDefinition->default)
261+
&& $this->shouldApplyDefaultValue($requiredOnly, $itemDefinition)) {
249262
if (is_object($itemDefinition->default)) {
250263
$value[$currentItem] = clone $itemDefinition->default;
251264
} else {
@@ -254,7 +267,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
254267
}
255268
$path->setFromDefault();
256269
}
257-
} elseif (($value instanceof self || $value === null) && isset($schema->default) && $shouldApply($schema)) {
270+
} elseif (
271+
($value instanceof self || $value === null)
272+
&& isset($schema->default)
273+
&& $this->shouldApplyDefaultValue($requiredOnly, $schema)) {
258274
// $value is a leaf, not a container - apply the default directly
259275
$value = is_object($schema->default) ? clone $schema->default : $schema->default;
260276
$path->setFromDefault();

0 commit comments

Comments
 (0)