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