Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
pamparam83 committed Jan 11, 2024
1 parent 6bc6584 commit 7c85996
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/Rule/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Closure;
use Attribute;
use InvalidArgumentException;
use Yiisoft\Validator\WhenInterface;
use Yiisoft\Validator\DumpedRuleInterface;
use Yiisoft\Validator\SkipOnErrorInterface;
Expand All @@ -25,6 +26,17 @@ final class Date implements DumpedRuleInterface, SkipOnErrorInterface, WhenInter
use SkipOnErrorTrait;
use WhenTrait;

/**
* @var string The regular expression used to validate the value. See
* {@link https://www.regular-expressions.info/email.html}.
* @psalm-var non-empty-string
*/
private string $pattern;

/**
* @psalm-var non-empty-string
*/
private string $format;
/**
* @param string $format The format of the date.
* @param string $message A message used when the value is not valid.
Expand All @@ -41,16 +53,35 @@ final class Date implements DumpedRuleInterface, SkipOnErrorInterface, WhenInter
* @psalm-param WhenType $when
*/
public function __construct(
private string $format = 'Y-m-d',
private string $pattern = '/^(?=.*Y)(?=.*[mM])(?=.*d).*[Ymd](-|\/|.)[Ymd]\1[Ymd]$/',
string $format = 'Y-m-d',
string $pattern = '/^(?=.*Y)(?=.*[mM])(?=.*d).*[Ymd](-|\/|.)[Ymd]\1[Ymd]$/',
private string $incorrectInputMessage = 'The {attribute} must be a date.',
private string $message = 'The {attribute} is not a valid date.',
private mixed $skipOnEmpty = null,
private bool $skipOnError = false,
private ?Closure $when = null,
) {
if ($pattern === '') {
throw new InvalidArgumentException('Pattern can\'t be empty.');

Check warning on line 65 in src/Rule/Date.php

View check run for this annotation

Codecov / codecov/patch

src/Rule/Date.php#L65

Added line #L65 was not covered by tests
}

$this->pattern = $pattern;

if ($format === '') {
throw new InvalidArgumentException('Format can\'t be empty.');

Check warning on line 71 in src/Rule/Date.php

View check run for this annotation

Codecov / codecov/patch

src/Rule/Date.php#L71

Added line #L71 was not covered by tests
}

$this->format = $format;
}

/**
* The format date.
*
* @return string The format.
* @psalm-return non-empty-string
*
* @see $format
*/

public function getFormat(): string
{
Expand Down Expand Up @@ -100,7 +131,14 @@ public function getHandler(): string
{
return DateHandler::class;
}

/**
* Get the regular expression used to validate the value.
*
* @return string The regular expression.
* @psalm-return non-empty-string
*
* @see $pattern
*/
public function getPattern(): string
{
return $this->pattern;
Expand Down

0 comments on commit 7c85996

Please sign in to comment.