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

Callback::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 InvalidArgumentException;
10
use TypeError;
11
use Yiisoft\Validator\AttributeEventInterface;
12
use Yiisoft\Validator\DataSet\ObjectDataSet;
13
use Yiisoft\Validator\DataSetInterface;
14
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
15
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
16
use Yiisoft\Validator\Rule\Trait\WhenTrait;
17
use Yiisoft\Validator\SerializableRuleInterface;
18
use Yiisoft\Validator\SkipOnEmptyInterface;
19
use Yiisoft\Validator\SkipOnErrorInterface;
20
use Yiisoft\Validator\ValidationContext;
21
use Yiisoft\Validator\WhenInterface;
22
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Callback implements
25
    SerializableRuleInterface,
26
    SkipOnErrorInterface,
27
    WhenInterface,
28
    SkipOnEmptyInterface,
29
    AttributeEventInterface
30
{
31
    use SkipOnEmptyTrait;
32
    use SkipOnErrorTrait;
33
    use WhenTrait;
34
35 10
    public function __construct(
36
        /**
37
         * @var callable|null
38
         */
39
        private $callback = null,
40
        private ?string $method = null,
41
42
        /**
43
         * @var bool|callable|null
44
         */
45
        private $skipOnEmpty = null,
46
        private bool $skipOnError = false,
47
        /**
48
         * @var Closure(mixed, ValidationContext):bool|null
49
         */
50
        private ?Closure $when = null,
51
    ) {
52 10
        if ($this->callback === null && $this->method === null) {
53
            throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
54
        }
55
56 10
        if ($this->callback !== null && $this->method !== null) {
57
            throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
58
        }
59
    }
60
61 1
    public function getName(): string
62
    {
63 1
        return 'callback';
64
    }
65
66 13
    public function getCallback(): ?callable
67
    {
68 13
        return $this->callback;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->callback returns the type mixed which is incompatible with the type-hinted return callable|null.
Loading history...
69
    }
70
71
    public function getMethod(): ?string
72
    {
73
        return $this->method;
74
    }
75
76 4
    public function afterInitAttribute(DataSetInterface $dataSet): void
77
    {
78 4
        if (!$dataSet instanceof ObjectDataSet) {
79
            return;
80
        }
81
82
        try {
83 4
            $this->callback = Closure::fromCallable([$dataSet->getObject()::class, $this->method]);
84 3
        } catch (TypeError) {
85 3
            throw new InvalidArgumentException('Method must exist and have public and static modifers.');
86
        }
87
    }
88
89 2
    public function getOptions(): array
90
    {
91
        return [
92 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
93 2
            'skipOnError' => $this->skipOnError,
94
        ];
95
    }
96
97 8
    public function getHandlerClassName(): string
98
    {
99 8
        return CallbackHandler::class;
100
    }
101
}
102