-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
32d45c9
commit 4893110
Showing
3 changed files
with
76 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters