Passed
Pull Request — master (#595)
by
unknown
02:31
created

StringValue::getOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 9
rs 10
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Attribute;
8
use Closure;
9
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
10
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
11
use Yiisoft\Validator\Rule\Trait\WhenTrait;
12
use Yiisoft\Validator\RuleWithOptionsInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\SkipOnErrorInterface;
15
use Yiisoft\Validator\WhenInterface;
16
17
/**
18
 * Defines validation options to check that the value is a string.
19
 *
20
 * When you also need to check the length of the value, use just {@see Length} rule instead (without combining them).
21
 *
22
 * @see StringValueHandler
23
 *
24
 * @psalm-import-type WhenType from WhenInterface
25
 */
26
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
27
final class StringValue implements
28
    RuleWithOptionsInterface,
29
    SkipOnErrorInterface,
30
    WhenInterface,
31
    SkipOnEmptyInterface
32
{
33
    use SkipOnEmptyTrait;
34
    use SkipOnErrorTrait;
35
    use WhenTrait;
36
37
    /**
38
     * @param string $message Error message used when the value is not a string.
39
     *
40
     * You may use the following placeholders in the message:
41
     *
42
     * - `{attribute}`: the translated label of the attribute being validated.
43
     * - `{type}`: the type of the value being validated.
44
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
45
     * See {@see SkipOnEmptyInterface}.
46
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
47
     * See {@see SkipOnErrorInterface}.
48
     * @param Closure|null $when A callable to define a condition for applying the rule.
49
     * See {@see WhenInterface}.
50
     *
51
     * @psalm-param WhenType $when
52
     */
53
    public function __construct(
54
        private string $message = 'The value must be a string.',
55
        private mixed $skipOnEmpty = null,
56
        private bool $skipOnError = false,
57
        private Closure|null $when = null,
58
    ) {
59
    }
60
61
    public function getName(): string
62
    {
63
        return 'string';
64
    }
65
66
    /**
67
     * Get error message used when the value is not a string.
68
     *
69
     * @return string Error message.
70
     *
71
     * @see $message
72
     */
73
    public function getMessage(): string
74
    {
75
        return $this->message;
76
    }
77
78
    public function getOptions(): array
79
    {
80
        return [
81
            'message' => [
82
                'template' => $this->message,
83
                'parameters' => [],
84
            ],
85
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
86
            'skipOnError' => $this->skipOnError,
87
        ];
88
    }
89
90
    public function getHandler(): string
91
    {
92
        return StringValueHandler::class;
93
    }
94
}
95