MDL-76723 core_external: Handle format properties properly

- If a text format property defines the 'optional' attribute as true,
then the $required parameter that
is passed to \core\external\exporter::get_format_structure() should be
VALUE_OPTIONAL.
- If a default value is provided for a format property,
\core\external\exporter::get_format_structure() should pass the default
value to the external_format_value constructor.
- Added validation for external_format_value for the default parameter.
  Debugging will be shown in case an invalid default value is passed.
- Amended unit tests for better coverage.
This commit is contained in:
Jun Pataleta
2023-12-19 15:50:21 +08:00
parent def6645cdc
commit bbfbc8abd1
3 changed files with 105 additions and 17 deletions
+9 -2
View File
@@ -425,10 +425,12 @@ abstract class exporter {
* @return external_format_value
*/
final protected static function get_format_structure($property, $definition, $required = VALUE_REQUIRED) {
$default = null;
if (array_key_exists('default', $definition)) {
$required = VALUE_DEFAULT;
$default = $definition['default'];
}
return new external_format_value($property, $required);
return new external_format_value($property, $required, $default);
}
/**
@@ -547,7 +549,12 @@ abstract class exporter {
if (isset($returns[$formatproperty])) {
throw new coding_exception('The format for \'' . $property . '\' is already defined.');
}
$returns[$formatproperty] = self::get_format_structure($property, $properties[$formatproperty]);
$formatpropertydef = $properties[$formatproperty];
$formatpropertyrequired = VALUE_REQUIRED;
if (!empty($formatpropertydef['optional'])) {
$formatpropertyrequired = VALUE_OPTIONAL;
}
$returns[$formatproperty] = self::get_format_structure($property, $formatpropertydef, $formatpropertyrequired);
}
}
}