Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
feature #654 Allow numeric (unixtimestamp) value for DateTimeParamCon…
Browse files Browse the repository at this point in the history
…verter (jennevdmeer)

This PR was squashed before being merged into the 5.5.x-dev branch.

Discussion
----------

Allow numeric (unixtimestamp) value for DateTimeParamConverter

Commits
-------

89d6f62 Allow numeric (unixtimestamp) value for DateTimeParamConverter
  • Loading branch information
fabpot committed Apr 6, 2020
2 parents 95c5c58 + 89d6f62 commit d0585d4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Request/ParamConverter/DateTimeParamConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,16 @@ public function apply(Request $request, ParamConverter $configuration)
throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $param));
}
} else {
if (false === strtotime($value)) {
throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $param));
$valueIsInt = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]]);
if (false !== $valueIsInt) {
$date = (new $class())->setTimestamp($value);
} else {
if (false === strtotime($value)) {
throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $param));
}

$date = new $class($value);
}

$date = new $class($value);
}

$request->attributes->set($param, $date);
Expand Down
11 changes: 11 additions & 0 deletions tests/Request/ParamConverter/DateTimeParamConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public function testApply()
$this->assertEquals('2012-07-21', $request->attributes->get('start')->format('Y-m-d'));
}

public function testApplyUnixTimestamp()
{
$request = new Request([], [], ['start' => '989541720']);
$config = $this->createConfiguration('DateTime', 'start');

$this->converter->apply($request, $config);

$this->assertInstanceOf('DateTime', $request->attributes->get('start'));
$this->assertEquals('2001-05-11', $request->attributes->get('start')->format('Y-m-d'));
}

public function testApplyInvalidDate404Exception()
{
$this->expectException(\Symfony\Component\HttpKernel\Exception\NotFoundHttpException::class);
Expand Down

0 comments on commit d0585d4

Please sign in to comment.