|
15 | 15 | class PolicyFactory |
16 | 16 | { |
17 | 17 | /** |
18 | | - * @param string[][] $defaultDirective |
19 | | - * @param string[][] $customDirective |
| 18 | + * @param string[][] $defaultDirectives |
| 19 | + * @param string[][] $customDirectives |
20 | 20 | * @throws InvalidDirectiveException |
21 | 21 | */ |
22 | | - public function create(Nonce $nonce, array $defaultDirective, array $customDirective): Policy |
| 22 | + public function create(Nonce $nonce, array $defaultDirectives, array $customDirectives): Policy |
23 | 23 | { |
24 | | - $directiveCollections = [$defaultDirective, $customDirective]; |
25 | | - $defaultDirective = array_shift($directiveCollections); |
26 | | - |
27 | | - array_walk($defaultDirective, function (array &$item, string $key) use ($directiveCollections) { |
28 | | - foreach ($directiveCollections as $collection) { |
29 | | - if (array_key_exists($key, $collection)) { |
30 | | - $item = array_unique([...$item, ...$collection[$key]]); |
31 | | - } |
| 24 | + $resultDirectives = $defaultDirectives; |
| 25 | + foreach ($customDirectives as $key => $customDirective) { |
| 26 | + if (array_key_exists($key, $resultDirectives)) { |
| 27 | + $resultDirectives[$key] = array_merge($resultDirectives[$key], $customDirective); |
| 28 | + } else { |
| 29 | + // Custom directive is not present in default, still needs to be added. |
| 30 | + $resultDirectives[$key] = $customDirective; |
32 | 31 | } |
33 | | - }); |
| 32 | + |
| 33 | + $resultDirectives[$key] = array_unique($resultDirectives[$key]); |
| 34 | + } |
34 | 35 |
|
35 | 36 | $policy = new Policy(); |
36 | 37 | $policy->setNonce($nonce); |
37 | 38 |
|
38 | | - foreach ($defaultDirective as $directive => $values) { |
| 39 | + foreach ($resultDirectives as $directive => $values) { |
39 | 40 | $policy->addDirective($directive, $values); |
40 | 41 | } |
41 | 42 |
|
|
0 commit comments