Test Failed
Pull Request — master (#175)
by
unknown
12:54 queued 12:54
created

GroupRule::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 5
dl 0
loc 8
ccs 3
cts 3
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 Yiisoft\Validator\FormatterInterface;
8
use Yiisoft\Validator\Result;
9
use Yiisoft\Validator\Rule;
10
use Yiisoft\Validator\RuleSet;
11
use Yiisoft\Validator\ValidationContext;
12
13
/**
14
 * Validates a single value for a set of custom rules.
15
 */
16
abstract class GroupRule extends Rule
17
{
18
    public function __construct(
19
        protected string $message = 'This value is not a valid.',
20
        ?FormatterInterface $formatter = null,
21
        bool $skipOnEmpty = false,
22 3
        bool $skipOnError = false,
23
        $when = null
24 3
    ) {
25 3
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
26 3
    }
27
28
    protected function validateValue($value, ?ValidationContext $context = null): Result
29 3
    {
30
        $result = new Result();
31
        if (!$this->getRuleSet()->validate($value, $context)->isValid()) {
32
            $result->addError($this->formatMessage($this->message));
33
        }
34
35
        return $result;
36
    }
37
38
    /**
39 2
     * Return custom rules set
40
     */
41 2
    abstract protected function getRuleSet(): RuleSet;
42
43
    public function getOptions(): array
44
    {
45
        return $this->getRuleSet()->asArray();
46
    }
47
}
48