diff --git a/CHANGELOG.md b/CHANGELOG.md index acede3f..a34d540 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,8 @@ All notable changes to this project will be documented in this file. Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles. ## [Unreleased][unreleased] +### Added +- Input type validation for InputParam ## 3.0.0 diff --git a/src/Params/GetInputParam.php b/src/Params/GetInputParam.php index b6b7a9c..750a1c3 100644 --- a/src/Params/GetInputParam.php +++ b/src/Params/GetInputParam.php @@ -4,6 +4,8 @@ namespace Tomaj\NetteApi\Params; +use Tomaj\NetteApi\Validation\InputValidator; + class GetInputParam extends InputParam { protected $type = self::TYPE_GET; @@ -14,6 +16,8 @@ public function getValue() return $_GET[$this->key]; } $value = $this->isMulti() ? filter_input(INPUT_GET, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) : filter_input(INPUT_GET, $this->key); - return $value !== null && $value !== false ? $value : $this->default; + $value = $value !== null && $value !== false ? $value : $this->default; + $inputValidator = new InputValidator(); + return $inputValidator->transformType($value, $this->valueType); } } diff --git a/src/Params/InputParam.php b/src/Params/InputParam.php index 468520f..02c15b3 100644 --- a/src/Params/InputParam.php +++ b/src/Params/InputParam.php @@ -7,21 +7,22 @@ use Nette\Application\UI\Form; use Nette\Forms\Controls\BaseControl; use Nette\Utils\Html; +use Tomaj\NetteApi\Validation\InputValidator; use Tomaj\NetteApi\ValidationResult\ValidationResult; use Tomaj\NetteApi\ValidationResult\ValidationResultInterface; abstract class InputParam implements ParamInterface { - const TYPE_POST = 'POST'; - const TYPE_GET = 'GET'; - const TYPE_PUT = 'PUT'; - const TYPE_FILE = 'FILE'; - const TYPE_COOKIE = 'COOKIE'; - const TYPE_POST_RAW = 'POST_RAW'; - const TYPE_POST_JSON = 'POST_JSON'; + public const TYPE_POST = 'POST'; + public const TYPE_GET = 'GET'; + public const TYPE_PUT = 'PUT'; + public const TYPE_FILE = 'FILE'; + public const TYPE_COOKIE = 'COOKIE'; + public const TYPE_POST_RAW = 'POST_RAW'; + public const TYPE_POST_JSON = 'POST_JSON'; - const OPTIONAL = false; - const REQUIRED = true; + public const OPTIONAL = false; + public const REQUIRED = true; /** @var string */ protected $type; @@ -47,9 +48,17 @@ abstract class InputParam implements ParamInterface /** @var array */ protected $examples = []; - public function __construct(string $key) + /** @var string|null */ + protected $valueType; + + /** + * InputParam constructor. + * @param string|null $valueType + */ + public function __construct(string $key, $valueType = null) { $this->key = $key; + $this->valueType = $valueType; } public function setRequired(): self @@ -238,6 +247,7 @@ public function validate(): ValidationResultInterface } } - return new ValidationResult(ValidationResult::STATUS_OK); + $inputValidator = new InputValidator(); + return $inputValidator->validate($value, $this->valueType); } } diff --git a/src/Params/PostInputParam.php b/src/Params/PostInputParam.php index e30e5fd..0e6a23d 100644 --- a/src/Params/PostInputParam.php +++ b/src/Params/PostInputParam.php @@ -4,6 +4,8 @@ namespace Tomaj\NetteApi\Params; +use Tomaj\NetteApi\Validation\InputValidator; + class PostInputParam extends InputParam { protected $type = self::TYPE_POST; @@ -14,6 +16,8 @@ public function getValue() return $_POST[$this->key]; } $value = $this->isMulti() ? filter_input(INPUT_POST, $this->key, FILTER_DEFAULT, FILTER_REQUIRE_ARRAY) : filter_input(INPUT_POST, $this->key); - return $value !== null && $value !== false ? $value : $this->default; + $value = $value !== null && $value !== false ? $value : $this->default; + $inputValidator = new InputValidator(); + return $inputValidator->transformType($value, $this->valueType); } } diff --git a/src/Validation/InputType.php b/src/Validation/InputType.php new file mode 100644 index 0000000..08f90d2 --- /dev/null +++ b/src/Validation/InputType.php @@ -0,0 +1,13 @@ +