Skip to content

Strict validation of different date (and time) formats #350

Closed Answered by romm
repli2dev asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @repli2dev, maybe I missed something, or maybe something was fixed in the library in the previous months, but the following code should give you what you want to achieve:

class DateOnly extends DateTimeImmutable
{
    public function __toString(): string
    {
        return $this->format('Y-m-d');
    }
}

final readonly class MyClass
{
    public string $name;

    public DateOnly $date;

    public DateTimeInterface $date2;
}

$json = <<<JSON
    {
        "name": "France",
        "date": "2021-01-01T02:02:03+10:00",
        "date2": "2021-01-01T01:02:03+10:00"
    }
JSON;

try {
    $foo = (new \CuyZ\Valinor\MapperBuilder())
        ->supportDateFormats(DATE_ATOM, 'Y-m-d')
        ->

Replies: 3 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@repli2dev
Comment options

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@repli2dev
Comment options

Answer selected by repli2dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants