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