- function addMessage($mainSection, $subSection, $value, $multiitem = false, $preserveXmlSpacing = false) {\r
- if (!array_key_exists($mainSection, $this->mData)) {\r
- $this->mData[$mainSection] = array ();\r
- }\r
- if ($subSection !== null) {\r
- if (!array_key_exists($subSection, $this->mData[$mainSection])) {\r
- $this->mData[$mainSection][$subSection] = array ();\r
- }\r
- $element = & $this->mData[$mainSection][$subSection];\r
- } else {\r
- $element = & $this->mData[$mainSection];\r
- }\r
- if ($multiitem) {\r
- $element['_element'] = $multiitem;\r
- $element[] = $value;\r
- } else {\r
- if (is_array($value)) {\r
- $element = array_merge($element, $value);\r
- } else {\r
- if (array_key_exists('*', $element)) {\r
- $element['*'] .= $value;\r
- } else {\r
- $element['*'] = $value;\r
- }\r
- if ($preserveXmlSpacing) {\r
- $element['xml:space'] = 'preserve';\r
+ /**\r
+ * Add an output value to the array by name.\r
+ * Verifies that value with the same name has not been added before.\r
+ */\r
+ public static function addElement(& $arr, $name, $value) {\r
+ if ($arr === null || $name === null || $value === null || !is_array($arr) || is_array($name))\r
+ ApiBase :: dieDebug('Bad parameter for ' . __FUNCTION__);\r
+ if (isset ($arr[$name]))\r
+ ApiBase :: dieDebug("Attempting to add element $name=$value, existing value is {$arr[$name]}");\r
+ $arr[$name] = $value;\r
+ }\r
+\r
+ /**\r
+ * Adds the content element to the array.\r
+ * Use this function instead of hardcoding the '*' element.\r
+ */\r
+ public static function addContent(& $arr, $value) {\r
+ if (is_array($value))\r
+ ApiBase :: dieDebug('Bad parameter for ' . __FUNCTION__);\r
+ ApiResult :: addElement($arr, '*', $value);\r
+ }\r
+\r
+ // public static function makeContentElement($tag, $value) {\r
+ // $result = array();\r
+ // ApiResult::addContent($result, )\r
+ // }\r
+ //\r
+ /**\r
+ * In case the array contains indexed values (in addition to named),\r
+ * all indexed values will have the given tag name.\r
+ */\r
+ public static function setIndexedTagName(& $arr, $tag) {\r
+ // Do not use addElement() as it is ok to call this more than once\r
+ if ($arr === null || $tag === null || !is_array($arr) || is_array($tag))\r
+ ApiBase :: dieDebug('Bad parameter for ' . __FUNCTION__);\r
+ $arr['_element'] = $tag;\r
+ }\r
+\r
+ /**\r
+ * Add value to the output data at the given path.\r
+ * Path is an indexed array, each element specifing the branch at which to add the new value\r
+ * Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value \r
+ */\r
+ public function addValue($path, $name, $value) {\r
+\r
+ $data = & $this->getData();\r
+\r
+ if (isset ($path)) {\r
+ if (is_array($path)) {\r
+ foreach ($path as $p) {\r
+ if (!isset ($data[$p]))\r
+ $data[$p] = array ();\r
+ $data = & $data[$p];\r