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

Required::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\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