Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| ExternalServices | |
100.00% |
8 / 8 |
|
100.00% |
3 / 3 |
8 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| validateMapping | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
6 | |||
| toArray | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\WikibaseManifest; |
| 4 | |
| 5 | use InvalidArgumentException; |
| 6 | |
| 7 | class ExternalServices { |
| 8 | |
| 9 | public const KEY_QUERYSERVICE = 'queryservice'; |
| 10 | public const KEY_QUERYSERVICE_UI = 'queryservice_ui'; |
| 11 | public const KEY_QUICKSTATEMENTS = 'quickstatements'; |
| 12 | public const KEY_OPENREFINE_RECONCILE = 'openrefine_reconcile'; |
| 13 | |
| 14 | private const ALLOWLIST = [ |
| 15 | self::KEY_QUERYSERVICE, |
| 16 | self::KEY_QUERYSERVICE_UI, |
| 17 | self::KEY_QUICKSTATEMENTS, |
| 18 | self::KEY_OPENREFINE_RECONCILE |
| 19 | ]; |
| 20 | |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $mapping; |
| 25 | |
| 26 | /** |
| 27 | * @param string[] $mapping |
| 28 | */ |
| 29 | public function __construct( array $mapping ) { |
| 30 | $this->validateMapping( $mapping ); |
| 31 | $this->mapping = $mapping; |
| 32 | } |
| 33 | |
| 34 | private function validateMapping( array $mapping ): void { |
| 35 | foreach ( $mapping as $k => $v ) { |
| 36 | if ( !is_string( $k ) || !in_array( $k, self::ALLOWLIST ) ) { |
| 37 | throw new InvalidArgumentException( 'Keys of mapping should be allowed strings' ); |
| 38 | } |
| 39 | if ( !is_string( $v ) || !filter_var( $v, FILTER_VALIDATE_URL ) ) { |
| 40 | throw new InvalidArgumentException( 'Values of mapping should be string URLs' ); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | public function toArray(): array { |
| 46 | return $this->mapping; |
| 47 | } |
| 48 | |
| 49 | } |