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