-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNotInArrayTrait.php
More file actions
47 lines (39 loc) · 1.47 KB
/
NotInArrayTrait.php
File metadata and controls
47 lines (39 loc) · 1.47 KB
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);
namespace SimpleSAML\Assert;
use InvalidArgumentException;
use function array_map;
use function implode;
use function in_array;
use function sprintf;
/**
* @package simplesamlphp/assert
*/
trait NotInArrayTrait
{
/***********************************************************************************
* NOTE: Custom assertions may be added below this line. *
* They SHOULD be marked as `protected` to ensure the call is forced *
* through __callStatic(). *
* Assertions marked `public` are called directly and will *
* not handle any custom exception passed to it. *
***********************************************************************************/
/**
* @param mixed $value
* @param array<mixed> $values
* @param string $message
*/
protected static function notInArray($value, array $values, string $message = ''): void
{
if (in_array($value, $values, true)) {
$callable = function (mixed $val) {
return self::valueToString($val);
};
throw new InvalidArgumentException(sprintf(
$message ?: 'Expected none of: %2$s. Got: %s',
self::valueToString($value),
implode(', ', array_map($callable, $values)),
));
}
}
}