Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 5 additions & 1 deletion src/Params/GetInputParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Tomaj\NetteApi\Params;

use Tomaj\NetteApi\Validation\InputValidator;

class GetInputParam extends InputParam
{
protected $type = self::TYPE_GET;
Expand All @@ -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);
}
}
32 changes: 21 additions & 11 deletions src/Params/InputParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -238,6 +247,7 @@ public function validate(): ValidationResultInterface
}
}

return new ValidationResult(ValidationResult::STATUS_OK);
$inputValidator = new InputValidator();
return $inputValidator->validate($value, $this->valueType);
}
}
6 changes: 5 additions & 1 deletion src/Params/PostInputParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Tomaj\NetteApi\Params;

use Tomaj\NetteApi\Validation\InputValidator;

class PostInputParam extends InputParam
{
protected $type = self::TYPE_POST;
Expand All @@ -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);
}
}
13 changes: 13 additions & 0 deletions src/Validation/InputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Tomaj\NetteApi\Validation;

class InputType
{
public const BOOLEAN = 'boolean';
public const INTEGER = 'integer';
public const DOUBLE = 'double';
public const FLOAT = 'float';
public const STRING = 'string';
public const ARRAY = 'array';
}
103 changes: 103 additions & 0 deletions src/Validation/InputValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<?php

declare(strict_types=1);

namespace Tomaj\NetteApi\Validation;

use Tomaj\NetteApi\ValidationResult\ValidationResult;
use Tomaj\NetteApi\ValidationResult\ValidationResultInterface;

class InputValidator
{
/**
* Summary of validate
* @param mixed $value
* @param ?string $expectedType
*/
public function validate($value, $expectedType = null): ValidationResultInterface
{
if ($value === null || $expectedType === null) {
return new ValidationResult(ValidationResult::STATUS_OK);
}

switch ($expectedType) {
case InputType::BOOLEAN:
if (!is_bool($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected boolean.']);
}
break;
case InputType::INTEGER:
if (!is_int($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected integer.']);
}
break;
case InputType::DOUBLE:
if (!is_float($value) && !is_int($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected double.']);
}
break;
case InputType::FLOAT:
if (!is_float($value) && !is_int($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected float.']);
}
break;
case InputType::STRING:
if (!is_string($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected string.']);
}
break;
case InputType::ARRAY:
if (!is_array($value)) {
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type. Expected array.']);
}
break;
default:
return new ValidationResult(ValidationResult::STATUS_ERROR, ['Value ' . $value . ' has invalid type.']);
}
return new ValidationResult(ValidationResult::STATUS_OK);
}

/**
* Summary of transformType
* @param mixed $value
* @param ?string $expectedType
*/
public function transformType($value, $expectedType = null)
{
if ($value === null || $expectedType === null) {
return $value;
}
switch ($expectedType) {
case InputType::BOOLEAN:
if ($value === '1' || $value === 1 || $value === true || strtolower((string) $value) === 'true') {
return true;
} else {
return false;
}
// no break
case InputType::INTEGER:
if (is_numeric($value)) {
settype($value, 'integer');
}
break;
case InputType::DOUBLE:
if (is_numeric($value)) {
settype($value, 'double');
}
break;
case InputType::FLOAT:
if (is_numeric($value)) {
settype($value, 'float');
}
break;
case InputType::STRING:
if (is_string($value)) {
settype($value, 'string');
}
break;
default:
return $value;
}
return $value;
}
}