UidValueResolver.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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 Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpKernel\Controller\ValueResolverInterface;
  13. use Symfony\Component\HttpKernel\ControllerMetadata\ArgumentMetadata;
  14. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  15. use Symfony\Component\Uid\AbstractUid;
  16. final class UidValueResolver implements ValueResolverInterface
  17. {
  18. public function resolve(Request $request, ArgumentMetadata $argument): array
  19. {
  20. if ($argument->isVariadic()
  21. || !\is_string($value = $request->attributes->get($argument->getName()))
  22. || null === ($uidClass = $argument->getType())
  23. || !is_subclass_of($uidClass, AbstractUid::class, true)
  24. ) {
  25. return [];
  26. }
  27. try {
  28. return [$uidClass::fromString($value)];
  29. } catch (\InvalidArgumentException $e) {
  30. throw new NotFoundHttpException(\sprintf('The uid for the "%s" parameter is invalid.', $argument->getName()), $e);
  31. }
  32. }
  33. }