Test Failed
Pull Request — master (#175)
by
unknown
12:57
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 7
c 0
b 0
f 0
dl 0
loc 28
ccs 11
cts 11
cp 1
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A validateValue() 0 9 3
A getOptions() 0 4 1
A __construct() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Yiisoft\Validator\FormatterInterface;
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
final class Required extends Rule
18
{
19
    public function __construct(
20
        private string $message = 'Value cannot be blank.',
21
22
        ?FormatterInterface $formatter = null,
23 17
        bool $skipOnEmpty = false,
24
        bool $skipOnError = false,
25 17
        $when = null
26
    ) {
27
        parent::__construct(formatter: $formatter, skipOnEmpty: $skipOnEmpty, skipOnError: $skipOnError, when: $when);
28 13
    }
29
30 13
    protected function validateValue($value, ?ValidationContext $context = null): Result
31
    {
32 13
        $result = new Result();
33 3
34
        if ($this->isEmpty(is_string($value) ? trim($value) : $value)) {
35
            $result->addError($this->formatMessage($this->message));
36 13
        }
37
38
        return $result;
39 4
    }
40
41 4
    public function getOptions(): array
42 4
    {
43
        return array_merge(parent::getOptions(), [
44 4
            'message' => $this->formatMessage($this->message),
45
        ]);
46
    }
47
}
48