-
In my APIs we use the full ( The library provides From the docs it is possible to add custom constructor What is a supposed way of doing this if I don't want to disconnect the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Hi @repli2dev, sorry for the late answer. I'll need more details, could you post what |
Beta Was this translation helpful? Give feedback.
-
Sadly I lost the playground with the code, I tried to reconstruct:
This provides undesired time part in the
No change with
Not even with
What I need is to ensure that the DateOnly value is treated with its constructor and other |
Beta Was this translation helpful? Give feedback.
-
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')
->registerConstructor(function (string $date): DateOnly {
return (new DateOnly($date))->setTime(hour: 0, minute: 0, second: 0, microsecond: 0);
})
->mapper()
->map(MyClass::class, \CuyZ\Valinor\Mapper\Source\Source::json($json));
var_dump($foo);
} catch (\CuyZ\Valinor\Mapper\MappingError $error) {
throw $error;
} I'll be closing the issue; if it does not answer your needs please feel free to reopen! |
Beta Was this translation helpful? Give feedback.
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: