Manual:ApiResult.php

From Linux Web Expert

Revision as of 07:01, 8 June 2021 by imported>Shirayuki (use Template:phpi)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

The ApiResult class represents the result of the API operations.

It simply wraps a nested array() structure, adding some functions to simplify array's modifications. As various modules execute, they add different pieces of information to this result, structuring it as it will be given to the client.

Each subarray may either be a dictionary - key-value pairs with unique keys, or lists, where the items are added using $data[] = $value notation.

There are two special key values that change how XML output is generated:

  • '_element' - This key sets the tag name for the rest of the elements in the current array. It is only inserted if the formatter returned true for getNeedsRawData()
  • '*' - This key has special meaning only to the XML formatter, and is output as is for all others. In XML it becomes the content of the current element.

Functions

addValue()

addValue ( $path, $name, $value, $flags = 0 )

Add value to the output data at the given path.

Path can be an indexed array, each element specifying the branch at which to add the new value. Setting $path to array('a','b','c') is equivalent to data['a']['b']['c'] = $value. If $path is null, the value will be inserted at the data root. If $name is empty, the $value is added as a next list element data[] = $value.

Example

class ApiFoo extends ApiBase {
	public function execute() {
		$apiResult = $this->getResult();
		$r['result'] = 'Success';
		$r['bar'] = 'baz';
		$apiResult->addValue( null, $this->getModuleName(), $r );
	}

	public function getResultProperties() {
		return array(
			'foo' => array(
				'result' => array(
					ApiBase::PROP_TYPE => array(
						'success',
						'warning',
						'needtoken'
					)
				),
				'bar' => array(
					ApiBase::PROP_TYPE => 'string',
					ApiBase::PROP_NULLABLE => true
				),
			)
		);
	}
}

See also