@@ -184,6 +184,39 @@ protected function validateCommonProperties(&$value, $schema = null, JsonPointer
184
184
}
185
185
}
186
186
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
+
187
220
/**
188
221
* Apply default values
189
222
*
@@ -198,38 +231,15 @@ protected function applyDefaultValues(&$value, $schema, $path)
198
231
return ;
199
232
}
200
233
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
-
225
234
// apply defaults if appropriate
235
+ $ requiredOnly = $ this ->factory ->getConfig (self ::CHECK_MODE_ONLY_REQUIRED_DEFAULTS );
226
236
if (isset ($ schema ->properties ) && LooseTypeCheck::isObject ($ value )) {
227
237
// $value is an object or assoc array, and properties are defined - treat as an object
228
238
foreach ($ schema ->properties as $ currentProperty => $ propertyDefinition ) {
229
239
if (
230
240
!LooseTypeCheck::propertyExists ($ value , $ currentProperty )
231
241
&& isset ($ propertyDefinition ->default )
232
- && $ shouldApply ( $ propertyDefinition , $ currentProperty )
242
+ && $ this -> shouldApplyDefaultValue ( $ requiredOnly , $ propertyDefinition , $ currentProperty, $ schema )
233
243
) {
234
244
// assign default value
235
245
if (is_object ($ propertyDefinition ->default )) {
@@ -243,7 +253,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
243
253
} elseif (isset ($ schema ->items ) && LooseTypeCheck::isArray ($ value )) {
244
254
// $value is an array, and items are defined - treat as plain array
245
255
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 )) {
247
260
if (is_object ($ itemDefinition ->default )) {
248
261
$ value [$ currentItem ] = clone $ itemDefinition ->default ;
249
262
} else {
@@ -252,7 +265,10 @@ protected function applyDefaultValues(&$value, $schema, $path)
252
265
}
253
266
$ path ->setFromDefault ();
254
267
}
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 )) {
256
272
// $value is a leaf, not a container - apply the default directly
257
273
$ value = is_object ($ schema ->default ) ? clone $ schema ->default : $ schema ->default ;
258
274
$ path ->setFromDefault ();
0 commit comments