-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[HttpKernel] Add DateTimeValueResolver #45589
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[HttpKernel] Add DateTimeValueResolver
- Loading branch information
commit dcc12280b72128c7b3358260513e7db86dff36ea
There are no files selected for viewing
This file contains hidden or 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
24 changes: 24 additions & 0 deletions
24
src/Symfony/Component/HttpKernel/Attribute/MapDateTime.php
This file contains hidden or 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,24 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Attribute; | ||
|
||
/** | ||
* Controller parameter tag to configure DateTime arguments. | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PARAMETER)] | ||
class MapDateTime | ||
{ | ||
public function __construct( | ||
public readonly ?string $format = null | ||
) { | ||
} | ||
} |
This file contains hidden or 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
77 changes: 77 additions & 0 deletions
77
src/Symfony/Component/HttpKernel/Controller/ArgumentResolver/DateTimeValueResolver.php
This file contains hidden or 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,77 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver; | ||
|
||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Attribute\MapDateTime; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
/** | ||
* Convert DateTime instances from request attribute variable. | ||
* | ||
* @author Benjamin Eberlei <kontakt@beberlei.de> | ||
* @author Tim Goudriaan <tim@codedmonkey.com> | ||
*/ | ||
final class DateTimeValueResolver implements ArgumentValueResolverInterface | ||
{ | ||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Request $request, ArgumentMetadata $argument): bool | ||
{ | ||
return is_a($argument->getType(), \DateTimeInterface::class, true) && $request->attributes->has($argument->getName()); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function resolve(Request $request, ArgumentMetadata $argument): iterable | ||
{ | ||
$value = $request->attributes->get($argument->getName()); | ||
|
||
if ($argument->isNullable() && !$value) { | ||
yield null; | ||
|
||
return; | ||
} | ||
|
||
$class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType(); | ||
$format = null; | ||
|
||
if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) { | ||
$attribute = $attributes[0]; | ||
$format = $attribute->format; | ||
} | ||
|
||
$date = false; | ||
|
||
if (null !== $format) { | ||
$date = $class::createFromFormat($format, $value); | ||
|
||
if ($class::getLastErrors()['warning_count']) { | ||
$date = false; | ||
} | ||
} elseif (false !== filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) { | ||
$date = new $class('@'.$value); | ||
} elseif (false !== $timestamp = strtotime($value)) { | ||
$date = new $class('@'.$timestamp); | ||
} | ||
|
||
if (!$date) { | ||
throw new NotFoundHttpException(sprintf('Invalid date given for parameter "%s".', $argument->getName())); | ||
} | ||
|
||
yield $date; | ||
} | ||
} |
166 changes: 166 additions & 0 deletions
166
...fony/Component/HttpKernel/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php
This file contains hidden or 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,166 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpKernel\Tests\Controller\ArgumentResolver; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Attribute\MapDateTime; | ||
use Symfony\Component\HttpKernel\Controller\ArgumentResolver\DateTimeValueResolver; | ||
use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata; | ||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | ||
|
||
class DateTimeValueResolverTest extends TestCase | ||
{ | ||
public function testSupports() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => 'now']); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
|
||
$argument = new ArgumentMetadata('dummy', FooDateTime::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => 'now']); | ||
$this->assertTrue($resolver->supports($request, $argument)); | ||
|
||
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => 'now']); | ||
$this->assertFalse($resolver->supports($request, $argument)); | ||
} | ||
|
||
public function testFullDate() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => '2012-07-21 00:00:00']); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
$results = iterator_to_array($results); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertInstanceOf(\DateTime::class, $results[0]); | ||
$this->assertEquals('2012-07-21', $results[0]->format('Y-m-d')); | ||
} | ||
|
||
public function testUnixTimestamp() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => '989541720']); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
$results = iterator_to_array($results); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertInstanceOf(\DateTime::class, $results[0]); | ||
$this->assertEquals('2001-05-11', $results[0]->format('Y-m-d')); | ||
} | ||
|
||
public function testNullableWithEmptyAttribute() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null, true); | ||
$request = self::requestWithAttributes(['dummy' => '']); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
$results = iterator_to_array($results); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertNull($results[0]); | ||
} | ||
|
||
public function testCustomClass() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', FooDateTime::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => '2016-09-08 00:00:00']); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
$results = iterator_to_array($results); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertInstanceOf(FooDateTime::class, $results[0]); | ||
$this->assertEquals('2016-09-08', $results[0]->format('Y-m-d')); | ||
} | ||
|
||
public function testDateTimeImmutable() | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null); | ||
$request = self::requestWithAttributes(['dummy' => '2016-09-08 00:00:00']); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
$results = iterator_to_array($results); | ||
|
||
$this->assertCount(1, $results); | ||
$this->assertInstanceOf(\DateTimeImmutable::class, $results[0]); | ||
$this->assertEquals('2016-09-08', $results[0]->format('Y-m-d')); | ||
} | ||
|
||
public function provideInvalidDates() | ||
{ | ||
return [ | ||
'invalid date' => [ | ||
new ArgumentMetadata('dummy', \DateTime::class, false, false, null), | ||
self::requestWithAttributes(['dummy' => 'Invalid DateTime Format']) | ||
], | ||
'invalid format' => [ | ||
new ArgumentMetadata('dummy', \DateTime::class, false, false, null, false, [new MapDateTime(format: 'd.m.Y')]), | ||
self::requestWithAttributes(['dummy' => '2012-07-21']), | ||
], | ||
'invalid ymd format' => [ | ||
new ArgumentMetadata('dummy', \DateTime::class, false, false, null, false, [new MapDateTime(format: 'Y-m-d')]), | ||
self::requestWithAttributes(['dummy' => '2012-21-07']), | ||
], | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvalidDates | ||
*/ | ||
public function test404Exception(ArgumentMetadata $argument, Request $request) | ||
{ | ||
$resolver = new DateTimeValueResolver(); | ||
|
||
$this->expectException(NotFoundHttpException::class); | ||
$this->expectExceptionMessage('Invalid date given for parameter "dummy".'); | ||
|
||
/** @var \Generator $results */ | ||
$results = $resolver->resolve($request, $argument); | ||
iterator_to_array($results); | ||
} | ||
|
||
private static function requestWithAttributes(array $attributes): Request | ||
{ | ||
$request = Request::create('/'); | ||
|
||
foreach ($attributes as $name => $value) { | ||
$request->attributes->set($name, $value); | ||
} | ||
|
||
return $request; | ||
} | ||
} | ||
|
||
class FooDateTime extends \DateTime | ||
nicolas-grekas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.