From f5fc7b6cd197dc4ddf3c910cbd551866beb2971c Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Mon, 3 Jan 2022 17:39:28 +0000 Subject: [PATCH] [CORE][Controller] Add facility for either returning null or throwing, from Controller->{int,string,bool} --- src/Core/Controller.php | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/Core/Controller.php b/src/Core/Controller.php index 69196d9b00..e75e9c5c4f 100644 --- a/src/Core/Controller.php +++ b/src/Core/Controller.php @@ -53,6 +53,7 @@ use Symfony\Component\HttpKernel\Event\ExceptionEvent; use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\Validator\Exception\ValidatorException; +use Throwable; /** * @method ?int int(string $param) @@ -242,11 +243,21 @@ abstract class Controller extends AbstractController implements EventSubscriberI { switch ($method) { case 'int': - return !$this->request->query->has($args[0]) ? null : $this->request->query->getInt($args[0]); case 'bool': - return !$this->request->query->has($args[0]) ? null : $this->request->query->getBoolean($args[0]); case 'string': - return !$this->request->query->has($args[0]) ? null : $this->request->query->get($args[0]); + if ($this->request->query->has($args[0])) { + return match ($method) { + 'int' => $this->request->query->getInt($args[0]), + 'bool' => $this->request->query->getBoolean($args[0]), + 'string' => $this->request->query->get($args[0]), + default => throw new BugFoundException('Inconsistent switch/match spotted'), + }; + } elseif (\array_key_exists(1, $args) && $args[1] instanceof Throwable) { + throw $args[1]; + } else { + return null; + } + break; case 'params': return $this->request->query->all(); default: