Test Failed
Pull Request — master (#219)
by
unknown
02:42
created

GroupRule   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 10
c 2
b 0
f 0
dl 0
loc 41
ccs 9
cts 9
cp 1
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A message() 0 6 1
A validateValue() 0 8 2
A getOptions() 0 3 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
 * Validates a single value for a set of custom rules.
15
 */
16
abstract class GroupRule extends Rule
17
{
18 7
    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 7
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
26
    }
27
28 5
    /**
29
     * @see $message
30 5
     */
31 5
    public function message(string $value): self
32 4
    {
33
        $new = clone $this;
34
        $new->message = $value;
35 5
36
        return $new;
37
    }
38
39
    protected function validateValue($value, ?ValidationContext $context = null): Result
40
    {
41
        $result = new Result();
42
        if (!$this->getRuleSet()->validate($value, $context)->isValid()) {
43 2
            $result->addError($this->formatMessage($this->message));
44
        }
45 2
46
        return $result;
47
    }
48
49
    /**
50
     * Return custom rules set
51
     */
52
    abstract protected function getRuleSet(): RuleSet;
53
54
    public function getOptions(): array
55
    {
56
        return $this->getRuleSet()->asArray();
57
    }
58
}
59