Passed
Pull Request — master (#222)
by Alexander
02:58
created

Json   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
dl 0
loc 50
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 6
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;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 10 at column 27
Loading history...
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