Passed
Push — master ( 098479...e4440e )
by Tim
01:55
created

NotInArrayTrait::notInArray()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 11
rs 10
cc 3
nc 2
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Assert;
6
7
use InvalidArgumentException;
8
9
use function array_map;
10
use function implode;
11
use function in_array;
12
use function sprintf;
13
14
/**
15
 * @package simplesamlphp/assert
16
 */
17
trait NotInArrayTrait
18
{
19
    /***********************************************************************************
20
     *  NOTE:  Custom assertions may be added below this line.                         *
21
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
22
     *          through __callStatic().                                                *
23
     *         Assertions marked `public` are called directly and will                 *
24
     *          not handle any custom exception passed to it.                          *
25
     ***********************************************************************************/
26
27
28
    /**
29
     * @param mixed $value
30
     * @param array<mixed> $values
31
     * @param string $message
32
     */
33
    protected static function notInArray($value, array $values, string $message = ''): void
34
    {
35
        if (in_array($value, $values, true)) {
36
            $callable = function (mixed $val) {
37
                return self::valueToString($val);
38
            };
39
40
            throw new InvalidArgumentException(sprintf(
41
                $message ?: 'Expected none of: %2$s. Got: %s',
42
                self::valueToString($value),
43
                implode(', ', array_map($callable, $values)),
44
            ));
45
        }
46
    }
47
}
48