Skip to content

Commit

Permalink
fix validator definition helper (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mararok authored Sep 25, 2019
1 parent b82c1bb commit 6be7d43
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 8 deletions.
5 changes: 3 additions & 2 deletions src/SAREhub/Microt/App/Request/AttributesValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -36,4 +37,4 @@ public function getName(): string
{
return "Request.Attributes";
}
}
}
3 changes: 2 additions & 1 deletion src/SAREhub/Microt/App/Request/ParsedBodyValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/SAREhub/Microt/App/Request/QueryParamsValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -36,4 +37,4 @@ public function getName(): string
{
return "Request.QueryParams";
}
}
}
32 changes: 29 additions & 3 deletions src/SAREhub/Microt/App/Request/ValidationDefinitionHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
}
}

0 comments on commit 6be7d43

Please sign in to comment.