From 432cdac4bdc303028692c39b685c64dd4c17bdd1 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 11 Mar 2019 09:21:54 +0800 Subject: [PATCH] MDL-64820 core: Exporter read_definition Modified the read_properties_definition to recursively check and set the default array values expected. --- lib/classes/external/exporter.php | 24 ++++++++++++++++++++---- lib/tests/exporter_test.php | 9 +++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/classes/external/exporter.php b/lib/classes/external/exporter.php index 38c195a5964a8..2eee814846dc4 100644 --- a/lib/classes/external/exporter.php +++ b/lib/classes/external/exporter.php @@ -261,16 +261,32 @@ protected function get_other_values(renderer_base $output) { final public static function read_properties_definition() { $properties = static::properties_definition(); $customprops = static::define_other_properties(); - foreach ($customprops as $property => $definition) { + $customprops = static::format_properties($customprops); + $properties += $customprops; + return $properties; + } + + /** + * Recursively formats a given property definition with the default fields required. + * + * @param array $properties List of properties to format + * @return array Formatted array + */ + final public static function format_properties($properties) { + foreach ($properties as $property => $definition) { // Ensures that null is set to its default. if (!isset($definition['null'])) { - $customprops[$property]['null'] = NULL_NOT_ALLOWED; + $properties[$property]['null'] = NULL_NOT_ALLOWED; } if (!isset($definition['description'])) { - $customprops[$property]['description'] = $property; + $properties[$property]['description'] = $property; + } + + // If an array is provided, it may be a nested array that is unformatted so rinse and repeat. + if (is_array($definition['type'])) { + $properties[$property]['type'] = static::format_properties($definition['type']); } } - $properties += $customprops; return $properties; } diff --git a/lib/tests/exporter_test.php b/lib/tests/exporter_test.php index f456628946a08..57929ca6d8a36 100644 --- a/lib/tests/exporter_test.php +++ b/lib/tests/exporter_test.php @@ -179,6 +179,8 @@ public function test_properties_description() { $this->assertEquals('otherstring description', $properties['otherstring']['description']); // Other properties default description. $this->assertEquals('otherstrings', $properties['otherstrings']['description']); + // Assert nested elements are formatted correctly. + $this->assertEquals('id', $properties['nestedarray']['type']['id']['description']); } } @@ -228,6 +230,13 @@ public static function define_other_properties() { 'otherstrings' => array( 'type' => PARAM_TEXT, 'multiple' => true + ), + 'nestedarray' => array( + 'multiple' => true, + 'optional' => true, + 'type' => [ + 'id' => ['type' => PARAM_INT] + ] ) ); }