Skip to content

Commit 1e1f7ff

Browse files
committed
Move nested closure into separate method
1 parent c1818da commit 1e1f7ff

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
@@ -184,6 +184,39 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
184184
}
185185
}
186186

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

201-
// check whether this default should be applied
202-
$requiredOnly = $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS);
203-
$shouldApply = function ($definition, $name = null) use ($schema, $requiredOnly) {
204-
// required-only mode is off
205-
if (!$requiredOnly) {
206-
return true;
207-
}
208-
// draft-04 required is set
209-
if (
210-
$name !== null
211-
&& isset($schema->required)
212-
&& is_array($schema->required)
213-
&& in_array($name, $schema->required)
214-
) {
215-
return true;
216-
}
217-
// draft-03 required is set
218-
if (isset($definition->required) && !is_array($definition->required) && $definition->required) {
219-
return true;
220-
}
221-
// default case
222-
return false;
223-
};
224-
225234
// apply defaults if appropriate
235+
$requiredOnly = $this->factory->getConfig(self::CHECK_MODE_ONLY_REQUIRED_DEFAULTS);
226236
if (isset($schema->properties) && LooseTypeCheck::isObject($value)) {
227237
// $value is an object or assoc array, and properties are defined - treat as an object
228238
foreach ($schema->properties as $currentProperty => $propertyDefinition) {
229239
if (
230240
!LooseTypeCheck::propertyExists($value, $currentProperty)
231241
&& isset($propertyDefinition->default)
232-
&& $shouldApply($propertyDefinition, $currentProperty)
242+
&& $this->shouldApplyDefaultValue($requiredOnly, $propertyDefinition, $currentProperty, $schema)
233243
) {
234244
// assign default value
235245
if (is_object($propertyDefinition->default)) {
@@ -243,7 +253,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
243253
} elseif (isset($schema->items) && LooseTypeCheck::isArray($value)) {
244254
// $value is an array, and items are defined - treat as plain array
245255
foreach ($schema->items as $currentItem => $itemDefinition) {
246-
if (!isset($value[$currentItem]) && isset($itemDefinition->default) && $shouldApply($itemDefinition)) {
256+
if (
257+
!isset($value[$currentItem])
258+
&& isset($itemDefinition->default)
259+
&& $this->shouldApplyDefaultValue($requiredOnly, $itemDefinition)) {
247260
if (is_object($itemDefinition->default)) {
248261
$value[$currentItem] = clone $itemDefinition->default;
249262
} else {
@@ -252,7 +265,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
252265
}
253266
$path->setFromDefault();
254267
}
255-
} elseif (($value instanceof self || $value === null) && isset($schema->default) && $shouldApply($schema)) {
268+
} elseif (
269+
($value instanceof self || $value === null)
270+
&& isset($schema->default)
271+
&& $this->shouldApplyDefaultValue($requiredOnly, $schema)) {
256272
// $value is a leaf, not a container - apply the default directly
257273
$value = is_object($schema->default) ? clone $schema->default : $schema->default;
258274
$path->setFromDefault();

0 commit comments

Comments
 (0)