Passed
Pull Request — master (#595)
by
unknown
12:07
created

StringValue   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
dl 0
loc 67
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getHandler() 0 3 1
A getOptions() 0 9 1
A getName() 0 3 1
A getMessage() 0 3 1
A __construct() 0 6 1
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
 * @see StringValueHandler
21
 *
22
 * @psalm-import-type WhenType from WhenInterface
23
 */
24
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
25
final class StringValue implements
26
    RuleWithOptionsInterface,
27
    SkipOnErrorInterface,
28
    WhenInterface,
29
    SkipOnEmptyInterface
30
{
31
    use SkipOnEmptyTrait;
32
    use SkipOnErrorTrait;
33
    use WhenTrait;
34
35
    /**
36
     * @param string $message Error message used when the value is not a string.
37
     *
38
     * You may use the following placeholders in the message:
39
     *
40
     * - `{attribute}`: the translated label of the attribute being validated.
41
     * - `{type}`: the type of the value being validated.
42
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty.
43
     * See {@see SkipOnEmptyInterface}.
44
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error.
45
     * See {@see SkipOnErrorInterface}.
46
     * @param Closure|null $when A callable to define a condition for applying the rule.
47
     * See {@see WhenInterface}.
48
     *
49
     * @psalm-param WhenType $when
50
     */
51
    public function __construct(
52
        private string $message = 'The value must be a string.',
53
        private mixed $skipOnEmpty = null,
54
        private bool $skipOnError = false,
55
        private Closure|null $when = null,
56
    ) {
57
    }
58
59
    public function getName(): string
60
    {
61
        return 'string';
62
    }
63
64
    /**
65
     * Get error message used when the value is not a string.
66
     *
67
     * @return string Error message.
68
     *
69
     * @see $message
70
     */
71
    public function getMessage(): string
72
    {
73
        return $this->message;
74
    }
75
76
    public function getOptions(): array
77
    {
78
        return [
79
            'message' => [
80
                'template' => $this->message,
81
                'parameters' => [],
82
            ],
83
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
84
            'skipOnError' => $this->skipOnError,
85
        ];
86
    }
87
88
    public function getHandler(): string
89
    {
90
        return StringValueHandler::class;
91
    }
92
}
93