Passed
Push — master ( 315e77...e369d0 )
by Jonathan
16:56
created

AssertOneOfTrait   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 9
dl 0
loc 30
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertOneOf() 0 11 4
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
19
/**
20
 * Trait AssertOneOfTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertOneOfTrait
26
{
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return string
31
     */
32
    abstract protected static function valueToString($value): string;
33
34
    /**
35
     * Check value is in values
36
     *
37
     * @param mixed  $value
38
     * @param array  $values
39
     * @param string $message
40
     *
41
     * @return void
42
     * @throws InvalidArgumentException
43
     */
44 259
    public static function assertOneOf($value, array $values, string $message = ''): void
45
    {
46 259
        if (!in_array($value, $values, true)) {
47 123
            $array = [];
48 123
            foreach ($values as $key => $record) {
49 123
                $array[$key] = self::valueToString($record);
50
            }
51 123
            $valuesString = implode(', ', $array);
52 123
            $format       = $message ?: 'Expected one of %2$s. Got %1$s';
53 123
            $message      = sprintf($format, self::valueToString($value), $valuesString);
54 123
            throw new InvalidArgumentException($message);
55
        }
56 163
    }
57
}
58