Passed
Push — master ( 99ab46...a20a6f )
by Alexander
02:59
created

Required::rule()   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\Result;
9
use Yiisoft\Validator\Rule;
10
use Yiisoft\Validator\ValidationContext;
11
12
use function is_string;
13
14
/**
15
 * RequiredValidator validates that the specified attribute does not have null or empty value.
16
 */
17
class Required extends Rule
18
{
19
    use HasValidationErrorMessage;
20
21
    private string $message = 'Value cannot be blank.';
22
23 17
    public static function rule(): self
24
    {
25 17
        return new self();
26
    }
27
28 13
    protected function validateValue($value, ValidationContext $context = null): Result
29
    {
30 13
        $result = new Result();
31
32 13
        if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
33 3
            $result->addError($this->formatMessage($this->message));
34
        }
35
36 13
        return $result;
37
    }
38
39 4
    public function getOptions(): array
40
    {
41 4
        return array_merge(
42 4
            parent::getOptions(),
43
            [
44 4
                'message' => $this->formatMessage($this->message),
45
            ],
46
        );
47
    }
48
}
49