blob: 295a7299c019209bac1ffaf182643e768742022e (
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
<?php
namespace Maps;
/**
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/
final class MappingServices {
/**
* @var MappingService[]
*/
private $nameToServiceMap = [];
/**
* @var string Name of the default service, which is used as fallback
*/
private $defaultService;
/**
* @param string[] $availableServices
* @param string $defaultService
* @param MappingService ...$services
* @throws \InvalidArgumentException
*/
public function __construct( array $availableServices, string $defaultService, MappingService ...$services ) {
$this->defaultService = $defaultService;
foreach ( $services as $service ) {
if ( in_array( $service->getName(), $availableServices ) ) {
$this->nameToServiceMap[$service->getName()] = $service;
foreach ( $service->getAliases() as $alias ) {
$this->nameToServiceMap[$alias] = $service;
}
}
}
if ( !$this->nameIsKnown( $defaultService ) ) {
throw new \InvalidArgumentException( 'The default mapping service needs to be available' );
}
}
/**
* @param string $name Name or alias of a service
* @return bool
*/
public function nameIsKnown( string $name ): bool {
return array_key_exists( $name, $this->nameToServiceMap );
}
/**
* @param string $name Name or alias of a service
* @return MappingService
* @throws \OutOfBoundsException
*/
public function getService( string $name ): MappingService {
if ( !$this->nameIsKnown( $name ) ) {
throw new \OutOfBoundsException();
}
return $this->nameToServiceMap[$name];
}
/**
* @param string $name Name or alias of a service
* @return MappingService
*/
public function getServiceOrDefault( string $name ): MappingService {
if ( $this->nameIsKnown( $name ) ) {
return $this->nameToServiceMap[$name];
}
return $this->nameToServiceMap[$this->defaultService];
}
public function getAllNames(): array {
return array_keys( $this->nameToServiceMap );
}
}
|