1 | <?php |
||
16 | class IndexManager |
||
17 | { |
||
18 | /** |
||
19 | * @var Index |
||
20 | */ |
||
21 | private $defaultIndex; |
||
22 | |||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | private $indexes; |
||
27 | |||
28 | 11 | public function __construct(array $indexes, Index $defaultIndex) |
|
29 | { |
||
30 | 11 | $this->defaultIndex = $defaultIndex; |
|
31 | 11 | $this->indexes = $indexes; |
|
32 | 11 | } |
|
33 | |||
34 | /** |
||
35 | * Gets all registered indexes. |
||
36 | */ |
||
37 | 1 | public function getAllIndexes(): array |
|
38 | { |
||
39 | 1 | return $this->indexes; |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Gets an index by its name or the default index. |
||
44 | * |
||
45 | * @throws \InvalidArgumentException if no index exists for the given name |
||
46 | */ |
||
47 | 6 | public function getIndex(?string $name = null): Index |
|
48 | { |
||
49 | 6 | if (null === $name) { |
|
50 | 1 | return $this->defaultIndex; |
|
51 | } |
||
52 | |||
53 | 5 | if (!isset($this->indexes[$name])) { |
|
54 | 1 | throw new \InvalidArgumentException(\sprintf('The index "%s" does not exist', $name)); |
|
55 | } |
||
56 | |||
57 | 4 | return $this->indexes[$name]; |
|
58 | } |
||
59 | |||
60 | /** |
||
61 | * Gets the default index. |
||
62 | */ |
||
63 | 1 | public function getDefaultIndex(): Index |
|
67 | } |
||
68 |