mirror of
https://github.com/skipperbent/simple-php-router.git
synced 2026-06-17 00:37:52 +00:00
8c5d8c2dc9
- Fixed: namespace being prepended on `RouterController` routes with absolute namespaces. - Fixed: match domain on `RouteController` and `RouteResource`. - Fixed: strict url-matching on `RouteController`. Fix should provide better url-matching. - Fixed: `RouterResource` always matching first url when having simular urls (ex: `/funny-cat` and `/funny-dog`). - Added `loadRoutes` method to `Router` class so routes now can be loaded without routing the request (useful when running in Cli etc). - Removed `getInstance` from `Router` class. Please use `SimpleRouter::router()` for singleton usage instead. - Added `getRemoteAddr` alias-method for `getIp` in `Request` class. - Added `getValue` to `IInputItem` interface. - Other minor optimizations. - Updated documentation with note on absolute/relative namespaces.
275 lines
4.9 KiB
PHP
275 lines
4.9 KiB
PHP
<?php
|
|
namespace Pecee\Http\Input;
|
|
|
|
class InputFile implements IInputItem
|
|
{
|
|
public $index;
|
|
public $name;
|
|
public $filename;
|
|
public $size;
|
|
public $type;
|
|
public $error;
|
|
public $tmpName;
|
|
|
|
public function __construct($index)
|
|
{
|
|
$this->index = $index;
|
|
|
|
// Make the name human friendly, by replace _ with space
|
|
$this->name = ucfirst(str_replace('_', ' ', $this->index));
|
|
}
|
|
|
|
/**
|
|
* Create from array
|
|
*
|
|
* @param array $values
|
|
* @throws \InvalidArgumentException
|
|
* @return static
|
|
*/
|
|
public static function createFromArray(array $values)
|
|
{
|
|
if (!isset($values['index'])) {
|
|
throw new \InvalidArgumentException('Index key is required');
|
|
}
|
|
|
|
/* Easy way of ensuring that all indexes-are set and not filling the screen with isset() */
|
|
|
|
$values = array_merge([
|
|
'tmp_name' => null,
|
|
'type' => null,
|
|
'size' => null,
|
|
'name' => null,
|
|
'error' => null,
|
|
], $values);
|
|
|
|
return (new static($values['index']))
|
|
->setSize($values['size'])
|
|
->setError($values['error'])
|
|
->setType($values['type'])
|
|
->setTmpName($values['tmp_name'])
|
|
->setFilename($values['name']);
|
|
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getIndex()
|
|
{
|
|
return $this->index;
|
|
}
|
|
|
|
/**
|
|
* Set input index
|
|
* @param string $index
|
|
* @return static $this
|
|
*/
|
|
public function setIndex($index)
|
|
{
|
|
$this->index = $index;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getSize()
|
|
{
|
|
return $this->size;
|
|
}
|
|
|
|
/**
|
|
* Set file size
|
|
* @param int $size
|
|
* @return static $this
|
|
*/
|
|
public function setSize($size)
|
|
{
|
|
$this->size = $size;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get mime-type of file
|
|
* @return string
|
|
*/
|
|
public function getMime()
|
|
{
|
|
return $this->getType();
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getType()
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
/**
|
|
* Set type
|
|
* @param string $type
|
|
* @return static $this
|
|
*/
|
|
public function setType($type)
|
|
{
|
|
$this->type = $type;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Returns extension without "."
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getExtension()
|
|
{
|
|
return pathinfo($this->getFilename(), PATHINFO_EXTENSION);
|
|
}
|
|
|
|
/**
|
|
* Get human friendly name
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getName()
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
/**
|
|
* Set human friendly name.
|
|
* Useful for adding validation etc.
|
|
*
|
|
* @param string $name
|
|
* @return static $this
|
|
*/
|
|
public function setName($name)
|
|
{
|
|
$this->name = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Set filename
|
|
*
|
|
* @param string $name
|
|
* @return static $this
|
|
*/
|
|
public function setFilename($name)
|
|
{
|
|
$this->filename = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Get filename
|
|
*
|
|
* @return string mixed
|
|
*/
|
|
public function getFilename()
|
|
{
|
|
return $this->filename;
|
|
}
|
|
|
|
/**
|
|
* Move the uploaded temporary file to it's new home
|
|
*
|
|
* @param string $destination
|
|
* @return bool
|
|
*/
|
|
public function move($destination)
|
|
{
|
|
return move_uploaded_file($this->tmpName, $destination);
|
|
}
|
|
|
|
/**
|
|
* Get file contents
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getContents()
|
|
{
|
|
return file_get_contents($this->tmpName);
|
|
}
|
|
|
|
/**
|
|
* Return true if an upload error occurred.
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function hasError()
|
|
{
|
|
return ($this->getError() !== 0);
|
|
}
|
|
|
|
/**
|
|
* Get upload-error code.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getError()
|
|
{
|
|
return $this->error;
|
|
}
|
|
|
|
/**
|
|
* Set error
|
|
*
|
|
* @param int $error
|
|
* @return static $this
|
|
*/
|
|
public function setError($error)
|
|
{
|
|
$this->error = (int)$error;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function getTmpName()
|
|
{
|
|
return $this->tmpName;
|
|
}
|
|
|
|
/**
|
|
* Set file temp. name
|
|
* @param string $name
|
|
* @return static $this
|
|
*/
|
|
public function setTmpName($name)
|
|
{
|
|
$this->tmpName = $name;
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function __toString()
|
|
{
|
|
return $this->getTmpName();
|
|
}
|
|
|
|
public function getValue()
|
|
{
|
|
return $this->getFilename();
|
|
}
|
|
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'tmp_name' => $this->tmpName,
|
|
'type' => $this->type,
|
|
'size' => $this->size,
|
|
'name' => $this->filename,
|
|
'error' => $this->error,
|
|
];
|
|
}
|
|
|
|
} |