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