Files
kiri-core/HttpServer/Http/HttpParams.php
T

410 lines
8.0 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
/**
* Created by PhpStorm.
* User: admin
* Date: 2019-03-18
* Time: 14:54
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-10-30 01:26:42 +08:00
2020-08-31 01:27:08 +08:00
namespace HttpServer\Http;
use Exception;
use HttpServer\Exception\RequestException;
2020-12-25 15:57:52 +08:00
use ReflectionException;
2020-09-17 14:12:14 +08:00
use Snowflake\Core\Help;
2020-12-24 11:12:23 +08:00
use Snowflake\Core\Json;
2020-12-25 15:57:52 +08:00
use Snowflake\Exception\NotFindClassException;
2020-08-31 12:38:32 +08:00
use Snowflake\Snowflake;
2020-08-31 01:27:08 +08:00
/**
* Class HttpParams
2020-08-31 12:38:32 +08:00
* @package Snowflake\Snowflake\Http
2020-08-31 01:27:08 +08:00
*/
class HttpParams
{
2020-11-19 10:24:22 +08:00
private ?array $body = [];
2020-09-17 14:12:14 +08:00
/** @var array */
2020-10-29 18:17:25 +08:00
private array $gets = [];
2020-09-17 14:12:14 +08:00
/** @var array */
2020-10-29 18:17:25 +08:00
private array $files = [];
2020-09-17 14:12:14 +08:00
/**
* HttpParams constructor.
* @param $body
* @param $get
* @param $files
*/
public function __construct($body, $get, $files)
{
$this->gets = $get ?? [];
$this->files = $files ?? [];
2020-10-30 01:26:54 +08:00
if (!is_array($body)) {
2020-10-30 12:28:38 +08:00
$this->body = Help::toArray($body);
2020-10-30 01:26:42 +08:00
} else {
2020-10-30 17:00:15 +08:00
$this->body = $body ?? [];
2020-09-17 14:12:14 +08:00
}
}
/**
* @return int
*/
2020-12-25 15:57:52 +08:00
public function offset(): int
2020-09-17 14:12:14 +08:00
{
return ($this->page() - 1) * $this->size();
}
/**
* @param array $data
* 批量添加数据
*/
public function setPosts(array $data)
{
if (!is_array($data)) {
return;
}
foreach ($data as $key => $vla) {
$this->body[$key] = $vla;
}
}
/**
* @param string $key
* @param string $value
*/
public function addGetParam(string $key, string $value)
{
$this->gets[$key] = $value;
}
/**
* @return int
*/
2020-12-25 15:57:52 +08:00
private function page(): int
2020-09-17 14:12:14 +08:00
{
return (int)$this->get('page', 1);
}
/**
* @return int
*/
2020-12-25 15:57:52 +08:00
public function size(): int
2020-09-17 14:12:14 +08:00
{
return (int)$this->get('size', 20);
}
/**
* @param $name
2020-12-25 15:57:52 +08:00
* @param null $defaultValue
* @param null $call
* @return mixed
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function get($name, $defaultValue = null, $call = null): mixed
2020-09-17 14:12:14 +08:00
{
return $this->gets[$name] ?? $defaultValue;
}
/**
* @param $name
* @param null $defaultValue
2020-12-25 15:57:52 +08:00
* @param null $call
* @return mixed
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function post($name, $defaultValue = null, $call = null): mixed
2020-09-17 14:12:14 +08:00
{
$data = $this->body[$name] ?? $defaultValue;
if ($call !== null) {
$data = call_user_func($call, $data);
}
return $data;
}
/**
* @param $name
2020-12-25 15:57:52 +08:00
* @return bool|string
2020-09-17 14:12:14 +08:00
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
public function json($name): bool|string
2020-09-17 14:12:14 +08:00
{
$data = $this->array($name);
if (empty($data)) {
2020-12-24 11:12:23 +08:00
return Json::encode([]);
2020-09-17 14:12:14 +08:00
} else if (!is_array($data)) {
2020-12-24 11:12:23 +08:00
return Json::encode([]);
2020-09-17 14:12:14 +08:00
}
2020-12-24 11:12:23 +08:00
return Json::encode($data);
2020-09-17 14:12:14 +08:00
}
/**
* @return array
*/
2020-12-25 15:57:52 +08:00
public function gets(): array
2020-09-17 14:12:14 +08:00
{
return $this->gets;
}
/**
* @return array
*/
2020-12-25 15:57:52 +08:00
public function params(): array
2020-09-17 14:12:14 +08:00
{
return array_merge($this->body ?? [], $this->files ?? []);
}
/**
* @return array
*/
2020-12-25 15:57:52 +08:00
public function load(): array
2020-09-17 14:12:14 +08:00
{
return array_merge($this->files, $this->body, $this->gets);
}
/**
* @param $name
* @param array $defaultValue
2020-12-25 15:57:52 +08:00
* @return mixed
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function array($name, $defaultValue = []): mixed
2020-09-17 14:12:14 +08:00
{
return $this->body[$name] ?? $defaultValue;
}
/**
* @param $name
2020-12-25 15:57:52 +08:00
* @return File|null
* @throws ReflectionException
* @throws NotFindClassException
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function file($name): File|null
2020-09-17 14:12:14 +08:00
{
if (!isset($this->files[$name])) {
return null;
}
$param = $this->files[$name];
$param['class'] = File::class;
return Snowflake::createObject($param);
}
/**
* @param $name
* @param bool $isNeed
2020-12-25 15:57:52 +08:00
* @return mixed
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
private function required($name, $isNeed = false): mixed
2020-09-17 14:12:14 +08:00
{
$int = $this->body[$name] ?? NULL;
if (is_null($int) && $isNeed === true) {
throw new RequestException("You need to add request parameter $name");
}
return $int;
}
/**
* @param $name
* @param bool $isNeed
* @param null $min
* @param null $max
2020-12-25 15:57:52 +08:00
* @return int|null
* @throws RequestException
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function int($name, $isNeed = FALSE, $min = NULL, $max = NULL): ?int
2020-09-17 14:12:14 +08:00
{
$int = $this->required($name, $isNeed);
2020-12-25 15:57:52 +08:00
if (is_null($int)) return null;
2020-09-17 14:12:14 +08:00
if (is_array($min)) {
list($min, $max) = $min;
}
2020-12-25 15:57:52 +08:00
$length = strlen((string)$int);
2020-09-17 14:12:14 +08:00
if (!is_numeric($int) || intval($int) != $int) {
throw new RequestException("The request parameter $name must integer.");
}
$this->between($length, $min, $max);
return (int)$int;
}
/**
* @param $name
* @param bool $isNeed
* @param int $round
* @return float
* @throws Exception
*/
2020-12-25 15:57:52 +08:00
public function float($name, $isNeed = FALSE, $round = 0): ?float
2020-09-17 14:12:14 +08:00
{
$int = $this->required($name, $isNeed);
if ($int === null) {
return null;
}
if ($round > 0) {
return round(floatval($int), $round);
} else {
return floatval($int);
}
}
/**
* @param $name
* @param bool $isNeed
* @param null $length
*
* @return string
* @throws
*/
2020-12-25 15:57:52 +08:00
public function string($name, $isNeed = FALSE, $length = NULL): string
2020-09-17 14:12:14 +08:00
{
$string = $this->required($name, $isNeed);
if ($string === null || $length === null) {
return $string;
}
if (!is_string($string)) {
$string = json_encode($string, JSON_UNESCAPED_UNICODE);
}
$_length = strlen($string);
if (is_array($length)) {
if (count($length) < 2) {
array_unshift($length, 0);
}
$this->between($_length, ...$length);
} else if (is_numeric($length) && $_length != $length) {
throw new RequestException("The length of the string must be $length characters");
}
return $string;
}
/**
* @param $_length
* @param $min
* @param $max
* @throws RequestException
*/
private function between($_length, $min, $max)
{
if ($min !== NULL && $_length < $min) {
throw new RequestException("The minimum value cannot be lower than $min");
}
if ($max !== NULL && $_length > $max) {
throw new RequestException("Maximum cannot exceed $max, has length " . $_length);
}
}
/**
* @param $name
* @param bool $isNeed
*
2020-12-25 15:57:52 +08:00
* @return string|null
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
public function email($name, $isNeed = FALSE): ?string
2020-09-17 14:12:14 +08:00
{
$email = $this->required($name, $isNeed);
if ($email === null) {
return null;
}
if (!preg_match('/^\w+([.-_]\w+)+@\w+(\.\w+)+$/', $email)) {
throw new RequestException("Request parameter $name is in the wrong format", 4001);
}
return $email;
}
/**
* @param $name
* @param bool $isNeed
*
2020-12-25 15:57:52 +08:00
* @return bool|string
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
public function bool($name, $isNeed = FALSE): bool|string
2020-09-17 14:12:14 +08:00
{
$email = $this->required($name, $isNeed);
if ($email === null) {
return false;
}
return (bool)$email;
}
/**
* @param $name
* @param null $default
*
2020-12-25 15:57:52 +08:00
* @return int|string|null
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
public function timestamp($name, $default = NULL): null|int|string
2020-09-17 14:12:14 +08:00
{
$value = $this->required($name, false);
if ($value === null) {
return $default;
}
if (!is_numeric($value)) {
throw new RequestException('The request param :attribute not is a timestamp value');
}
if (strlen((string)$value) != 10) {
throw new RequestException('The request param :attribute not is a timestamp value');
}
if (!date('YmdHis', $value)) {
throw new RequestException('The request param :attribute format error', 4001);
}
return $value;
}
/**
* @param $name
* @param null $default
*
2020-12-25 15:57:52 +08:00
* @return mixed
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
public function datetime($name, $default = NULL): mixed
2020-09-17 14:12:14 +08:00
{
$value = $this->required($name, false);
if ($value === null) {
return $default;
}
$match = '/^\d{4}.*?([1-12]).*([1-31]).*?[0-23].*?[0-59].*?[0-59].*?$/';
$match = preg_match($match, $value, $result);
if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001);
}
return $value;
}
/**
* @param $name
* @param null $default
2020-12-25 15:57:52 +08:00
* @return mixed
2020-09-17 14:12:14 +08:00
* @throws RequestException
*/
2020-12-25 15:57:52 +08:00
public function ip($name, $default = NULL): mixed
2020-09-17 14:12:14 +08:00
{
$value = $this->required($name, false);
if ($value == NULL) {
return $default;
}
$match = preg_match('/^\d{1,3}(\.\d{1,3}){3}$/', $value, $result);
if (!$match || $result[0] != $value) {
throw new RequestException('The request param :attribute format error', 4001);
}
return $value;
}
/**
* @param $name
2020-12-25 15:57:52 +08:00
* @return mixed
2020-09-17 14:12:14 +08:00
*/
2020-12-25 15:57:52 +08:00
public function __get($name): mixed
2020-09-17 14:12:14 +08:00
{
$load = $this->load();
return $load[$name] ?? null;
}
2020-08-31 01:27:08 +08:00
}