From 3ac74bce0bc388b7f7ea561a683dda8accf1780a Mon Sep 17 00:00:00 2001 From: James Walker <jh_walker@outlook.com> Date: Fri, 21 Oct 2022 14:27:28 +0100 Subject: [PATCH 1/3] RequestHandler now actually constructs RequestBag instances; supporting 'RequiresAtLeastOneValue' on RequestBags --- src/Http/RequestBag.php | 10 +++++++++- src/Http/RequestHandler.php | 17 ++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Http/RequestBag.php b/src/Http/RequestBag.php index 51ee7986..d129020a 100644 --- a/src/Http/RequestBag.php +++ b/src/Http/RequestBag.php @@ -15,6 +15,14 @@ namespace Ardeidae\Http; * @license MIT License */ #[\Attribute(\Attribute::TARGET_CLASS)] -final class RequestBag {} +final class RequestBag { + + /** + * Construct an instance. + */ + public function __construct( + public readonly bool $RequiresAtLeastOneValue) {} + +} ?> diff --git a/src/Http/RequestHandler.php b/src/Http/RequestHandler.php index af1e4104..c363613c 100644 --- a/src/Http/RequestHandler.php +++ b/src/Http/RequestHandler.php @@ -61,13 +61,17 @@ class RequestHandler implements RequestLayer { $paramCls = new \ReflectionClass($typeName); - if ($paramCls -> getAttributes(RequestBag::CLASS)) { + if (($bagAttr = ($paramCls -> getAttributes(RequestBag::CLASS)[0] ?? null))) { + + $bagAttrInstance = $bagAttr -> newInstance(); + if ($paramClsConstructor = $paramCls -> getConstructor()) { + try { - $requestBags[$parameter -> getName()] = $this -> Basis -> getParameterResolver() -> resolveMethodParameters( + $requestBagParams = $this -> Basis -> getParameterResolver() -> resolveMethodParameters( $paramClsConstructor, $requestValues - ); + ); } catch (ParameterResolutionException $ex) { if ($this -> Config -> has(self::PARAM_EX_LOGGING_CONF)) { @@ -75,6 +79,13 @@ class RequestHandler implements RequestLayer { } return new Response(400); } + + if (empty($requestBagParams) && $bagAttrInstance -> RequiresAtLeastOneValue) { + return new Response(400); + } + + $requestBags[$parameter -> getName()] = $paramCls -> newInstance(...$requestBagParams); + } } -- GitLab From 1fb5f73cf4fa011e38811ef54aa53f93c13f0824 Mon Sep 17 00:00:00 2001 From: James Walker <jh_walker@outlook.com> Date: Fri, 21 Oct 2022 13:28:41 +0000 Subject: [PATCH 2/3] RequestHandler remove redundant whitespace --- src/Http/RequestHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/RequestHandler.php b/src/Http/RequestHandler.php index c363613c..82929bba 100644 --- a/src/Http/RequestHandler.php +++ b/src/Http/RequestHandler.php @@ -71,7 +71,7 @@ class RequestHandler implements RequestLayer { $requestBagParams = $this -> Basis -> getParameterResolver() -> resolveMethodParameters( $paramClsConstructor, $requestValues - ); + ); } catch (ParameterResolutionException $ex) { if ($this -> Config -> has(self::PARAM_EX_LOGGING_CONF)) { -- GitLab From 46a266d9edd9bf08e9a84b87b32ec7a13bffab2b Mon Sep 17 00:00:00 2001 From: James Walker <jh_walker@outlook.com> Date: Fri, 21 Oct 2022 13:28:49 +0000 Subject: [PATCH 3/3] RequestBag set default value for RequiresAtLeastOneValue --- src/Http/RequestBag.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Http/RequestBag.php b/src/Http/RequestBag.php index d129020a..304e515b 100644 --- a/src/Http/RequestBag.php +++ b/src/Http/RequestBag.php @@ -21,7 +21,7 @@ final class RequestBag { * Construct an instance. */ public function __construct( - public readonly bool $RequiresAtLeastOneValue) {} + public readonly bool $RequiresAtLeastOneValue=false) {} } -- GitLab