mirror of
https://codeberg.org/vlw/vlw.se.git
synced 2025-09-13 21:13:40 +02:00
feat(api): create DateTimeImmutable from timestamp for coffee POST endpoint (#49)
This PR allows creation of coffee entities at a specific date from a Unix timestamp by passing an integer to `date_created` when making a POST request to the `/coffee` endpoint. Reviewed-on: https://codeberg.org/vlw/vlw.se/pulls/49
This commit is contained in:
parent
d4d73e9278
commit
85e8e00091
1 changed files with 5 additions and 1 deletions
|
@ -16,6 +16,7 @@
|
||||||
parent::__construct(new Ruleset()->POST([
|
parent::__construct(new Ruleset()->POST([
|
||||||
new Rules(CoffeeTable::DATE_CREATED->value)
|
new Rules(CoffeeTable::DATE_CREATED->value)
|
||||||
->type(Type::STRING)
|
->type(Type::STRING)
|
||||||
|
->type(Type::NUMBER)
|
||||||
->default(null)
|
->default(null)
|
||||||
]));
|
]));
|
||||||
}
|
}
|
||||||
|
@ -26,7 +27,10 @@
|
||||||
// Parse DateTime from POST string
|
// Parse DateTime from POST string
|
||||||
if ($_POST[CoffeeTable::DATE_CREATED->value]) {
|
if ($_POST[CoffeeTable::DATE_CREATED->value]) {
|
||||||
try {
|
try {
|
||||||
$datetime = new DateTimeImmutable($_POST[CoffeeTable::DATE_CREATED->value]);
|
// Create DateTimeImmutable from Unix timestamp or datetime string
|
||||||
|
$datetime = gettype($_POST[CoffeeTable::DATE_CREATED->value]) === "integer"
|
||||||
|
? DateTimeImmutable::createFromTimestamp($_POST[CoffeeTable::DATE_CREATED->value])
|
||||||
|
: new DateTimeImmutable($_POST[CoffeeTable::DATE_CREATED->value]);
|
||||||
} catch (DateMalformedStringException $error) {
|
} catch (DateMalformedStringException $error) {
|
||||||
return new Response($error->getMessage(), 400);
|
return new Response($error->getMessage(), 400);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue