Test Failed
Pull Request — master (#175)
by
unknown
12:57
created

Required::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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