Passed
Push — master ( 1f27ac...52075a )
by Alexander
04:53 queued 02:24
created

isScalarOrInstanceOfDateTimeInterface()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader;
6
7
use DateTimeInterface;
8
use InvalidArgumentException;
9
use Yiisoft\Data\Reader\Iterable\IterableFilterHandlerInterface;
10
11
use function is_scalar;
12
use function is_string;
13
use function sprintf;
14
15
/**
16
 * Filter-related assertions.
17
 */
18
final class FilterAssert
19
{
20
    /**
21
     * Asserts that field is a string.
22
     *
23
     * @param mixed $field Value to check.
24
     *
25
     * @throws InvalidArgumentException If value is not correct.
26
     */
27 189
    public static function fieldIsString(mixed $field): void
28
    {
29 189
        if (!is_string($field)) {
30 80
            throw new InvalidArgumentException(sprintf(
31 80
                'The field should be string. The %s is received.',
32 80
                get_debug_type($field),
33 80
            ));
34
        }
35
    }
36
37
    /**
38
     * Asserts that the value is an instance of {@see IterableFilterHandlerInterface}.
39
     *
40
     * @param mixed $value Value to check.
41
     *
42
     * @throws InvalidArgumentException If value is not correct.
43
     */
44 23
    public static function isIterableFilterHandlerInterface(mixed $value): void
45
    {
46 23
        if (!$value instanceof IterableFilterHandlerInterface) {
47 3
            throw new InvalidArgumentException(sprintf(
48 3
                'The filter handler should be an object and implement "%s". The %s is received.',
49 3
                IterableFilterHandlerInterface::class,
50 3
                get_debug_type($value),
51 3
            ));
52
        }
53
    }
54
55
    /**
56
     * Asset that value is scalar.
57
     *
58
     * @param mixed $value Value to check.
59
     *
60
     * @throws InvalidArgumentException If value is not correct.
61
     */
62 9
    public static function isScalar(mixed $value): void
63
    {
64 9
        if (!is_scalar($value)) {
65 4
            throw new InvalidArgumentException(sprintf(
66 4
                'The value should be scalar. The %s is received.',
67 4
                get_debug_type($value),
68 4
            ));
69
        }
70
    }
71
72
    /**
73
     * Asserts that value is either a scalar or an instance of {@see DateTimeInterface}.
74
     *
75
     * @param mixed $value Value to check.
76
     *
77
     * @throws InvalidArgumentException If value is not correct.
78
     */
79 102
    public static function isScalarOrInstanceOfDateTimeInterface(mixed $value): void
80
    {
81 102
        if (!$value instanceof DateTimeInterface && !is_scalar($value)) {
82 28
            throw new InvalidArgumentException(sprintf(
83 28
                'The value should be scalar or %s instance. The %s is received.',
84 28
                DateTimeInterface::class,
85 28
                get_debug_type($value),
86 28
            ));
87
        }
88
    }
89
}
90