summaryrefslogtreecommitdiff
blob: f723b6ceff652e8173775708c762a51411169e45 (plain)
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
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\TranslatorInterface\Insertable;

use InvalidArgumentException;

/**
 * A factory class used to instantiate instances of Insertables
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2020.12
 */
class InsertableFactory {
	/**
	 * Takes a InsertableSuggester class name, and returns an instance of that class.
	 * @param string $class
	 * @param array|string|null $params
	 * @throws InvalidArgumentException
	 */
	public static function make( string $class, $params = null ): InsertablesSuggester {
		// FIXME: We should look at using "id / class" similar to what we do with the ValidatorFactory
		$checkedClasses = [];

		// This check is done for custom insertables that might be added for certain groups.
		if ( !class_exists( $class ) ) {
			$checkedClasses[] = $class;
			// Custom class not found, so lets try to load pre-provided Insertables.
			$class = __NAMESPACE__ . '\\' . $class;
		}

		if ( !class_exists( $class ) ) {
			$checkedClasses[] = $class;
			throw new InvalidArgumentException(
				'Could not find InsertableSuggester with class: ' . implode( ', ', $checkedClasses )
			);
		}

		$suggester = new $class( $params );
		if ( !$suggester instanceof InsertablesSuggester ) {
			throw new InvalidArgumentException(
				"$class does not implement the InsertableSuggester interface"
			);
		}

		return $suggester;
	}
}