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

Required   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A rule() 0 3 1
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\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