Skip to content

Commit

Permalink
new dateTime rule added
Browse files Browse the repository at this point in the history
  • Loading branch information
Triplkrypl committed Jan 7, 2025
1 parent 32d45c9 commit 4893110
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .phive/phars.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phive xmlns="https://phar.io/phive">
<phar name="php-cs-fixer" version="^3.2" installed="3.52.1" location="./tools/php-cs-fixer" copy="false"/>
<phar name="php-cs-fixer" version="^3.2" installed="3.66.2" location="./tools/php-cs-fixer" copy="false"/>
<phar name="phpunit" version="^10.2.6" installed="10.4.2" location="./tools/phpunit" copy="false"/>
</phive>
57 changes: 57 additions & 0 deletions src/Rule/DateTime/DateTime.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

declare(strict_types=1);

namespace SimpleAsFuck\Validator\Rule\DateTime;

use SimpleAsFuck\Validator\Factory\Exception;
use SimpleAsFuck\Validator\Model\RuleChain;
use SimpleAsFuck\Validator\Model\Validated;
use SimpleAsFuck\Validator\Model\ValueMust;
use SimpleAsFuck\Validator\Rule\General\Rule;

/**
* @extends Rule<string, non-empty-string>
*/
final class DateTime extends Rule
{
private readonly ?\DateTimeZone $timeZone;

/**
* @param RuleChain<covariant string> $ruleChain
* @param Validated<covariant mixed> $validated
* @param non-empty-string $valueName
* @param non-empty-string $format
* @param non-empty-string|null $timeZone
*/
public function __construct(
?Exception $exceptionFactory,
RuleChain $ruleChain,
Validated $validated,
string $valueName,
private readonly string $format,
?string $timeZone = null,
) {
parent::__construct($exceptionFactory, $ruleChain, $validated, $valueName);

$this->timeZone = $timeZone !== null ? new \DateTimeZone($timeZone) : null;
}

/**
* @param string $value
* @return non-empty-string
*/
protected function validate($value): string
{
$dateTime = \DateTime::createFromFormat($this->format, $value, $this->timeZone);
if ($dateTime === false) {
throw new ValueMust('be date time in format: \''.$this->format.'\' example: \''.(new \DateTimeImmutable('now', $this->timeZone))->format($this->format).'\'');
}

if ($this->timeZone !== null) {
$dateTime->setTimezone($this->timeZone);
}

return $dateTime->format($this->format);
}
}
18 changes: 18 additions & 0 deletions src/Rule/String/StringRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use SimpleAsFuck\Validator\Model\RuleChain;
use SimpleAsFuck\Validator\Model\Validated;
use SimpleAsFuck\Validator\Model\ValueMust;
use SimpleAsFuck\Validator\Rule\DateTime\DateTime;
use SimpleAsFuck\Validator\Rule\DateTime\ParseDateTime;
use SimpleAsFuck\Validator\Rule\Email\EmailRule;
use SimpleAsFuck\Validator\Rule\Enum\Enum;
Expand Down Expand Up @@ -256,6 +257,23 @@ public function parseDecimal(int $digits, int $decimals): Max
return $numericRule->maxDigit($digits)->maxDecimal($decimals);
}

/**
* @param non-empty-string $format
* @param non-empty-string|null $timeZone
* @return Rule<string, non-empty-string>
*/
public function dateTime(string $format, ?string $timeZone = null): Rule
{
return new DateTime(
$this->exceptionFactory,
$this->ruleChain(),
$this->validated,
$this->valueName.': \''.$this->nullable(true).'\'',
$format,
$timeZone,
);
}

/**
* @template TDateTime of \DateTimeInterface
* @param non-empty-string $format
Expand Down

0 comments on commit 4893110

Please sign in to comment.