Added support for PHP7

This commit is contained in:
Simon Sessingø
2018-03-20 03:38:55 +01:00
parent d279d5598d
commit f23d569757
58 changed files with 678 additions and 532 deletions
+6 -6
View File
@@ -5,17 +5,17 @@ namespace Pecee\Http\Input;
interface IInputItem
{
public function getIndex();
public function getIndex(): string;
public function setIndex($index);
public function setIndex($index): self;
public function getName();
public function getName(): string;
public function setName($name);
public function setName($name): self;
public function getValue();
public function getValue(): string;
public function setValue($value);
public function setValue($value): self;
public function __toString();
+30 -30
View File
@@ -31,7 +31,7 @@ class InputFile implements IInputItem
* @throws InvalidArgumentException
* @return static
*/
public static function createFromArray(array $values)
public static function createFromArray(array $values): self
{
if (isset($values['index']) === false) {
throw new InvalidArgumentException('Index key is required');
@@ -59,7 +59,7 @@ class InputFile implements IInputItem
/**
* @return string
*/
public function getIndex()
public function getIndex(): string
{
return $this->index;
}
@@ -67,9 +67,9 @@ class InputFile implements IInputItem
/**
* Set input index
* @param string $index
* @return static $this
* @return static
*/
public function setIndex($index)
public function setIndex($index): IInputItem
{
$this->index = $index;
@@ -79,7 +79,7 @@ class InputFile implements IInputItem
/**
* @return string
*/
public function getSize()
public function getSize(): string
{
return $this->size;
}
@@ -87,9 +87,9 @@ class InputFile implements IInputItem
/**
* Set file size
* @param int $size
* @return static $this
* @return static
*/
public function setSize($size)
public function setSize($size): IInputItem
{
$this->size = $size;
@@ -100,7 +100,7 @@ class InputFile implements IInputItem
* Get mime-type of file
* @return string
*/
public function getMime()
public function getMime(): string
{
return $this->getType();
}
@@ -108,7 +108,7 @@ class InputFile implements IInputItem
/**
* @return string
*/
public function getType()
public function getType(): string
{
return $this->type;
}
@@ -116,9 +116,9 @@ class InputFile implements IInputItem
/**
* Set type
* @param string $type
* @return static $this
* @return static
*/
public function setType($type)
public function setType($type): IInputItem
{
$this->type = $type;
@@ -130,7 +130,7 @@ class InputFile implements IInputItem
*
* @return string
*/
public function getExtension()
public function getExtension(): string
{
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
}
@@ -140,7 +140,7 @@ class InputFile implements IInputItem
*
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -150,9 +150,9 @@ class InputFile implements IInputItem
* Useful for adding validation etc.
*
* @param string $name
* @return static $this
* @return static
*/
public function setName($name)
public function setName($name): IInputItem
{
$this->name = $name;
@@ -163,9 +163,9 @@ class InputFile implements IInputItem
* Set filename
*
* @param string $name
* @return static $this
* @return static
*/
public function setFilename($name)
public function setFilename($name): IInputItem
{
$this->filename = $name;
@@ -177,7 +177,7 @@ class InputFile implements IInputItem
*
* @return string mixed
*/
public function getFilename()
public function getFilename(): string
{
return $this->filename;
}
@@ -188,7 +188,7 @@ class InputFile implements IInputItem
* @param string $destination
* @return bool
*/
public function move($destination)
public function move($destination): bool
{
return move_uploaded_file($this->tmpName, $destination);
}
@@ -198,7 +198,7 @@ class InputFile implements IInputItem
*
* @return string
*/
public function getContents()
public function getContents(): string
{
return file_get_contents($this->tmpName);
}
@@ -208,7 +208,7 @@ class InputFile implements IInputItem
*
* @return bool
*/
public function hasError()
public function hasError(): bool
{
return ($this->getError() !== 0);
}
@@ -218,7 +218,7 @@ class InputFile implements IInputItem
*
* @return string
*/
public function getError()
public function getError(): string
{
return $this->errors;
}
@@ -227,9 +227,9 @@ class InputFile implements IInputItem
* Set error
*
* @param int $error
* @return static $this
* @return static
*/
public function setError($error)
public function setError($error): IInputItem
{
$this->errors = (int)$error;
@@ -239,7 +239,7 @@ class InputFile implements IInputItem
/**
* @return string
*/
public function getTmpName()
public function getTmpName(): string
{
return $this->tmpName;
}
@@ -247,9 +247,9 @@ class InputFile implements IInputItem
/**
* Set file temp. name
* @param string $name
* @return static $this
* @return static
*/
public function setTmpName($name)
public function setTmpName($name): IInputItem
{
$this->tmpName = $name;
@@ -261,7 +261,7 @@ class InputFile implements IInputItem
return $this->getTmpName();
}
public function getValue()
public function getValue(): string
{
return $this->getFilename();
}
@@ -270,14 +270,14 @@ class InputFile implements IInputItem
* @param string $value
* @return static
*/
public function setValue($value)
public function setValue($value): IInputItem
{
$this->filename = $value;
return $this;
}
public function toArray()
public function toArray(): array
{
return [
'tmp_name' => $this->tmpName,
@@ -5,7 +5,7 @@ namespace Pecee\Http\Input;
use Pecee\Exceptions\InvalidArgumentException;
use Pecee\Http\Request;
class Input
class InputHandler
{
/**
* @var array
@@ -42,26 +42,26 @@ class Input
* Parse input values
*
*/
public function parseInputs()
public function parseInputs() : void
{
/* Parse get requests */
if (count($_GET) !== 0) {
if (\count($_GET) !== 0) {
$this->get = $this->handleGetPost($_GET);
}
/* Parse post requests */
$postVars = $_POST;
if (in_array($this->request->getMethod(), ['put', 'patch', 'delete'], false) === true) {
if (\in_array($this->request->getMethod(), ['put', 'patch', 'delete'], false) === true) {
parse_str(file_get_contents('php://input'), $postVars);
}
if (count($postVars) !== 0) {
if (\count($postVars) !== 0) {
$this->post = $this->handleGetPost($postVars);
}
/* Parse get requests */
if (count($_FILES) !== 0) {
if (\count($_FILES) !== 0) {
$this->file = $this->parseFiles();
}
}
@@ -69,14 +69,14 @@ class Input
/**
* @return array
*/
public function parseFiles()
public function parseFiles() : array
{
$list = [];
foreach ((array)$_FILES as $key => $value) {
// Handle array input
if (is_array($value['name']) === false) {
if (\is_array($value['name']) === false) {
$values['index'] = $key;
try {
$list[$key] = InputFile::createFromArray($values + $value);
@@ -101,7 +101,7 @@ class Input
return $list;
}
protected function rearrangeFiles(array $values, &$index, $original)
protected function rearrangeFiles(array $values, &$index, $original) : array
{
$originalIndex = $index[0];
@@ -111,7 +111,7 @@ class Input
foreach ($values as $key => $value) {
if (is_array($original['name'][$key]) === false) {
if (\is_array($original['name'][$key]) === false) {
try {
@@ -152,14 +152,14 @@ class Input
return $output;
}
protected function handleGetPost(array $array)
protected function handleGetPost(array $array) : array
{
$list = [];
foreach ($array as $key => $value) {
// Handle array input
if (is_array($value) === false) {
if (\is_array($value) === false) {
$list[$key] = new InputItem($key, $value);
continue;
}
@@ -181,7 +181,7 @@ class Input
*/
public function findPost($index, $defaultValue = null)
{
return isset($this->post[$index]) ? $this->post[$index] : $defaultValue;
return $this->post[$index] ?? $defaultValue;
}
/**
@@ -193,7 +193,7 @@ class Input
*/
public function findFile($index, $defaultValue = null)
{
return isset($this->file[$index]) ? $this->file[$index] : $defaultValue;
return $this->file[$index] ?? $defaultValue;
}
/**
@@ -205,7 +205,7 @@ class Input
*/
public function findGet($index, $defaultValue = null)
{
return isset($this->get[$index]) ? $this->get[$index] : $defaultValue;
return $this->get[$index] ?? $defaultValue;
}
/**
@@ -218,25 +218,25 @@ class Input
*/
public function getObject($index, $defaultValue = null, $methods = null)
{
if ($methods !== null && is_string($methods) === true) {
if ($methods !== null && \is_string($methods) === true) {
$methods = [$methods];
}
$element = null;
if ($methods === null || in_array('get', $methods, false) === true) {
if ($methods === null || \in_array('get', $methods, true) === true) {
$element = $this->findGet($index);
}
if (($element === null && $methods === null) || ($methods !== null && in_array('post', $methods, false) === true)) {
if (($element === null && $methods === null) || ($methods !== null && \in_array('post', $methods, true) === true)) {
$element = $this->findPost($index);
}
if (($element === null && $methods === null) || ($methods !== null && in_array('file', $methods, false) === true)) {
if (($element === null && $methods === null) || ($methods !== null && \in_array('file', $methods, true) === true)) {
$element = $this->findFile($index);
}
return ($element !== null) ? $element : $defaultValue;
return $element ?? $defaultValue;
}
/**
@@ -264,7 +264,7 @@ class Input
* @param string $index
* @return bool
*/
public function exists($index)
public function exists($index) : bool
{
return ($this->getObject($index) !== null);
}
@@ -274,7 +274,7 @@ class Input
* @param array|null $filter Only take items in filter
* @return array
*/
public function all(array $filter = null)
public function all(array $filter = null) : array
{
$output = $_GET + $_POST;
+6 -6
View File
@@ -20,12 +20,12 @@ class InputItem implements IInputItem
/**
* @return string
*/
public function getIndex()
public function getIndex(): string
{
return $this->index;
}
public function setIndex($index)
public function setIndex($index): IInputItem
{
$this->index = $index;
@@ -35,7 +35,7 @@ class InputItem implements IInputItem
/**
* @return string
*/
public function getName()
public function getName(): string
{
return $this->name;
}
@@ -45,7 +45,7 @@ class InputItem implements IInputItem
* @param string $name
* @return static $this
*/
public function setName($name)
public function setName($name): IInputItem
{
$this->name = $name;
@@ -55,7 +55,7 @@ class InputItem implements IInputItem
/**
* @return string
*/
public function getValue()
public function getValue(): string
{
return $this->value;
}
@@ -65,7 +65,7 @@ class InputItem implements IInputItem
* @param string $value
* @return static $this
*/
public function setValue($value)
public function setValue($value): IInputItem
{
$this->value = $value;