1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of CaptainHook. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace CaptainHook\App\Hook; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class PHP |
18
|
|
|
* |
19
|
|
|
* @package CaptainHook |
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
21
|
|
|
* @link https://github.com/captainhookphp/captainhook |
22
|
|
|
* @since Class available since Release 5.0.0 |
23
|
|
|
*/ |
24
|
|
|
class Restriction |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* List of applicable hooks |
28
|
|
|
* |
29
|
|
|
* @var array<string, bool> |
30
|
|
|
*/ |
31
|
|
|
private $applicableHooks; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Restriction constructor |
35
|
|
|
* |
36
|
|
|
* @param array<int, string> $hooks |
37
|
|
|
*/ |
38
|
|
|
public function __construct(string ...$hooks) |
39
|
|
|
{ |
40
|
|
|
foreach ($hooks as $hook) { |
41
|
|
|
$this->allowHook($hook); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Add an allowed hook to the restriction |
47
|
|
|
* |
48
|
|
|
* @param string $hook |
49
|
|
|
* @return $this |
50
|
|
|
*/ |
51
|
|
|
public function with(string $hook): self |
52
|
|
|
{ |
53
|
|
|
if ($this->isApplicableFor($hook)) { |
54
|
|
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$restriction = clone ($this); |
58
|
|
|
$restriction->allowHook($hook); |
59
|
|
|
return $restriction; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Check if a given hook is applicable for this restriction |
64
|
|
|
* |
65
|
|
|
* @param string $hook |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function isApplicableFor(string $hook): bool |
69
|
|
|
{ |
70
|
|
|
return $this->applicableHooks[$hook] ?? false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Add hook to allow execution, invalid hooks will be ignored |
75
|
|
|
* |
76
|
|
|
* @param string $hook |
77
|
|
|
*/ |
78
|
|
|
private function allowHook(string $hook): void |
79
|
|
|
{ |
80
|
|
|
if (Util::isValid($hook)) { |
81
|
|
|
$this->applicableHooks[$hook] = true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Create restriction from array |
87
|
|
|
* |
88
|
|
|
* @param array<string> $hooks |
89
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
90
|
|
|
*/ |
91
|
|
|
public static function fromArray(array $hooks): Restriction |
92
|
|
|
{ |
93
|
|
|
return new self(...$hooks); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Create restriction from string |
98
|
|
|
* |
99
|
|
|
* @param string $hook |
100
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
101
|
|
|
*/ |
102
|
|
|
public static function fromString(string $hook): self |
103
|
|
|
{ |
104
|
|
|
return new self($hook); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Create empty restriction |
109
|
|
|
* |
110
|
|
|
* @return \CaptainHook\App\Hook\Restriction |
111
|
|
|
*/ |
112
|
|
|
public static function empty(): self |
113
|
|
|
{ |
114
|
|
|
return new self(...[]); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|