Passed
Push — master ( 2b6758...acf834 )
by Aleksei
06:59
created

Required::validateValue()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 3
rs 10
c 0
b 0
f 0
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