1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait; |
11
|
|
|
use Yiisoft\Validator\Rule\Trait\WhenTrait; |
12
|
|
|
use Yiisoft\Validator\RuleWithOptionsInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Validates that the value is a valid json. |
19
|
|
|
* |
20
|
|
|
* @psalm-import-type WhenType from WhenInterface |
21
|
|
|
*/ |
22
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
23
|
|
|
final class Json implements RuleWithOptionsInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
24
|
|
|
{ |
25
|
|
|
use SkipOnEmptyTrait; |
26
|
|
|
use SkipOnErrorTrait; |
27
|
|
|
use WhenTrait; |
28
|
3 |
|
|
29
|
|
|
public function __construct( |
30
|
|
|
private string $incorrectInputMessage = 'The value must have a string type.', |
31
|
|
|
private string $message = 'The value is not JSON.', |
32
|
|
|
|
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var bool|callable|null |
35
|
|
|
*/ |
36
|
|
|
private $skipOnEmpty = null, |
37
|
|
|
private bool $skipOnError = false, |
38
|
|
|
/** |
39
|
|
|
* @var WhenType |
40
|
|
|
*/ |
41
|
|
|
private Closure|null $when = null, |
42
|
|
|
) { |
43
|
|
|
} |
44
|
1 |
|
|
45
|
|
|
public function getName(): string |
46
|
1 |
|
{ |
47
|
|
|
return 'json'; |
48
|
|
|
} |
49
|
6 |
|
|
50
|
|
|
public function getIncorrectInputMessage(): string |
51
|
6 |
|
{ |
52
|
|
|
return $this->incorrectInputMessage; |
53
|
|
|
} |
54
|
5 |
|
|
55
|
|
|
public function getMessage(): string |
56
|
5 |
|
{ |
57
|
|
|
return $this->message; |
58
|
|
|
} |
59
|
1 |
|
|
60
|
|
|
public function getOptions(): array |
61
|
|
|
{ |
62
|
|
|
return [ |
63
|
1 |
|
'incorrectInputMessage' => [ |
64
|
|
|
'template' => $this->incorrectInputMessage, |
65
|
|
|
'parameters' => [], |
66
|
|
|
], |
67
|
1 |
|
'message' => [ |
68
|
|
|
'template' => $this->message, |
69
|
|
|
'parameters' => [], |
70
|
1 |
|
], |
71
|
1 |
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
72
|
|
|
'skipOnError' => $this->skipOnError, |
73
|
|
|
]; |
74
|
|
|
} |
75
|
14 |
|
|
76
|
|
|
public function getHandler(): string |
77
|
14 |
|
{ |
78
|
|
|
return JsonHandler::class; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|