Passed
Pull Request — master (#81)
by Def
01:23
created

Required   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 29
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 9 3
A getName() 0 3 1
A getOptions() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\HasValidationErrorMessage;
8
use Yiisoft\Validator\Rule;
9
use Yiisoft\Validator\Result;
10
use Yiisoft\Validator\DataSetInterface;
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 2
    public function getName(): string
33
    {
34 2
        return 'required';
35
    }
36
37 2
    public function getOptions(): array
38
    {
39 2
        return array_merge(
40
            [
41 2
                'message' => $this->translateMessage($this->message)
42
            ],
43 2
            parent::getOptions()
44
        );
45
    }
46
}
47