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

GroupRule   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 31
ccs 7
cts 7
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 8 2
A getOptions() 0 3 1
A __construct() 0 9 1
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
 * GroupRule 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
21
        ?FormatterInterface $formatter = null,
22 3
        bool $skipOnEmpty = false,
23
        bool $skipOnError = false,
24 3
        $when = null
25 3
    ) {
26 3
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
27
    }
28
29 3
    protected function validateValue($value, ?ValidationContext $context = null): Result
30
    {
31
        $result = new Result();
32
        if (!$this->getRuleSet()->validate($value, $context)->isValid()) {
33
            $result->addError($this->formatMessage($this->message));
34
        }
35
36
        return $result;
37
    }
38
39 2
    /**
40
     * Return custom rules set
41 2
     */
42
    abstract protected function getRuleSet(): RuleSet;
43
44
    public function getOptions(): array
45
    {
46
        return $this->getRuleSet()->asArray();
47
    }
48
}
49