fix:更新已知bug,优化代码
This commit is contained in:
176
vendor/yansongda/supports/src/Collection.php
vendored
176
vendor/yansongda/supports/src/Collection.php
vendored
@@ -9,16 +9,20 @@ use ArrayIterator;
|
||||
use Countable;
|
||||
use IteratorAggregate;
|
||||
use JsonSerializable;
|
||||
use Serializable;
|
||||
use Yansongda\Supports\Traits\Accessable;
|
||||
use Yansongda\Supports\Traits\Arrayable;
|
||||
use Yansongda\Supports\Traits\Serializable;
|
||||
|
||||
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable, Serializable
|
||||
class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
|
||||
{
|
||||
use Serializable;
|
||||
use Accessable;
|
||||
use Arrayable;
|
||||
|
||||
/**
|
||||
* The collection data.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $items = [];
|
||||
protected array $items = [];
|
||||
|
||||
/**
|
||||
* set data.
|
||||
@@ -32,50 +36,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* To string.
|
||||
*/
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a data by key.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $key)
|
||||
{
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Assigns a value to the specified data.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function __set(string $key, $value)
|
||||
{
|
||||
$this->set($key, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not an data exists by key.
|
||||
*/
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return $this->has($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsets an data by key.
|
||||
*/
|
||||
public function __unset(string $key)
|
||||
{
|
||||
$this->forget($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the given value in a collection if applicable.
|
||||
*
|
||||
@@ -126,10 +86,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* Get all items except for those with the specified keys.
|
||||
*
|
||||
* @param mixed $keys
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function except($keys): Collection
|
||||
public function except($keys): self
|
||||
{
|
||||
$keys = is_array($keys) ? $keys : func_get_args();
|
||||
|
||||
@@ -198,7 +156,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* @param string|int|null $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function add($key, $value)
|
||||
public function add($key, $value): void
|
||||
{
|
||||
Arr::set($this->items, $key, $value);
|
||||
}
|
||||
@@ -209,7 +167,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* @param string|int|null $key
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set($key, $value)
|
||||
public function set($key, $value): void
|
||||
{
|
||||
Arr::set($this->items, $key, $value);
|
||||
}
|
||||
@@ -232,7 +190,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
*
|
||||
* @param string|int $key
|
||||
*/
|
||||
public function forget($key)
|
||||
public function forget($key): void
|
||||
{
|
||||
Arr::forget($this->items, $key);
|
||||
}
|
||||
@@ -260,6 +218,8 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
|
||||
/**
|
||||
* Get and remove the last item from the collection.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function pop()
|
||||
{
|
||||
@@ -294,8 +254,10 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
/**
|
||||
* Get and remove an item from the collection.
|
||||
*
|
||||
* @param mixed|null $default
|
||||
* @param mixed $key
|
||||
* @param mixed $default
|
||||
* @param mixed $key
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function pull($key, $default = null)
|
||||
{
|
||||
@@ -469,33 +431,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
return json_encode($this->all(), $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.4.0)<br/>
|
||||
* Specify data which should be serialized to JSON.
|
||||
*
|
||||
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->items;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.1.0)<br/>
|
||||
* String representation of object.
|
||||
*
|
||||
* @see http://php.net/manual/en/serializable.serialize.php
|
||||
*
|
||||
* @return string the string representation of the object or null
|
||||
*/
|
||||
public function serialize(): string
|
||||
{
|
||||
return serialize($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Retrieve an external iterator.
|
||||
@@ -505,6 +440,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* @return ArrayIterator An instance of an object implementing <b>Iterator</b> or
|
||||
* <b>ArrayIterator</b>
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function getIterator()
|
||||
{
|
||||
return new ArrayIterator($this->items);
|
||||
@@ -521,46 +457,12 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* <p>
|
||||
* The return value is cast to an integer
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function count()
|
||||
{
|
||||
return count($this->items);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.1.0)<br/>
|
||||
* Constructs the object.
|
||||
*
|
||||
* @see http://php.net/manual/en/serializable.unserialize.php
|
||||
*
|
||||
* @param string $serialized <p>
|
||||
* The string representation of the object.
|
||||
* </p>
|
||||
*
|
||||
* @return mixed|void
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
return $this->items = unserialize($serialized);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Whether a offset exists.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetexists.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* An offset to check for.
|
||||
* </p>
|
||||
*
|
||||
* @return bool true on success or false on failure.
|
||||
* The return value will be casted to boolean if non-boolean was returned
|
||||
*/
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return $this->has($offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Offset to unset.
|
||||
@@ -571,6 +473,7 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
* The offset to unset.
|
||||
* </p>
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
@@ -578,41 +481,6 @@ class Collection implements ArrayAccess, Countable, IteratorAggregate, JsonSeria
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Offset to retrieve.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetget.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to retrieve.
|
||||
* </p>
|
||||
*
|
||||
* @return mixed Can return all value types
|
||||
*/
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->offsetExists($offset) ? $this->get($offset) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* (PHP 5 >= 5.0.0)<br/>
|
||||
* Offset to set.
|
||||
*
|
||||
* @see http://php.net/manual/en/arrayaccess.offsetset.php
|
||||
*
|
||||
* @param mixed $offset <p>
|
||||
* The offset to assign the value to.
|
||||
* </p>
|
||||
* @param mixed $value <p>
|
||||
* The value to set.
|
||||
* </p>
|
||||
*/
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->set($offset, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the given value is callable, but not a string.
|
||||
*
|
||||
|
||||
5
vendor/yansongda/supports/src/Functions.php
vendored
5
vendor/yansongda/supports/src/Functions.php
vendored
@@ -2,8 +2,9 @@
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
use Yansongda\Supports\Arr;
|
||||
use Yansongda\Supports\Collection;
|
||||
namespace Yansongda\Supports;
|
||||
|
||||
use Closure;
|
||||
|
||||
if (!function_exists('collect')) {
|
||||
/**
|
||||
|
||||
46
vendor/yansongda/supports/src/Logger.php
vendored
46
vendor/yansongda/supports/src/Logger.php
vendored
@@ -26,33 +26,13 @@ use Psr\Log\LoggerInterface;
|
||||
*/
|
||||
class Logger
|
||||
{
|
||||
/**
|
||||
* Logger instance.
|
||||
*
|
||||
* @var \Psr\Log\LoggerInterface|null
|
||||
*/
|
||||
protected $logger;
|
||||
protected ?LoggerInterface $logger = null;
|
||||
|
||||
/**
|
||||
* formatter.
|
||||
*
|
||||
* @var \Monolog\Formatter\FormatterInterface|null
|
||||
*/
|
||||
protected $formatter;
|
||||
protected ?FormatterInterface $formatter = null;
|
||||
|
||||
/**
|
||||
* handler.
|
||||
*
|
||||
* @var \Monolog\Handler\AbstractProcessingHandler|null
|
||||
*/
|
||||
protected $handler;
|
||||
protected ?AbstractProcessingHandler $handler = null;
|
||||
|
||||
/**
|
||||
* config.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $config = [
|
||||
protected array $config = [
|
||||
'file' => null,
|
||||
'identify' => 'yansongda.supports',
|
||||
'level' => BaseLogger::DEBUG,
|
||||
@@ -95,11 +75,7 @@ class Logger
|
||||
*/
|
||||
public function getLogger(): LoggerInterface
|
||||
{
|
||||
if (is_null($this->logger)) {
|
||||
$this->logger = $this->createLogger();
|
||||
}
|
||||
|
||||
return $this->logger;
|
||||
return $this->logger ??= $this->createLogger();
|
||||
}
|
||||
|
||||
public function createLogger(): BaseLogger
|
||||
@@ -132,11 +108,7 @@ class Logger
|
||||
*/
|
||||
public function getFormatter(): FormatterInterface
|
||||
{
|
||||
if (is_null($this->formatter)) {
|
||||
$this->formatter = $this->createFormatter();
|
||||
}
|
||||
|
||||
return $this->formatter;
|
||||
return $this->formatter ??= $this->createFormatter();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,11 +138,7 @@ class Logger
|
||||
|
||||
public function getHandler(): AbstractProcessingHandler
|
||||
{
|
||||
if (is_null($this->handler)) {
|
||||
$this->handler = $this->createHandler();
|
||||
}
|
||||
|
||||
return $this->handler;
|
||||
return $this->handler ??= $this->createHandler();
|
||||
}
|
||||
|
||||
public function createHandler(): AbstractProcessingHandler
|
||||
|
||||
12
vendor/yansongda/supports/src/Pipeline.php
vendored
12
vendor/yansongda/supports/src/Pipeline.php
vendored
@@ -15,10 +15,8 @@ class Pipeline
|
||||
{
|
||||
/**
|
||||
* The container implementation.
|
||||
*
|
||||
* @var \Psr\Container\ContainerInterface
|
||||
*/
|
||||
protected $container;
|
||||
protected ContainerInterface $container;
|
||||
|
||||
/**
|
||||
* The object being passed through the pipeline.
|
||||
@@ -29,17 +27,13 @@ class Pipeline
|
||||
|
||||
/**
|
||||
* The array of class pipes.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $pipes = [];
|
||||
protected array $pipes = [];
|
||||
|
||||
/**
|
||||
* The method to call on each pipe.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $method = 'handle';
|
||||
protected string $method = 'handle';
|
||||
|
||||
public function __construct(ContainerInterface $container)
|
||||
{
|
||||
|
||||
36
vendor/yansongda/supports/src/Str.php
vendored
36
vendor/yansongda/supports/src/Str.php
vendored
@@ -14,24 +14,18 @@ class Str
|
||||
{
|
||||
/**
|
||||
* The cache of snake-cased words.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $snakeCache = [];
|
||||
protected static array $snakeCache = [];
|
||||
|
||||
/**
|
||||
* The cache of camel-cased words.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $camelCache = [];
|
||||
protected static array $camelCache = [];
|
||||
|
||||
/**
|
||||
* The cache of studly-cased words.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected static $studlyCache = [];
|
||||
protected static array $studlyCache = [];
|
||||
|
||||
/**
|
||||
* Return the remainder of a string after a given value.
|
||||
@@ -239,6 +233,30 @@ class Str
|
||||
return $string;
|
||||
}
|
||||
|
||||
public static function uuidV4(): string
|
||||
{
|
||||
return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
|
||||
|
||||
// 32 bits for "time_low"
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
|
||||
|
||||
// 16 bits for "time_mid"
|
||||
mt_rand(0, 0xffff),
|
||||
|
||||
// 16 bits for "time_hi_and_version",
|
||||
// four most significant bits holds version number 4
|
||||
mt_rand(0, 0x0fff) | 0x4000,
|
||||
|
||||
// 16 bits, 8 bits for "clk_seq_hi_res",
|
||||
// 8 bits for "clk_seq_low",
|
||||
// two most significant bits holds zero and one for variant DCE1.1
|
||||
mt_rand(0, 0x3fff) | 0x8000,
|
||||
|
||||
// 48 bits for "node"
|
||||
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace a given value in the string sequentially with an array.
|
||||
*/
|
||||
|
||||
@@ -18,6 +18,22 @@ trait Accessable
|
||||
return $this->get($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not an data exists by key.
|
||||
*/
|
||||
public function __isset(string $key): bool
|
||||
{
|
||||
return !is_null($this->get($key));
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsets an data by key.
|
||||
*/
|
||||
public function __unset(string $key)
|
||||
{
|
||||
$this->offsetUnset($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* __set.
|
||||
*
|
||||
@@ -77,6 +93,7 @@ trait Accessable
|
||||
*
|
||||
* The return value will be casted to boolean if non-boolean was returned.
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetExists($offset)
|
||||
{
|
||||
return !is_null($this->get($offset));
|
||||
@@ -91,6 +108,7 @@ trait Accessable
|
||||
*
|
||||
* @return mixed can return all value types
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetGet($offset)
|
||||
{
|
||||
return $this->get($offset);
|
||||
@@ -106,6 +124,7 @@ trait Accessable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetSet($offset, $value)
|
||||
{
|
||||
$this->set($offset, $value);
|
||||
@@ -120,6 +139,7 @@ trait Accessable
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function offsetUnset($offset)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -1,207 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* Trait HasHttpRequest.
|
||||
*
|
||||
* @property string $baseUri
|
||||
* @property float $timeout
|
||||
* @property float $connectTimeout
|
||||
*/
|
||||
trait HasHttpRequest
|
||||
{
|
||||
/**
|
||||
* Http client.
|
||||
*
|
||||
* @var Client|null
|
||||
*/
|
||||
protected $httpClient = null;
|
||||
|
||||
/**
|
||||
* Http client options.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $httpOptions = [];
|
||||
|
||||
/**
|
||||
* Send a GET request.
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function get(string $endpoint, array $query = [], array $headers = [])
|
||||
{
|
||||
return $this->request('get', $endpoint, [
|
||||
'headers' => $headers,
|
||||
'query' => $query,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a POST request.
|
||||
*
|
||||
* @param string|array $data
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function post(string $endpoint, $data, array $options = [])
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
$options['body'] = $data;
|
||||
} else {
|
||||
$options['form_params'] = $data;
|
||||
}
|
||||
|
||||
return $this->request('post', $endpoint, $options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send request.
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function request(string $method, string $endpoint, array $options = [])
|
||||
{
|
||||
return $this->unwrapResponse($this->getHttpClient()->{$method}($endpoint, $options));
|
||||
}
|
||||
|
||||
/**
|
||||
* Set http client.
|
||||
*/
|
||||
public function setHttpClient(Client $client): self
|
||||
{
|
||||
$this->httpClient = $client;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return http client.
|
||||
*/
|
||||
public function getHttpClient(): Client
|
||||
{
|
||||
if (is_null($this->httpClient)) {
|
||||
$this->httpClient = $this->getDefaultHttpClient();
|
||||
}
|
||||
|
||||
return $this->httpClient;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default http client.
|
||||
*/
|
||||
public function getDefaultHttpClient(): Client
|
||||
{
|
||||
return new Client($this->getOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* setBaseUri.
|
||||
*/
|
||||
public function setBaseUri(string $url): self
|
||||
{
|
||||
if (property_exists($this, 'baseUri')) {
|
||||
$parsedUrl = parse_url($url);
|
||||
|
||||
$this->baseUri = ($parsedUrl['scheme'] ?? 'http').'://'.
|
||||
$parsedUrl['host'].(isset($parsedUrl['port']) ? (':'.$parsedUrl['port']) : '');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* getBaseUri.
|
||||
*/
|
||||
public function getBaseUri(): string
|
||||
{
|
||||
return property_exists($this, 'baseUri') ? $this->baseUri : '';
|
||||
}
|
||||
|
||||
public function getTimeout(): float
|
||||
{
|
||||
return property_exists($this, 'timeout') ? $this->timeout : 5.0;
|
||||
}
|
||||
|
||||
public function setTimeout(float $timeout): self
|
||||
{
|
||||
if (property_exists($this, 'timeout')) {
|
||||
$this->timeout = $timeout;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getConnectTimeout(): float
|
||||
{
|
||||
return property_exists($this, 'connectTimeout') ? $this->connectTimeout : 3.0;
|
||||
}
|
||||
|
||||
public function setConnectTimeout(float $connectTimeout): self
|
||||
{
|
||||
if (property_exists($this, 'connectTimeout')) {
|
||||
$this->connectTimeout = $connectTimeout;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get default options.
|
||||
*/
|
||||
public function getOptions(): array
|
||||
{
|
||||
return array_merge([
|
||||
'base_uri' => $this->getBaseUri(),
|
||||
'timeout' => $this->getTimeout(),
|
||||
'connect_timeout' => $this->getConnectTimeout(),
|
||||
], $this->getHttpOptions());
|
||||
}
|
||||
|
||||
/**
|
||||
* setOptions.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options): self
|
||||
{
|
||||
return $this->setHttpOptions($options);
|
||||
}
|
||||
|
||||
public function getHttpOptions(): array
|
||||
{
|
||||
return $this->httpOptions;
|
||||
}
|
||||
|
||||
public function setHttpOptions(array $httpOptions): self
|
||||
{
|
||||
$this->httpOptions = $httpOptions;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert response.
|
||||
*
|
||||
* @return array|string
|
||||
*/
|
||||
public function unwrapResponse(ResponseInterface $response)
|
||||
{
|
||||
$contentType = $response->getHeaderLine('Content-Type');
|
||||
$contents = $response->getBody()->getContents();
|
||||
|
||||
if (false !== stripos($contentType, 'json') || stripos($contentType, 'javascript')) {
|
||||
return json_decode($contents, true);
|
||||
} elseif (false !== stripos($contentType, 'xml')) {
|
||||
return json_decode(json_encode(simplexml_load_string($contents, 'SimpleXMLElement', LIBXML_NOCDATA), JSON_UNESCAPED_UNICODE), true);
|
||||
}
|
||||
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
@@ -4,29 +4,9 @@ declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
trait Serializable
|
||||
{
|
||||
/**
|
||||
* toJson.
|
||||
*/
|
||||
public function toJson(): string
|
||||
{
|
||||
return $this->serialize();
|
||||
}
|
||||
|
||||
/**
|
||||
* Specify data which should be serialized to JSON.
|
||||
*
|
||||
* @see https://php.net/manual/en/jsonserializable.jsonserialize.php
|
||||
*
|
||||
* @return mixed data which can be serialized by <b>json_encode</b>,
|
||||
* which is a value of any type other than a resource
|
||||
*
|
||||
* @since 5.4.0
|
||||
*/
|
||||
public function jsonSerialize()
|
||||
public function __serialize(): array
|
||||
{
|
||||
if (method_exists($this, 'toArray')) {
|
||||
return $this->toArray();
|
||||
@@ -35,52 +15,51 @@ trait Serializable
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* String representation of object.
|
||||
*
|
||||
* @see https://php.net/manual/en/serializable.serialize.php
|
||||
*
|
||||
* @return string the string representation of the object or null
|
||||
*
|
||||
* @since 5.1.0
|
||||
*/
|
||||
public function serialize()
|
||||
public function __unserialize(array $data): void
|
||||
{
|
||||
if (method_exists($this, 'toArray')) {
|
||||
return json_encode($this->toArray());
|
||||
}
|
||||
|
||||
return json_encode([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs the object.
|
||||
*
|
||||
* @see https://php.net/manual/en/serializable.unserialize.php
|
||||
*
|
||||
* @param string $serialized <p>
|
||||
* The string representation of the object.
|
||||
* </p>
|
||||
*
|
||||
* @since 5.1.0
|
||||
*/
|
||||
public function unserialize($serialized)
|
||||
{
|
||||
$data = json_decode($serialized, true);
|
||||
|
||||
if (JSON_ERROR_NONE !== json_last_error()) {
|
||||
throw new RuntimeException('Invalid Json Format');
|
||||
}
|
||||
|
||||
$this->unserializeArray($data);
|
||||
}
|
||||
|
||||
public function unserializeArray(array $data): void
|
||||
public function __toString(): string
|
||||
{
|
||||
return $this->toJson();
|
||||
}
|
||||
|
||||
public function serialize(): ?string
|
||||
{
|
||||
return serialize($this);
|
||||
}
|
||||
|
||||
public function unserialize($data): void
|
||||
{
|
||||
unserialize($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* toJson.
|
||||
*/
|
||||
public function toJson(int $option = JSON_UNESCAPED_UNICODE): string
|
||||
{
|
||||
return json_encode($this->__serialize(), $option);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
#[\ReturnTypeWillChange]
|
||||
public function jsonSerialize()
|
||||
{
|
||||
return $this->__serialize();
|
||||
}
|
||||
|
||||
public function unserializeArray(array $data): self
|
||||
{
|
||||
foreach ($data as $key => $item) {
|
||||
if (method_exists($this, 'set')) {
|
||||
$this->set($key, $item);
|
||||
}
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,116 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Yansongda\Supports\Traits;
|
||||
|
||||
/**
|
||||
* Trait ShouldThrottle.
|
||||
*
|
||||
* @property \Redis $redis
|
||||
*/
|
||||
trait ShouldThrottle
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $throttle = [
|
||||
'limit' => 60,
|
||||
'period' => 60,
|
||||
'count' => 0,
|
||||
'reset_time' => 0,
|
||||
];
|
||||
|
||||
/**
|
||||
* isThrottled.
|
||||
*/
|
||||
public function isThrottled(string $key, int $limit = 60, int $period = 60, bool $autoAdd = false): bool
|
||||
{
|
||||
if (-1 === $limit) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$now = microtime(true) * 1000;
|
||||
|
||||
$this->redis->zRemRangeByScore($key, 0, $now - $period * 1000);
|
||||
|
||||
$this->throttle = [
|
||||
'limit' => $limit,
|
||||
'period' => $period,
|
||||
'count' => $this->getThrottleCounts($key, $period),
|
||||
'reset_time' => $this->getThrottleResetTime($key, $now),
|
||||
];
|
||||
|
||||
if ($this->throttle['count'] < $limit) {
|
||||
if ($autoAdd) {
|
||||
$this->throttleAdd($key, $period);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 限流 + 1.
|
||||
*/
|
||||
public function throttleAdd(string $key, int $period = 60): void
|
||||
{
|
||||
$now = microtime(true) * 1000;
|
||||
|
||||
$this->redis->zAdd($key, $now, $now);
|
||||
$this->redis->expire($key, $period * 2);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取下次重置时间.
|
||||
*
|
||||
* @param float $now 现在的毫秒时间
|
||||
*/
|
||||
public function getThrottleResetTime(string $key, float $now): int
|
||||
{
|
||||
$data = $this->redis->zRangeByScore(
|
||||
$key,
|
||||
$now - $this->throttle['period'] * 1000,
|
||||
$now,
|
||||
['limit' => [0, 1]]
|
||||
);
|
||||
|
||||
if (0 === count($data)) {
|
||||
return $this->throttle['reset_time'] = time() + $this->throttle['period'];
|
||||
}
|
||||
|
||||
return intval(reset($data) / 1000) + $this->throttle['period'];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取限流相关信息.
|
||||
*
|
||||
* @param mixed $default
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function getThrottleInfo(?string $key = null, $default = null)
|
||||
{
|
||||
if (is_null($key)) {
|
||||
return $this->throttle;
|
||||
}
|
||||
|
||||
if (isset($this->throttle[$key])) {
|
||||
return $this->throttle[$key];
|
||||
}
|
||||
|
||||
return $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取已使用次数.
|
||||
*/
|
||||
public function getThrottleCounts(string $key, int $period = 60): int
|
||||
{
|
||||
$now = microtime(true) * 1000;
|
||||
|
||||
return $this->redis->zCount($key, $now - $period * 1000, $now);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user