|
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\Processor\IterableProcessorInterface; |
|
10
|
|
|
|
|
11
|
|
|
use function get_class; |
|
12
|
|
|
use function gettype; |
|
13
|
|
|
use function is_object; |
|
14
|
|
|
use function is_scalar; |
|
15
|
|
|
use function is_string; |
|
16
|
|
|
use function sprintf; |
|
17
|
|
|
|
|
18
|
|
|
final class FilterDataValidationHelper |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @param mixed $field |
|
22
|
|
|
*/ |
|
23
|
204 |
|
public static function assertFieldIsString($field): void |
|
24
|
|
|
{ |
|
25
|
204 |
|
if (!is_string($field)) { |
|
26
|
80 |
|
throw new InvalidArgumentException(sprintf( |
|
27
|
|
|
'The field should be string. The %s is received.', |
|
28
|
80 |
|
self::getValueType($field), |
|
29
|
|
|
)); |
|
30
|
|
|
} |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param mixed $filterProcessor |
|
35
|
|
|
*/ |
|
36
|
23 |
|
public static function assertFilterProcessorIsIterable($filterProcessor): void |
|
37
|
|
|
{ |
|
38
|
23 |
|
if (!$filterProcessor instanceof IterableProcessorInterface) { |
|
39
|
3 |
|
throw new InvalidArgumentException(sprintf( |
|
40
|
|
|
'The filter processor should be an object and implement "%s". The %s is received.', |
|
41
|
|
|
IterableProcessorInterface::class, |
|
42
|
3 |
|
self::getValueType($filterProcessor), |
|
43
|
|
|
)); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param mixed $value |
|
49
|
|
|
*/ |
|
50
|
8 |
|
public static function assertIsScalar($value): void |
|
51
|
|
|
{ |
|
52
|
8 |
|
if (!is_scalar($value)) { |
|
53
|
4 |
|
throw new InvalidArgumentException(sprintf( |
|
54
|
|
|
'The value should be scalar. The %s is received.', |
|
55
|
4 |
|
self::getValueType($value), |
|
56
|
|
|
)); |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param mixed $value |
|
62
|
|
|
*/ |
|
63
|
102 |
|
public static function assertIsScalarOrInstanceOfDataTimeInterface($value): void |
|
64
|
|
|
{ |
|
65
|
102 |
|
if (!$value instanceof DateTimeInterface && !is_scalar($value)) { |
|
66
|
28 |
|
throw new InvalidArgumentException(sprintf( |
|
67
|
|
|
'The value should be scalar or %s instance. The %s is received.', |
|
68
|
|
|
DateTimeInterface::class, |
|
69
|
28 |
|
self::getValueType($value), |
|
70
|
|
|
)); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
*/ |
|
77
|
178 |
|
public static function getValueType($value): string |
|
78
|
|
|
{ |
|
79
|
178 |
|
return is_object($value) ? get_class($value) : gettype($value); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|