Passed
Pull Request — master (#333)
by Sergei
02:38
created

Json::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 2
    public function __construct(
29
        private string $message = 'The value is not JSON.',
30
31
        /**
32
         * @var bool|callable|null
33
         */
34
        private $skipOnEmpty = null,
35
        private bool $skipOnError = false,
36
        /**
37
         * @var Closure(mixed, ValidationContext):bool|null
38
         */
39
        private ?Closure $when = null,
40
    ) {
41
    }
42
43 1
    public function getName(): string
44
    {
45 1
        return 'json';
46
    }
47
48 6
    public function getMessage(): string
49
    {
50 6
        return $this->message;
51
    }
52
53 1
    public function getOptions(): array
54
    {
55
        return [
56
            'message' => [
57 1
                'message' => $this->message,
58
            ],
59 1
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
60 1
            'skipOnError' => $this->skipOnError,
61
        ];
62
    }
63
64 1
    public function getHandlerClassName(): string
65
    {
66 1
        return JsonHandler::class;
67
    }
68
}
69