This commit is contained in:
2021-09-06 11:12:52 +08:00
parent 53e0163388
commit ca24835df8
+43 -9
View File
@@ -3,6 +3,7 @@
namespace Server\Message;
use BadMethodCallException;
use Exception;
use Http\IInterface\AuthIdentity;
use JetBrains\PhpStorm\Pure;
use Psr\Http\Message\RequestInterface;
@@ -118,17 +119,50 @@ class Request implements RequestInterface
}
/**
* @param string $field
* @param int $max
* @return int
*/
public function size(string $field = 'size', int $max = 100): int
{
$size = (int)$this->query($field);
if ($size < 1) {
$size = 1;
} else if ($size > $max) {
$size = $max;
}
return $size;
}
/**
* @param string $field
* @param string $sizeField
* @param int $max
* @return float|int
*/
public function offset(string $field = 'page', string $sizeField = 'size', int $max = 100): float|int
{
$page = (int)$this->query($field);
if ($page < 1) {
$page = 1;
}
return ($page - 1) * $this->size($sizeField, $max);
}
/**
* @param $name
* @param $required
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function string($name, $required)
public function string($name, $required): mixed
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new \Exception('Parameter is required and cannot be empty.');
throw new Exception('Parameter is required and cannot be empty.');
}
}
return $data;
@@ -139,13 +173,13 @@ class Request implements RequestInterface
* @param $name
* @param $required
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function int($name, $required)
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new \Exception('Parameter is required and cannot be empty.');
throw new Exception('Parameter is required and cannot be empty.');
}
}
return (string)$data;
@@ -156,13 +190,13 @@ class Request implements RequestInterface
* @param $name
* @param $required
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function float($name, $required)
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new \Exception('Parameter is required and cannot be empty.');
throw new Exception('Parameter is required and cannot be empty.');
}
}
return (float)$data;
@@ -173,13 +207,13 @@ class Request implements RequestInterface
* @param $name
* @param $required
* @return mixed
* @throws \Exception
* @throws Exception
*/
public function array($name, $required)
{
if (is_null($data = $this->post($name))) {
if ($required) {
throw new \Exception('Parameter is required and cannot be empty.');
throw new Exception('Parameter is required and cannot be empty.');
}
}
if (!is_array($data)) return [];