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;
|
2021-02-02 19:18:06 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2020-12-25 15:57:52 +08:00
|
|
|
use ReflectionException;
|
2020-12-24 11:12:23 +08:00
|
|
|
use Snowflake\Core\Json;
|
2021-08-01 15:03:49 +08:00
|
|
|
use Snowflake\Core\Xml;
|
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
|
|
|
|
|
{
|
|
|
|
|
|
2021-08-01 15:07:55 +08:00
|
|
|
/** @var array|null */
|
|
|
|
|
private ?array $_gets = [];
|
2021-03-23 16:14:05 +08:00
|
|
|
|
2021-07-27 18:21:30 +08:00
|
|
|
|
2021-08-01 15:10:42 +08:00
|
|
|
/** @var array|null */
|
2021-08-01 15:07:55 +08:00
|
|
|
private ?array $_posts = [];
|
2021-03-23 16:14:05 +08:00
|
|
|
|
2021-08-01 15:03:49 +08:00
|
|
|
|
2021-08-01 15:07:55 +08:00
|
|
|
/** @var array|null */
|
|
|
|
|
private ?array $_files = [];
|
2021-08-01 15:03:49 +08:00
|
|
|
|
2021-08-01 15:07:55 +08:00
|
|
|
|
2021-08-01 15:10:42 +08:00
|
|
|
/** @var mixed|array */
|
2021-08-01 15:03:49 +08:00
|
|
|
private mixed $_rawContent = [];
|
2021-03-23 16:14:05 +08:00
|
|
|
|
2021-07-27 18:21:30 +08:00
|
|
|
|
2021-03-23 16:14:05 +08:00
|
|
|
/**
|
2021-07-27 18:21:30 +08:00
|
|
|
* @return mixed
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-07-27 18:21:30 +08:00
|
|
|
public function getRawContent(): mixed
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_rawContent;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function offset(): int
|
|
|
|
|
{
|
|
|
|
|
return ($this->page() - 1) * $this->size();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:10:42 +08:00
|
|
|
* @param mixed $data
|
2021-03-23 16:14:05 +08:00
|
|
|
* 批量添加数据
|
|
|
|
|
*/
|
2021-08-01 15:10:42 +08:00
|
|
|
public function setPosts(mixed $data)
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
|
|
|
|
if (!is_array($data)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-08-01 15:10:42 +08:00
|
|
|
if (is_object($data)) {
|
|
|
|
|
$data = get_object_vars($data);
|
|
|
|
|
}
|
2021-03-23 16:14:05 +08:00
|
|
|
foreach ($data as $key => $vla) {
|
2021-08-01 15:03:49 +08:00
|
|
|
$this->_posts[$key] = $vla;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:06:57 +08:00
|
|
|
* @param array|null $files
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:06:57 +08:00
|
|
|
public function setFiles(?array $files): void
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
$this->_files = $files;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:06:57 +08:00
|
|
|
* @param array|null $gets
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:06:57 +08:00
|
|
|
public function setGets(?array $gets): void
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
$this->_gets = $gets;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:03:49 +08:00
|
|
|
* @param mixed $content
|
|
|
|
|
* @param Request $contextType
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function setRawContent(mixed $content, Request $contextType)
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
if (empty($content)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$context_type = $contextType->headers->getContentType();
|
|
|
|
|
if (str_contains($context_type, 'json')) {
|
|
|
|
|
$this->setPosts(json_decode($content));
|
|
|
|
|
} else if (str_contains($context_type, 'xml')) {
|
|
|
|
|
$this->setPosts(Xml::toArray($content));
|
|
|
|
|
} else {
|
|
|
|
|
$this->_rawContent = $content;
|
|
|
|
|
}
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:08:30 +08:00
|
|
|
* @return array|null
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:08:30 +08:00
|
|
|
public function getBody(): ?array
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_posts;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:03:49 +08:00
|
|
|
* @return int
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
private function page(): int
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return (int)$this->get('page', 1);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-01 15:03:49 +08:00
|
|
|
* @param $name
|
|
|
|
|
* @param null $default
|
|
|
|
|
* @return mixed
|
2021-03-23 16:14:05 +08:00
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function query($name, $default = null): mixed
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_gets[$name] ?? $default;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
public function size(): int
|
|
|
|
|
{
|
|
|
|
|
return (int)$this->get('size', 20);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param null $defaultValue
|
|
|
|
|
* @param null $call
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function get($name, $defaultValue = null, $call = null): mixed
|
|
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_gets[$name] ?? $defaultValue;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param null $defaultValue
|
|
|
|
|
* @param null $call
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function post($name, $defaultValue = null, $call = null): mixed
|
|
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
$data = $this->_posts[$name] ?? $defaultValue;
|
2021-03-23 16:14:05 +08:00
|
|
|
if ($call !== null) {
|
|
|
|
|
$data = call_user_func($call, $data);
|
|
|
|
|
}
|
|
|
|
|
return $data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return bool|string
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function json($name): bool|string
|
|
|
|
|
{
|
|
|
|
|
$data = $this->array($name);
|
|
|
|
|
if (empty($data)) {
|
|
|
|
|
return Json::encode([]);
|
|
|
|
|
} else if (!is_array($data)) {
|
|
|
|
|
return Json::encode([]);
|
|
|
|
|
}
|
|
|
|
|
return Json::encode($data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function gets(): array
|
|
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_gets;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function params(): array
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return array_merge($this->_posts ?? [], $this->_files ?? []);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-08-01 15:03:49 +08:00
|
|
|
public function load(): array
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return array_merge($this->_files ?? [], $this->_posts ?? [], $this->_gets ?? []);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @param array $defaultValue
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2021-07-09 15:02:08 +08:00
|
|
|
public function array($name, array $defaultValue = []): mixed
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
return $this->_posts[$name] ?? $defaultValue;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return File|null
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
*/
|
|
|
|
|
public function file($name): File|null
|
|
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
$param = $this->_files[$name] ?? null;
|
|
|
|
|
if (!empty($param)) {
|
|
|
|
|
$param['class'] = File::class;
|
|
|
|
|
return Snowflake::createObject($param);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
2021-08-01 15:03:49 +08:00
|
|
|
return null;
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-09 15:02:08 +08:00
|
|
|
* @param string $name
|
2021-03-23 16:14:05 +08:00
|
|
|
* @param bool $isNeed
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
private function required(string $name, bool $isNeed = false): mixed
|
|
|
|
|
{
|
2021-08-01 15:03:49 +08:00
|
|
|
$int = $this->_posts[$name] ?? NULL;
|
2021-03-23 16:14:05 +08:00
|
|
|
if (is_null($int) && $isNeed === true) {
|
|
|
|
|
throw new RequestException("You need to add request parameter $name");
|
|
|
|
|
}
|
|
|
|
|
return $int;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param bool $isNeed
|
|
|
|
|
* @param array|int|null $min
|
|
|
|
|
* @param int|null $max
|
|
|
|
|
* @return int|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function int(string $name, bool $isNeed = FALSE, array|int|null $min = NULL, int|null $max = NULL): ?int
|
|
|
|
|
{
|
2021-07-11 02:05:56 +08:00
|
|
|
return (int)$this->required($name, $isNeed);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-09 15:02:08 +08:00
|
|
|
* @param string $name
|
2021-03-23 16:14:05 +08:00
|
|
|
* @param bool $isNeed
|
|
|
|
|
* @param int $round
|
|
|
|
|
* @return float|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
2021-07-09 15:02:08 +08:00
|
|
|
public function float(string $name, bool $isNeed = FALSE, int $round = 0): ?float
|
2021-03-23 16:14:05 +08:00
|
|
|
{
|
2021-07-27 18:21:30 +08:00
|
|
|
return (float)$this->required($name, $isNeed);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param bool $isNeed
|
|
|
|
|
* @param int|array|null $length
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function string(string $name, bool $isNeed = FALSE, int|array|null $length = NULL): ?string
|
|
|
|
|
{
|
2021-07-11 02:05:56 +08:00
|
|
|
return (string)$this->required($name, $isNeed);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-07-09 15:02:08 +08:00
|
|
|
* @param string $name
|
2021-03-23 16:14:05 +08:00
|
|
|
* @param bool $isNeed
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function email(string $name, bool $isNeed = FALSE): ?string
|
|
|
|
|
{
|
|
|
|
|
$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 string $name
|
|
|
|
|
* @param bool $isNeed
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function bool(string $name, bool $isNeed = FALSE): bool
|
|
|
|
|
{
|
2021-07-11 02:05:56 +08:00
|
|
|
return (boolean)$this->required($name, $isNeed);
|
2021-03-23 16:14:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param int|null $default
|
|
|
|
|
*
|
|
|
|
|
* @return int|string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function timestamp(string $name, int|null $default = NULL): null|int|string
|
|
|
|
|
{
|
|
|
|
|
$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 string $name
|
|
|
|
|
* @param string|null $default
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function datetime(string $name, string $default = NULL): string|null
|
|
|
|
|
{
|
|
|
|
|
$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;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-06 18:54:28 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|null $default
|
|
|
|
|
*
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function date(string $name, string $default = NULL): string|null
|
|
|
|
|
{
|
|
|
|
|
$value = $this->required($name, false);
|
|
|
|
|
if ($value === null) {
|
|
|
|
|
return $default;
|
|
|
|
|
}
|
|
|
|
|
$match = '/^\d{4}.*?([1-12]).*([1-31])$/';
|
|
|
|
|
$match = preg_match($match, $value, $result);
|
|
|
|
|
if (!$match || $result[0] != $value) {
|
|
|
|
|
throw new RequestException('The request param :attribute format error', 4001);
|
|
|
|
|
}
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-23 16:14:05 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string|null $default
|
|
|
|
|
* @return string|null
|
|
|
|
|
* @throws RequestException
|
|
|
|
|
*/
|
|
|
|
|
public function ip(string $name, string $default = NULL): string|null
|
|
|
|
|
{
|
|
|
|
|
$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
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function __get($name): mixed
|
|
|
|
|
{
|
|
|
|
|
$load = $this->load();
|
|
|
|
|
|
|
|
|
|
return $load[$name] ?? null;
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
}
|