1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* The MIT License (MIT) |
7
|
|
|
* |
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
9
|
|
|
* |
10
|
|
|
* This software may be modified and distributed under the terms |
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Jose\Component\Checker; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class HeaderCheckerManagerFactory. |
18
|
|
|
*/ |
19
|
|
|
final class HeaderCheckerManagerFactory |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var HeaderCheckerInterface[] |
23
|
|
|
*/ |
24
|
|
|
private $checkers = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string[] $aliases |
28
|
|
|
* |
29
|
|
|
* @return HeaderCheckerManager |
30
|
|
|
*/ |
31
|
|
|
public function create(array $aliases): HeaderCheckerManager |
32
|
|
|
{ |
33
|
|
|
$checkers = []; |
34
|
|
|
foreach ($aliases as $alias) { |
35
|
|
|
if (array_key_exists($alias, $this->checkers)) { |
36
|
|
|
$checkers[] = $this->checkers[$alias]; |
37
|
|
|
} else { |
38
|
|
|
throw new \InvalidArgumentException(sprintf('The header checker with the alias "%s" is not supported.', $alias)); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return HeaderCheckerManager::create($checkers); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param string $alias |
47
|
|
|
* @param HeaderCheckerInterface $checker |
48
|
|
|
* |
49
|
|
|
* @return HeaderCheckerManagerFactory |
50
|
|
|
*/ |
51
|
|
|
public function add(string $alias, HeaderCheckerInterface $checker): HeaderCheckerManagerFactory |
52
|
|
|
{ |
53
|
|
|
if (array_key_exists($alias, $this->checkers)) { |
54
|
|
|
throw new \InvalidArgumentException(sprintf('The alias "%s" already exists.', $alias)); |
55
|
|
|
} |
56
|
|
|
$this->checkers[$alias] = $checker; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string[] |
63
|
|
|
*/ |
64
|
|
|
public function aliases(): array |
65
|
|
|
{ |
66
|
|
|
return array_keys($this->checkers); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return HeaderCheckerInterface[] |
71
|
|
|
*/ |
72
|
|
|
public function checkers(): array |
73
|
|
|
{ |
74
|
|
|
return $this->checkers; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|