Completed
Pull Request — master (#175)
by Alexander
02:53 queued 02:53
created

GroupRule::validateValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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 5
    public function __construct(
19
        protected string $message = 'This value is not a valid.',
20
        ?FormatterInterface $formatter = null,
21
        bool $skipOnEmpty = false,
22
        bool $skipOnError = false,
23
        $when = null
24
    ) {
25 5
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
26
    }
27
28 3
    protected function validateValue($value, ?ValidationContext $context = null): Result
29
    {
30 3
        $result = new Result();
31 3
        if (!$this->getRuleSet()->validate($value, $context)->isValid()) {
32 3
            $result->addError($this->formatMessage($this->message));
33
        }
34
35 3
        return $result;
36
    }
37
38
    /**
39
     * Return custom rules set
40
     */
41
    abstract protected function getRuleSet(): RuleSet;
42
43 2
    public function getOptions(): array
44
    {
45 2
        return $this->getRuleSet()->asArray();
46
    }
47
}
48