Passed
Pull Request — master (#222)
by Alexander
04:26 queued 02:14
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\PreValidatableRuleInterface;
11
use Yiisoft\Validator\Rule\Trait\PreValidatableTrait;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_TRAIT, expecting T_STRING or '{' on line 11 at column 27
Loading history...
12
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
13
use Yiisoft\Validator\Rule\Trait\HandlerClassNameTrait;
14
use Yiisoft\Validator\ParametrizedRuleInterface;
15
16
/**
17
 * Validates that the value is a valid json.
18
 */
19
#[Attribute(Attribute::TARGET_PROPERTY)]
20
final class Json implements ParametrizedRuleInterface, PreValidatableRuleInterface
21
{
22
    use HandlerClassNameTrait;
23
    use PreValidatableTrait;
24
    use RuleNameTrait;
25
26
    public function __construct(
27
        private string $message = 'The value is not JSON.',
28
        private bool $skipOnEmpty = false,
29
        private bool $skipOnError = false,
30
        private ?Closure $when = null,
31
    ) {
32
    }
33
34
    /**
35
     * @return string
36
     */
37 6
    public function getMessage(): string
38
    {
39 6
        return $this->message;
40
    }
41
42 1
    #[ArrayShape(['message' => 'string[]', 'skipOnEmpty' => 'bool', 'skipOnError' => 'bool'])]
43
    public function getOptions(): array
44
    {
45
        return [
46
            'message' => [
47 1
                'message' => $this->message,
48
            ],
49 1
            'skipOnEmpty' => $this->skipOnEmpty,
50 1
            'skipOnError' => $this->skipOnError,
51
        ];
52
    }
53
}
54