-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathArrayKeysValidator.php
47 lines (41 loc) · 1.34 KB
/
ArrayKeysValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
declare(strict_types=1);
/*
* This file is part of the RollerworksSearch package.
*
* (c) Sebastiaan Stok <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Rollerworks\Component\Search\ApiPlatform;
use ApiPlatform\Exception\RuntimeException;
final class ArrayKeysValidator
{
public static function assertKeysExists(array $input, array $required, string $path): void
{
if ([] !== $missing = array_diff_key(array_flip($required), $input)) {
throw new RuntimeException(
\sprintf(
'Config "%s" is missing "%s", got "%s".',
$path,
implode('", "', array_flip($missing)),
implode('", "', array_keys($input))
)
);
}
}
public static function assertOnlyKeys(array $input, array $accepted, string $path): void
{
if (array_diff(array_keys($input), $accepted) !== []) {
throw new RuntimeException(
\sprintf(
'Config "%s" accepts only "%s", got "%s".',
$path,
implode('", "', $accepted),
implode('", "', array_keys($input))
)
);
}
}
}