[CORE][Controller] Add facility for either returning null or throwing, from Controller->{int,string,bool}

This commit is contained in:
Hugo Sales 2022-01-03 17:39:28 +00:00
parent 141c5f6785
commit f5fc7b6cd1
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0

View File

@ -53,6 +53,7 @@ use Symfony\Component\HttpKernel\Event\ExceptionEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent; use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents; use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Validator\Exception\ValidatorException; use Symfony\Component\Validator\Exception\ValidatorException;
use Throwable;
/** /**
* @method ?int int(string $param) * @method ?int int(string $param)
@ -242,11 +243,21 @@ abstract class Controller extends AbstractController implements EventSubscriberI
{ {
switch ($method) { switch ($method) {
case 'int': case 'int':
return !$this->request->query->has($args[0]) ? null : $this->request->query->getInt($args[0]);
case 'bool': case 'bool':
return !$this->request->query->has($args[0]) ? null : $this->request->query->getBoolean($args[0]);
case 'string': 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': case 'params':
return $this->request->query->all(); return $this->request->query->all();
default: default: