aboutsummaryrefslogtreecommitdiffstats
path: root/includes/search/SearchResultTrait.php
blob: 9a0df2560a4d549e257175d0c0cb2901f8812fdf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php

/**
 * Trait for SearchResult subclasses to share non-obvious behaviors or methods
 * that rarely specialized
 */
trait SearchResultTrait {
	/**
	 * A function returning a set of extension data.
	 * @var Closure|null
	 */
	protected $extensionData;

	/**
	 * Get the extension data as:
	 * augmentor name => data
	 * @return array[]
	 */
	public function getExtensionData() {
		if ( $this->extensionData ) {
			return call_user_func( $this->extensionData );
		} else {
			return [];
		}
	}

	/**
	 * Set extension data for this result.
	 * The data is:
	 * augmentor name => data
	 * @param Closure|array $extensionData Takes no arguments, returns
	 *  either array of extension data or null.
	 */
	public function setExtensionData( $extensionData ) {
		if ( $extensionData instanceof Closure ) {
			$this->extensionData = $extensionData;
		} elseif ( is_array( $extensionData ) ) {
			wfDeprecated( __METHOD__ . ' with array argument', '1.32' );
			$this->extensionData = function () use ( $extensionData ) {
				return $extensionData;
			};
		} else {
			$type = is_object( $extensionData )
				? get_class( $extensionData )
				: gettype( $extensionData );
			throw new \InvalidArgumentException(
				__METHOD__ . " must be called with Closure|array, but received $type" );
		}
	}
}