Passed
Push — master ( 099506...a7575d )
by Evgeniy
02:33
created

FilterDataValidationHelper::assertIsScalar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 4
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Data\Reader;
6
7
use InvalidArgumentException;
8
use Yiisoft\Data\Reader\Iterable\Processor\IterableProcessorInterface;
9
10
use function get_class;
11
use function gettype;
12
use function is_object;
13
use function is_scalar;
14
use function is_string;
15
use function sprintf;
16
17
final class FilterDataValidationHelper
18
{
19
    /**
20
     * @param mixed $field
21
     */
22 182
    public static function assertFieldIsString($field): void
23
    {
24 182
        if (!is_string($field)) {
25 80
            throw new InvalidArgumentException(sprintf(
26 80
                'The field should be string. The %s is received.',
27 80
                self::getValueType($field),
28
            ));
29
        }
30 102
    }
31
32
    /**
33
     * @param mixed $value
34
     */
35 73
    public static function assertIsScalar($value): void
36
    {
37 73
        if (!is_scalar($value)) {
38 32
            throw new InvalidArgumentException(sprintf(
39 32
                'The value should be scalar. The %s is received.',
40 32
                self::getValueType($value),
41
            ));
42
        }
43 45
    }
44
45
    /**
46
     * @param mixed $filterProcessor
47
     */
48 23
    public static function assertFilterProcessorIsIterable($filterProcessor): void
49
    {
50 23
        if (!$filterProcessor instanceof IterableProcessorInterface) {
51 3
            throw new InvalidArgumentException(sprintf(
52 3
                'The filter processor should be an object and implement "%s". The %s is received.',
53 3
                IterableProcessorInterface::class,
54 3
                self::getValueType($filterProcessor),
55
            ));
56
        }
57 20
    }
58
59
    /**
60
     * @param mixed $value
61
     */
62 178
    public static function getValueType($value): string
63
    {
64 178
        return is_object($value) ? get_class($value) : gettype($value);
65
    }
66
}
67