1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ElePHPant\Cookie\ValueObjects; |
4
|
|
|
|
5
|
|
|
use ElePHPant\Cookie\Strategies\Encryption\EncryptionStrategyInterface; |
6
|
|
|
use ElePHPant\Cookie\Exceptions\{ |
7
|
|
|
EmptyValuesException, |
8
|
|
|
InvalidConfigException, |
9
|
|
|
InvalidValueException, |
10
|
|
|
MissingDriversException |
11
|
|
|
}; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class Config |
15
|
|
|
* |
16
|
|
|
* Please report bugs on https://github.com/wilderamorim/cookie/issues |
17
|
|
|
* |
18
|
|
|
* @author Wilder Amorim <https://github.com/wilderamorim> |
19
|
|
|
* @link https://www.linkedin.com/in/wilderamorim/ |
20
|
|
|
*/ |
21
|
|
|
final class Config |
22
|
|
|
{ |
23
|
|
|
/** @var array */ |
24
|
|
|
private array $configs = []; |
25
|
|
|
|
26
|
|
|
/** @var array */ |
27
|
|
|
private array $requiredDrivers = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param array $configs The configurations to be validated. |
31
|
|
|
* @throws MissingDriversException If any required driver is missing. |
32
|
|
|
* @throws EmptyValuesException If any required driver has an empty value. |
33
|
|
|
* @throws InvalidConfigException If there are invalid configuration keys. |
34
|
|
|
* @throws InvalidValueException If there are invalid configuration values. |
35
|
|
|
*/ |
36
|
|
|
public function __construct(array $configs) |
37
|
|
|
{ |
38
|
|
|
$this->validate($configs); |
39
|
|
|
$this->configs = $configs; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function toArray(): array |
46
|
|
|
{ |
47
|
|
|
return $this->configs; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Validate the configurations by performing various validation checks. |
52
|
|
|
* |
53
|
|
|
* @param array $configs The configurations to be validated. |
54
|
|
|
* @throws MissingDriversException If any required driver is missing. |
55
|
|
|
* @throws EmptyValuesException If any required driver has an empty value. |
56
|
|
|
* @throws InvalidConfigException If there are invalid configuration keys. |
57
|
|
|
* @throws InvalidValueException If there are invalid configuration values. |
58
|
|
|
* @return void |
59
|
|
|
*/ |
60
|
|
|
private function validate(array $configs): void |
61
|
|
|
{ |
62
|
|
|
$this->validateRequiredDrivers($configs); |
63
|
|
|
$this->validateEmptyValues($configs); |
64
|
|
|
$this->validateConfigs($configs); |
65
|
|
|
$this->validateValues($configs); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Validate the presence of required drivers in the configurations. |
70
|
|
|
* |
71
|
|
|
* @param array $configs The configurations to be validated. |
72
|
|
|
* @throws MissingDriversException If any required driver is missing. |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
private function validateRequiredDrivers(array $configs): void |
76
|
|
|
{ |
77
|
|
|
$missingDrivers = array_diff($this->requiredDrivers, array_keys($configs)); |
78
|
|
|
if (!empty($missingDrivers)) { |
79
|
|
|
throw new MissingDriversException($missingDrivers); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Validate the absence of empty values for required drivers in the configurations. |
85
|
|
|
* |
86
|
|
|
* @param array $configs The configurations to be validated. |
87
|
|
|
* @throws EmptyValuesException If any required driver has an empty value. |
88
|
|
|
* @return void |
89
|
|
|
*/ |
90
|
|
|
private function validateEmptyValues(array $configs): void |
91
|
|
|
{ |
92
|
|
|
$emptyValues = array_filter( |
93
|
|
|
$configs, fn($value, $key) => empty($value) && in_array($key, $this->requiredDrivers), ARRAY_FILTER_USE_BOTH |
94
|
|
|
); |
95
|
|
|
if (!empty($emptyValues)) { |
96
|
|
|
throw new EmptyValuesException(array_keys($emptyValues)); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Validate the configurations by checking if the keys are valid and expected. |
102
|
|
|
* |
103
|
|
|
* @param array $configs The configurations to be validated. |
104
|
|
|
* @throws InvalidConfigException If there are invalid configuration keys. |
105
|
|
|
* @return void |
106
|
|
|
*/ |
107
|
|
|
private function validateConfigs(array $configs): void |
108
|
|
|
{ |
109
|
|
|
$invalidConfigs = array_diff_key($configs, $this->getValidations($configs)); |
110
|
|
|
if (!empty($invalidConfigs)) { |
111
|
|
|
throw new InvalidConfigException(array_keys($invalidConfigs)); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Validate the values of the configurations based on the defined validations. |
117
|
|
|
* |
118
|
|
|
* @param array $configs The configurations to be validated. |
119
|
|
|
* @throws InvalidValueException If there are invalid configuration values. |
120
|
|
|
* @return void |
121
|
|
|
*/ |
122
|
|
|
private function validateValues(array $configs): void |
123
|
|
|
{ |
124
|
|
|
$validations = $this->getValidations($configs); |
125
|
|
|
$invalidValues = array_filter($configs, fn($value, $key) => !$validations[$key]($value), ARRAY_FILTER_USE_BOTH); |
126
|
|
|
if (!empty($invalidValues)) { |
127
|
|
|
throw new InvalidValueException(array_keys($invalidValues)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get the array of validations for the configurations. |
133
|
|
|
* |
134
|
|
|
* @param array $configs The configurations for which to retrieve validations. |
135
|
|
|
* @return \Closure[] An array of validations for the configurations. |
136
|
|
|
*/ |
137
|
|
|
private function getValidations(array $configs): array |
138
|
|
|
{ |
139
|
|
|
return [ |
140
|
|
|
'expiration' => fn($value) => new Expiration($value), |
141
|
|
|
'encryption' => fn($value) => (new $value($configs) instanceof EncryptionStrategyInterface), |
142
|
|
|
'encrypt_key' => fn($value) => is_string($value), |
143
|
|
|
]; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|