Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Input Validation #519

Closed
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
27 changes: 27 additions & 0 deletions src/Pecee/Http/Exceptions/InputNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Pecee\Http\Exceptions;

use Throwable;

class InputNotFoundException extends \Exception
{

/**
* @var string $index
*/
private $index;

public function __construct($message, $index, $code = 0, Throwable $previous = null){
$this->index = $index;
parent::__construct($message, $code, $previous);
}

/**
* @return string
*/
public function getIndex(): string{
return $this->index;
}

}
27 changes: 27 additions & 0 deletions src/Pecee/Http/Exceptions/InputValidationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Pecee\Http\Exceptions;

use Throwable;

class InputValidationException extends \Exception
{

/**
* @var string $index
*/
private $index;

public function __construct($message, $index, $code = 0, Throwable $previous = null){
$this->index = $index;
parent::__construct($message, $code, $previous);
}

/**
* @return string
*/
public function getIndex(): string{
return $this->index;
}

}
13 changes: 13 additions & 0 deletions src/Pecee/Http/Input/IInputError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace Pecee\Http\Input;

interface IInputError
{

public function __construct(string $message, int $code = 0);

public function getMessage(): string;

public function setMessage();
}
51 changes: 51 additions & 0 deletions src/Pecee/Http/Input/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace Pecee\Http\Input;

use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Exceptions\InputNotFoundException;
use Pecee\Http\Exceptions\InputValidationException;
use Pecee\Http\Request;
use Pecee\SimpleRouter\SimpleRouter;

class InputHandler
{
Expand Down Expand Up @@ -220,6 +223,54 @@ protected function parseInputItem(array $array): array
return $list;
}

/**
* <p>If <b>Router::validationErrors</b> this function will throw <b>InputNotFoundException</b> and <b>InputValidationException</b></p>
* @param string $index
* @param callable|null $validator
* @return bool
*/
public function requireParameter(string $index, callable $validator = null): bool
{
if($this->exists($index)){
if($validator !== null){
if(!$validator($this->find($index))){
if(SimpleRouter::router()->isValidationErrors())
throw new InputValidationException('Failed to validate Input: ' . $index, $index);
return false;
}
}
return true;
}
if(SimpleRouter::router()->isValidationErrors())
throw new InputNotFoundException('Input not found: ' . $index, $index);
return false;
}

/**
* <p>Parameters can be a sequential array with a list of index.</p>
* <p>When the Parameter ist is associative the key is an index and the value is an callable which can return true or false.</p>
* <p>The first parameter of the callable is an InputItem or InputFile.</p>
* <p>If <b>Router::validationErrors</b> this function will throw <b>InputNotFoundException</b> and <b>InputValidationException</b></p>
* @param array $parameters - array<string> or array<string, callable>
* @return array|true
*/
public function requireParameters(array $parameters = array())
{
$errors = array();
foreach($parameters as $index => $validator){
if(is_numeric($index)){
$index = $validator;
$validator = null;
}
if(!$this->requireParameter($index, $validator)){
$errors[] = $index;
}
}
if(!empty($errors))
return $errors;
return true;
}

/**
* Find input object
*
Expand Down
45 changes: 45 additions & 0 deletions src/Pecee/Http/Input/InputItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class InputItem implements IInputItem, \IteratorAggregate
public $name;
public $value;

private $validator = null;

public function __construct(string $index, $value = null)
{
$this->index = $index;
Expand Down Expand Up @@ -72,6 +74,49 @@ public function setValue($value): IInputItem
return $this;
}

/**
* @param bool $forceNew
* @return InputValidator
*/
public function validate(bool $forceNew = false): InputValidator
{
if($this->validator === null || $forceNew)
$this->validator = new InputValidator($this);
return $this->validator;
}

/**
* @return bool
*/
public function parseBoolean(): bool
{
return filter_var($this->getValue(), FILTER_VALIDATE_BOOLEAN);
}

/**
* @return int
*/
public function parseInteger(): int
{
return filter_var($this->getValue(), FILTER_VALIDATE_INT);
}

/**
* @return float
*/
public function parseFloat(): float
{
return filter_var($this->getValue(), FILTER_VALIDATE_FLOAT);
}

/**
* @return string
*/
public function parseString(): string
{
return (string) $this->getValue();
}

public function __toString(): string
{
$value = $this->getValue();
Expand Down
Loading