Passed
Pull Request — master (#99)
by Def
02:19
created

GroupRule   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 8 2
A getRawOptions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\DataSetInterface;
8
use Yiisoft\Validator\ErrorMessage;
9
use Yiisoft\Validator\ErrorMessageFormatterInterface;
10
use Yiisoft\Validator\HasValidationErrorMessage;
11
use Yiisoft\Validator\Result;
12
use Yiisoft\Validator\Rule;
13
use Yiisoft\Validator\Rules;
14
15
/**
16
 * GroupRule validates a single value for a set of custom rules
17
 */
18
abstract class GroupRule extends Rule
19
{
20
    use HasValidationErrorMessage;
21
22
    protected string $message = 'This value is not a valid.';
23
24 3
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
25
    {
26 3
        $result = new Result();
27 3
        if (!$this->getRules()->validate($value, $dataSet)->isValid()) {
28 3
            $result->addError(new ErrorMessage($this->message));
29
        }
30
31 3
        return $result;
32
    }
33
34
    /**
35
     * Return custom rules set
36
     *
37
     * @return Rules
38
     */
39
    abstract protected function getRules(): Rules;
40
41 3
    public function getRawOptions(?ErrorMessageFormatterInterface $formatter = null): array
42
    {
43 3
        return $this->getRules()->asArray($formatter);
44
    }
45
}
46