1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Validator\Rule; |
6
|
|
|
|
7
|
|
|
use Attribute; |
8
|
|
|
use Closure; |
9
|
|
|
use JetBrains\PhpStorm\ArrayShape; |
10
|
|
|
use Yiisoft\Validator\Rule\Trait\RuleNameTrait; |
|
|
|
|
11
|
|
|
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait; |
12
|
|
|
use Yiisoft\Validator\ParametrizedRuleInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Validates that the value is a valid json. |
16
|
|
|
*/ |
17
|
|
|
#[Attribute(Attribute::TARGET_PROPERTY)] |
18
|
|
|
final class Json implements ParametrizedRuleInterface |
19
|
|
|
{ |
20
|
|
|
use HandlerClassNameTrait; |
21
|
|
|
use RuleNameTrait; |
22
|
|
|
|
23
|
|
|
public function __construct( |
24
|
|
|
private string $message = 'The value is not JSON.', |
25
|
|
|
private bool $skipOnEmpty = false, |
26
|
|
|
private bool $skipOnError = false, |
27
|
|
|
private ?Closure $when = null, |
28
|
|
|
) { |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
6 |
|
public function getMessage(): string |
35
|
|
|
{ |
36
|
6 |
|
return $this->message; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function isSkipOnEmpty(): bool |
43
|
|
|
{ |
44
|
|
|
return $this->skipOnEmpty; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return bool |
49
|
|
|
*/ |
50
|
|
|
public function isSkipOnError(): bool |
51
|
|
|
{ |
52
|
|
|
return $this->skipOnError; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return Closure|null |
57
|
|
|
*/ |
58
|
|
|
public function getWhen(): ?Closure |
59
|
|
|
{ |
60
|
|
|
return $this->when; |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
#[ArrayShape(['message' => 'string[]', 'skipOnEmpty' => 'bool', 'skipOnError' => 'bool'])] |
64
|
|
|
public function getOptions(): array |
65
|
|
|
{ |
66
|
|
|
return [ |
67
|
|
|
'message' => [ |
68
|
1 |
|
'message' => $this->message, |
69
|
|
|
], |
70
|
1 |
|
'skipOnEmpty' => $this->skipOnEmpty, |
71
|
1 |
|
'skipOnError' => $this->skipOnError, |
72
|
|
|
]; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|