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

Required   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 23
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 9 3
A getRawOptions() 0 6 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\HasValidationErrorMessage;
10
use Yiisoft\Validator\Result;
11
use Yiisoft\Validator\Rule;
12
13
/**
14
 * RequiredValidator validates that the specified attribute does not have null or empty value.
15
 */
16
class Required extends Rule
17
{
18
    use HasValidationErrorMessage;
19
20
    private string $message = 'Value cannot be blank.';
21
22 6
    protected function validateValue($value, DataSetInterface $dataSet = null): Result
23
    {
24 6
        $result = new Result();
25
26 6
        if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
27 2
            $result->addError(new ErrorMessage($this->message));
28
        }
29
30 6
        return $result;
31
    }
32
33 5
    public function getRawOptions(): array
34
    {
35 5
        return array_merge(
36 5
            parent::getRawOptions(),
37
            [
38 5
                'message' => new ErrorMessage($this->message),
39
            ],
40
        );
41
    }
42
}
43