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\DumpedRuleInterface; |
13
|
|
|
use Yiisoft\Validator\SkipOnEmptyInterface; |
14
|
|
|
use Yiisoft\Validator\SkipOnErrorInterface; |
15
|
|
|
use Yiisoft\Validator\WhenInterface; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Contains a set of options to determine if the value is a valid JSON string. |
19
|
|
|
* |
20
|
|
|
* @see https://en.wikipedia.org/wiki/JSON |
21
|
|
|
* @see https://tools.ietf.org/html/rfc8259 |
22
|
|
|
* @see JsonHandler Corresponding handler performing the actual validation. |
23
|
|
|
* |
24
|
|
|
* @psalm-import-type WhenType from WhenInterface |
25
|
|
|
*/ |
26
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)] |
27
|
|
|
final class Json implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface |
28
|
3 |
|
{ |
29
|
|
|
use SkipOnEmptyTrait; |
30
|
|
|
use SkipOnErrorTrait; |
31
|
|
|
use WhenTrait; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $incorrectInputMessage Error message used when validation fails because the type of validated value |
35
|
|
|
* is not a string. |
36
|
|
|
* |
37
|
|
|
* You may use the following placeholders in the message: |
38
|
|
|
* |
39
|
|
|
* - `{attribute}`: the translated label of the attribute being validated. |
40
|
|
|
* - `{type}`: the type of the value being validated. |
41
|
|
|
* @param string $message Error message used when validation fails because the validated value either not JSON at |
42
|
|
|
* all or invalid JSON with errors. |
43
|
|
|
* |
44
|
1 |
|
* You may use the following placeholders in the message: |
45
|
|
|
* |
46
|
1 |
|
* - `{attribute}`: the translated label of the attribute being validated. |
47
|
|
|
* - `{value}`: the value being validated. |
48
|
|
|
* @param bool|callable|null $skipOnEmpty Whether to skip this rule if the validated value is empty / not passed. |
49
|
6 |
|
* See {@see SkipOnEmptyInterface}. |
50
|
|
|
* @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See |
51
|
6 |
|
* {@see SkipOnErrorInterface}. |
52
|
|
|
* @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}. |
53
|
|
|
* |
54
|
5 |
|
* @psalm-param WhenType $when |
55
|
|
|
*/ |
56
|
5 |
|
public function __construct( |
57
|
|
|
private string $incorrectInputMessage = 'The value must be a string.', |
58
|
|
|
private string $message = 'The value is not JSON.', |
59
|
1 |
|
private mixed $skipOnEmpty = null, |
60
|
|
|
private bool $skipOnError = false, |
61
|
|
|
private Closure|null $when = null, |
62
|
|
|
) { |
63
|
1 |
|
} |
64
|
|
|
|
65
|
|
|
public function getName(): string |
66
|
|
|
{ |
67
|
1 |
|
return self::class; |
68
|
|
|
} |
69
|
|
|
|
70
|
1 |
|
/** |
71
|
1 |
|
* Gets error message used when validation fails because the type of validated value is not a string. |
72
|
|
|
* |
73
|
|
|
* @return string Error message / template. |
74
|
|
|
* |
75
|
14 |
|
* @see $incorrectInputMessage |
76
|
|
|
*/ |
77
|
14 |
|
public function getIncorrectInputMessage(): string |
78
|
|
|
{ |
79
|
|
|
return $this->incorrectInputMessage; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Gets error message used when validation fails because the validated value either not JSON at all or invalid JSON |
84
|
|
|
* with errors. |
85
|
|
|
* |
86
|
|
|
* @return string Error message / template. |
87
|
|
|
* |
88
|
|
|
* @see $message |
89
|
|
|
*/ |
90
|
|
|
public function getMessage(): string |
91
|
|
|
{ |
92
|
|
|
return $this->message; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getOptions(): array |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'incorrectInputMessage' => [ |
99
|
|
|
'template' => $this->incorrectInputMessage, |
100
|
|
|
'parameters' => [], |
101
|
|
|
], |
102
|
|
|
'message' => [ |
103
|
|
|
'template' => $this->message, |
104
|
|
|
'parameters' => [], |
105
|
|
|
], |
106
|
|
|
'skipOnEmpty' => $this->getSkipOnEmptyOption(), |
107
|
|
|
'skipOnError' => $this->skipOnError, |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function getHandler(): string |
112
|
|
|
{ |
113
|
|
|
return JsonHandler::class; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|