diff --git a/src/SAREhub/Microt/App/Request/AttributesValidator.php b/src/SAREhub/Microt/App/Request/AttributesValidator.php index 241c89e..2e9b0c7 100644 --- a/src/SAREhub/Microt/App/Request/AttributesValidator.php +++ b/src/SAREhub/Microt/App/Request/AttributesValidator.php @@ -4,6 +4,7 @@ namespace SAREhub\Microt\App\Request; +use Respect\Validation\Validatable; use Respect\Validation\Validator; use SAREhub\Microt\Util\ValidatorHelper; use Slim\Http\Request; @@ -20,7 +21,7 @@ class AttributesValidator implements RequestValidator */ private $helper; - public function __construct(Validator $validator, ?ValidatorHelper $helper = null) + public function __construct(Validatable $validator, ?ValidatorHelper $helper = null) { $this->validator = $validator; $this->helper = $helper ?? new ValidatorHelper; @@ -36,4 +37,4 @@ public function getName(): string { return "Request.Attributes"; } -} \ No newline at end of file +} diff --git a/src/SAREhub/Microt/App/Request/ParsedBodyValidator.php b/src/SAREhub/Microt/App/Request/ParsedBodyValidator.php index f940489..ecf091a 100644 --- a/src/SAREhub/Microt/App/Request/ParsedBodyValidator.php +++ b/src/SAREhub/Microt/App/Request/ParsedBodyValidator.php @@ -4,6 +4,7 @@ namespace SAREhub\Microt\App\Request; +use Respect\Validation\Validatable; use Respect\Validation\Validator; use SAREhub\Microt\Util\ValidatorHelper; use Slim\Http\Request; @@ -21,7 +22,7 @@ class ParsedBodyValidator implements RequestValidator */ private $helper; - public function __construct(Validator $validator, ?ValidatorHelper $helper = null) + public function __construct(Validatable $validator, ?ValidatorHelper $helper = null) { $this->validator = $validator; $this->helper = $helper ?? new ValidatorHelper; diff --git a/src/SAREhub/Microt/App/Request/QueryParamsValidator.php b/src/SAREhub/Microt/App/Request/QueryParamsValidator.php index b347fb8..f2c4a56 100644 --- a/src/SAREhub/Microt/App/Request/QueryParamsValidator.php +++ b/src/SAREhub/Microt/App/Request/QueryParamsValidator.php @@ -4,6 +4,7 @@ namespace SAREhub\Microt\App\Request; +use Respect\Validation\Validatable; use Respect\Validation\Validator; use SAREhub\Microt\Util\ValidatorHelper; use Slim\Http\Request; @@ -20,7 +21,7 @@ class QueryParamsValidator implements RequestValidator */ private $helper; - public function __construct(Validator $validator, ?ValidatorHelper $helper = null) + public function __construct(Validatable $validator, ?ValidatorHelper $helper = null) { $this->validator = $validator; $this->helper = $helper ?? new ValidatorHelper(); @@ -36,4 +37,4 @@ public function getName(): string { return "Request.QueryParams"; } -} \ No newline at end of file +} diff --git a/src/SAREhub/Microt/App/Request/ValidationDefinitionHelper.php b/src/SAREhub/Microt/App/Request/ValidationDefinitionHelper.php index 0631ca3..1710ad4 100644 --- a/src/SAREhub/Microt/App/Request/ValidationDefinitionHelper.php +++ b/src/SAREhub/Microt/App/Request/ValidationDefinitionHelper.php @@ -4,7 +4,11 @@ namespace SAREhub\Microt\App\Request; +use DI\Definition\Definition; +use DI\Definition\Helper\DefinitionHelper; +use Respect\Validation\Validatable; use function DI\create; +use function DI\factory; class ValidationDefinitionHelper { @@ -23,18 +27,40 @@ public static function middleware($requestValidatorDef) return create(RequestValidationMiddleware::class)->constructor($requestValidatorDef); } + /** + * @param Validatable|Definition|DefinitionHelper $validator + * @return Definition|DefinitionHelper + */ public static function attributes($validator) { - return create(AttributesValidator::class)->constructor($validator); + return create(AttributesValidator::class)->constructor(self::validatorDef($validator)); } + /** + * @param Validatable|Definition|DefinitionHelper $validator + * @return Definition|DefinitionHelper + */ public static function queryParams($validator) { - return create(QueryParamsValidator::class)->constructor($validator); + return create(QueryParamsValidator::class)->constructor(self::validatorDef($validator)); } + /** + * @param Validatable|Definition|DefinitionHelper $validator + * @return Definition|DefinitionHelper + */ public static function parsedBody($validator) { - return create(ParsedBodyValidator::class)->constructor($validator); + return create(ParsedBodyValidator::class)->constructor(self::validatorDef($validator)); + } + + private static function validatorDef($validator) + { + if ($validator instanceof Validatable) { + return factory(function () use ($validator) { + return $validator; + }); + } + return $validator; } }