Passed
Pull Request — master (#646)
by
unknown
02:35
created

Date::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
cc 3
eloc 6
c 2
b 1
f 1
nc 3
nop 7
dl 0
loc 20
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Validator\Rule;
6
7
use Closure;
8
use Attribute;
9
use InvalidArgumentException;
10
use Yiisoft\Validator\WhenInterface;
11
use Yiisoft\Validator\DumpedRuleInterface;
12
use Yiisoft\Validator\SkipOnErrorInterface;
13
use Yiisoft\Validator\SkipOnEmptyInterface;
14
use Yiisoft\Validator\Rule\Trait\WhenTrait;
15
use Yiisoft\Validator\RuleHandlerInterface;
16
use Yiisoft\Validator\Rule\Trait\SkipOnEmptyTrait;
17
use Yiisoft\Validator\Rule\Trait\SkipOnErrorTrait;
18
19
/**
20
 * @see DateHandler
21
 * @psalm-import-type WhenType from WhenInterface
22
 */
23
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
24
final class Date implements DumpedRuleInterface, SkipOnErrorInterface, WhenInterface, SkipOnEmptyInterface
25
{
26
    use SkipOnEmptyTrait;
27
    use SkipOnErrorTrait;
28
    use WhenTrait;
29
30
    /**
31
     * @var string The regular expression used to validate the value. See
32
     * {@link https://www.regular-expressions.info/email.html}.
33
     * @psalm-var non-empty-string
34
     */
35
    private string $pattern;
36
37
    /**
38
     * @psalm-var non-empty-string
39
     */
40
    private string $format;
41
    /**
42
     * @param string $format The format of the date.
43
     * @param string $message A message used when the value is not valid.
44
     *You may use the following placeholders in the message:
45
     * - `{attribute}`: the translated label of the attribute being validated.
46
     * - `{value}`: the value of the attribute being validated.
47
     * @param string $incorrectInputMessage A message used when the input is incorrect.
48
     * You may use the following placeholders in the message:
49
     * - `{attribute}`: the translated label of the attribute being validated.
50
     * - `{type}`: the type of the value being validated.
51
     * @param bool|callable|null $skipOnEmpty Whether to skip this rule if the value validated is empty. See {@see SkipOnEmptyInterface}.
52
     * @param bool $skipOnError Whether to skip this rule if any of the previous rules gave an error. See {@see SkipOnErrorInterface}.
53
     * @param Closure|null $when A callable to define a condition for applying the rule. See {@see WhenInterface}.
54
     * @psalm-param WhenType $when
55
     */
56
    public function __construct(
57
        string $format = 'Y-m-d',
58
        string $pattern = '/^(?=.*Y)(?=.*[mM])(?=.*d).*[Ymd](-|\/|.)[Ymd]\1[Ymd]$/',
59
        private string $incorrectInputMessage = 'The {attribute} must be a date.',
60
        private string $message = 'The {attribute} is not a valid date.',
61
        private mixed $skipOnEmpty = null,
62
        private bool $skipOnError = false,
63
        private ?Closure $when = null,
64
    ) {
65
        if ($pattern === '') {
66
            throw new InvalidArgumentException('Pattern can\'t be empty.');
67
        }
68
69
        $this->pattern = $pattern;
70
71
        if ($format === '') {
72
            throw new InvalidArgumentException('Format can\'t be empty.');
73
        }
74
75
        $this->format = $format;
76
    }
77
78
    /**
79
     *  The format date.
80
     *
81
     * @return string The format.
82
     * @psalm-return non-empty-string
83
     *
84
     * @see $format
85
     */
86
87
    public function getFormat(): string
88
    {
89
        return $this->format;
90
    }
91
92
    /**
93
     * Get a message used when the input is incorrect.
94
     *
95
     * @return string A message used when the input is incorrect.
96
     * @see $incorrectInputMessage
97
     */
98
    public function getIncorrectInputMessage(): string
99
    {
100
        return $this->incorrectInputMessage;
101
    }
102
103
    public function getOptions(): array
104
    {
105
        return [
106
            'format' => $this->format,
107
            'pattern' => $this->pattern,
108
            'incorrectInputMessage' => [
109
                'template' => $this->incorrectInputMessage,
110
                'parameters' => [],
111
            ],
112
            'message' => [
113
                'template' => $this->message,
114
                'parameters' => [],
115
            ],
116
            'skipOnEmpty' => $this->getSkipOnEmptyOption(),
117
            'skipOnError' => $this->skipOnError,
118
        ];
119
    }
120
121
    public function getMessage(): string
122
    {
123
        return $this->message;
124
    }
125
126
    public function getName(): string
127
    {
128
        return self::class;
129
    }
130
131
    public function getHandler(): string|RuleHandlerInterface
132
    {
133
        return DateHandler::class;
134
    }
135
    /**
136
     * Get the regular expression used to validate the value.
137
     *
138
     * @return string The regular expression.
139
     * @psalm-return non-empty-string
140
     *
141
     * @see $pattern
142
     */
143
    public function getPattern(): string
144
    {
145
        return $this->pattern;
146
    }
147
}
148