Passed
Pull Request — master (#321)
by
unknown
02:55
created

Callback::__construct()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 23
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6.6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 23
ccs 3
cts 5
cp 0.6
rs 9.6111
cc 5
nc 3
nop 5
crap 6.6
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 Yiisoft\Validator\AttributeEventInterface;
11
use Yiisoft\Validator\BeforeValidationInterface;
12
use Yiisoft\Validator\DataSet\ObjectDataSet;
13
use Yiisoft\Validator\DataSetInterface;
14
use Yiisoft\Validator\Rule\Trait\BeforeValidationTrait;
15
use Yiisoft\Validator\Rule\Trait\RuleNameTrait;
16
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
17
use Yiisoft\Validator\SerializableRuleInterface;
18
use Yiisoft\Validator\SkipOnEmptyInterface;
19
use Yiisoft\Validator\ValidationContext;
20
21
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
22
final class Callback implements
23
    SerializableRuleInterface,
24
    BeforeValidationInterface,
25
    SkipOnEmptyInterface,
26
    AttributeEventInterface
27
{
28
    use BeforeValidationTrait;
29
    use RuleNameTrait;
30
    use SkipOnEmptyTrait;
31
32 5
    public function __construct(
33
        /**
34
         * @var callable|null
35
         */
36
        private $callback = null,
37
        private ?string $method = null,
38
39
        /**
40
         * @var bool|callable|null
41
         */
42
        private $skipOnEmpty = null,
43
        private bool $skipOnError = false,
44
        /**
45
         * @var Closure(mixed, ValidationContext):bool|null
46
         */
47
        private ?Closure $when = null,
48
    ) {
49 5
        if ($this->callback === null && $this->method === null) {
50
            throw new InvalidArgumentException('Either "$callback" or "$method" must be specified.');
51
        }
52
53 5
        if ($this->callback !== null && $this->method !== null) {
54
            throw new InvalidArgumentException('"$callback" and "$method" are mutually exclusive.');
55
        }
56
    }
57
58
    /**
59
     * @return callable|null
60
     */
61 12
    public function getCallback(): ?callable
62
    {
63 12
        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...
64
    }
65
66
    public function getMethod(): ?string
67
    {
68
        return $this->method;
69
    }
70
71 1
    public function afterInitAttribute(DataSetInterface $dataSet): void
72
    {
73 1
        if (!$dataSet instanceof ObjectDataSet) {
74
            return;
75
        }
76
77 1
        $this->callback = Closure::fromCallable([get_debug_type($dataSet->getObject()), $this->method]);
78 1
        $this->method = null;
79
    }
80
81 2
    public function getOptions(): array
82
    {
83
        return [
84 2
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
85 2
            'skipOnError' => $this->skipOnError,
86
        ];
87
    }
88
89 8
    public function getHandlerClassName(): string
90
    {
91 8
        return CallbackHandler::class;
92
    }
93
}
94