|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of CaptainHook. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
namespace SebastianFeldmann\CaptainHook\Config; |
|
11
|
|
|
|
|
12
|
|
|
use SebastianFeldmann\CaptainHook\Hook\Util as HookUtil; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class Util |
|
16
|
|
|
* |
|
17
|
|
|
* @package CaptainHook |
|
18
|
|
|
* @author Sebastian Feldmann <[email protected]> |
|
19
|
|
|
* @link https://github.com/sebastianfeldmann/captainhook |
|
20
|
|
|
* @since Class available since Release 1.0.3 |
|
21
|
|
|
*/ |
|
22
|
|
|
abstract class Util |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Validate a configuration. |
|
26
|
|
|
* |
|
27
|
|
|
* @param array $json |
|
28
|
|
|
* @throws \RuntimeException |
|
29
|
|
|
*/ |
|
30
|
17 |
|
public static function validateJsonConfiguration(array $json) |
|
31
|
|
|
{ |
|
32
|
17 |
|
foreach ($json as $hook => $config) { |
|
33
|
|
|
// check hook name |
|
34
|
16 |
|
if (!HookUtil::isValid($hook)) { |
|
35
|
1 |
|
throw new \RuntimeException('Config error: invalid hook \'' . $hook . '\''); |
|
36
|
|
|
} |
|
37
|
15 |
|
self::validateHookConfig($config); |
|
38
|
|
|
} |
|
39
|
11 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Validate a hook configuration. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $json |
|
45
|
|
|
* @throws \RuntimeException |
|
46
|
|
|
*/ |
|
47
|
15 |
|
public static function validateHookConfig(array $json) |
|
48
|
|
|
{ |
|
49
|
15 |
|
if (!self::keysExist(['enabled', 'actions'], $json)) { |
|
50
|
2 |
|
throw new \RuntimeException('Config error: invalid hook configuration'); |
|
51
|
|
|
} |
|
52
|
13 |
|
if (!is_array($json['actions'])) { |
|
53
|
1 |
|
throw new \RuntimeException('Config error: \'actions\' must be an array'); |
|
54
|
|
|
} |
|
55
|
12 |
|
self::validateActionsConfig($json['actions']); |
|
56
|
10 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Validate a list of action configurations. |
|
60
|
|
|
* |
|
61
|
|
|
* @param array $json |
|
62
|
|
|
* @throws \RuntimeException |
|
63
|
|
|
*/ |
|
64
|
12 |
|
public static function validateActionsConfig(array $json) |
|
65
|
|
|
{ |
|
66
|
12 |
|
foreach ($json as $action) { |
|
67
|
10 |
|
if (!self::keysExist(['action'], $action)) { |
|
68
|
1 |
|
throw new \RuntimeException('Config error: \'action\' missing'); |
|
69
|
|
|
} |
|
70
|
9 |
|
if (empty($action['action'])) { |
|
71
|
9 |
|
throw new \RuntimeException('Config error: \'action\' can\'t be empty'); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
10 |
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Does an array have the expected keys. |
|
78
|
|
|
* |
|
79
|
|
|
* @param array $keys |
|
80
|
|
|
* @param array $subject |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
15 |
|
private static function keysExist(array $keys, array $subject) : bool |
|
84
|
|
|
{ |
|
85
|
15 |
|
foreach ($keys as $key) { |
|
86
|
15 |
|
if (!isset($subject[$key])) { |
|
87
|
15 |
|
return false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
13 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|