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

Json   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 13
dl 0
loc 46
ccs 11
cts 11
cp 1
rs 10
c 1
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getName() 0 3 1
A getHandlerClassName() 0 3 1
A getMessage() 0 3 1
A getOptions() 0 8 1
A __construct() 0 13 1
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