DateTimeValueResolver.php 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\HttpKernel\Controller\ArgumentResolver;
  11. use Psr\Clock\ClockInterface;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpKernel\Attribute\MapDateTime;
  14. use Symfony\Component\HttpKernel\Controller\ArgumentValueResolverInterface;
  15. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  16. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  17. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  18. /**
  19. * Convert DateTime instances from request attribute variable.
  20. *
  21. * @author Benjamin Eberlei <kontakt@beberlei.de>
  22. * @author Tim Goudriaan <tim@codedmonkey.com>
  23. */
  24. final class DateTimeValueResolver implements ArgumentValueResolverInterface, ValueResolverInterface
  25. {
  26. public function __construct(
  27. private readonly ?ClockInterface $clock = null,
  28. ) {
  29. }
  30. /**
  31. * @deprecated since Symfony 6.2, use resolve() instead
  32. */
  33. public function supports(Request $request, ArgumentMetadata $argument): bool
  34. {
  35. @trigger_deprecation('symfony/http-kernel', '6.2', 'The "%s()" method is deprecated, use "resolve()" instead.', __METHOD__);
  36. return is_a($argument->getType(), \DateTimeInterface::class, true) && $request->attributes->has($argument->getName());
  37. }
  38. public function resolve(Request $request, ArgumentMetadata $argument): array
  39. {
  40. if (!is_a($argument->getType(), \DateTimeInterface::class, true) || !$request->attributes->has($argument->getName())) {
  41. return [];
  42. }
  43. $value = $request->attributes->get($argument->getName());
  44. $class = \DateTimeInterface::class === $argument->getType() ? \DateTimeImmutable::class : $argument->getType();
  45. if (!$value) {
  46. if ($argument->isNullable()) {
  47. return [null];
  48. }
  49. if (!$this->clock) {
  50. return [new $class()];
  51. }
  52. $value = $this->clock->now();
  53. }
  54. if ($value instanceof \DateTimeInterface) {
  55. return [$value instanceof $class ? $value : $class::createFromInterface($value)];
  56. }
  57. $format = null;
  58. if ($attributes = $argument->getAttributes(MapDateTime::class, ArgumentMetadata::IS_INSTANCEOF)) {
  59. $attribute = $attributes[0];
  60. $format = $attribute->format;
  61. }
  62. if (null !== $format) {
  63. $date = $class::createFromFormat($format, $value, $this->clock?->now()->getTimeZone());
  64. if (($class::getLastErrors() ?: ['warning_count' => 0])['warning_count']) {
  65. $date = false;
  66. }
  67. } else {
  68. if (false !== filter_var($value, \FILTER_VALIDATE_INT, ['options' => ['min_range' => 0]])) {
  69. $value = '@'.$value;
  70. }
  71. try {
  72. $date = new $class($value, $this->clock?->now()->getTimeZone());
  73. } catch (\Exception) {
  74. $date = false;
  75. }
  76. }
  77. if (!$date) {
  78. throw new NotFoundHttpException(\sprintf('Invalid date given for parameter "%s".', $argument->getName()));
  79. }
  80. return [$date];
  81. }
  82. }