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

FilterDataValidationHelper   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 14
c 1
b 0
f 1
dl 0
loc 48
ccs 21
cts 21
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertIsScalar() 0 6 2
A assertFieldIsString() 0 6 2
A getValueType() 0 3 2
A assertFilterProcessorIsIterable() 0 7 2
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