From 20a05b482a5ae5761b45f695def7c98f31860503 Mon Sep 17 00:00:00 2001 From: EverForge <113529280+BadJacky@users.noreply.github.com> Date: Mon, 17 Jun 2024 17:52:27 +0800 Subject: [PATCH] Update some statments (#93) --- src/AppVerify.php | 19 +++++++++++---- src/ConsoleTable.php | 10 ++++---- src/Id.php | 20 +++++++-------- src/LoginUser.php | 8 +++--- src/Str.php | 58 ++++++++++++++++---------------------------- 5 files changed, 54 insertions(+), 61 deletions(-) diff --git a/src/AppVerify.php b/src/AppVerify.php index c7e2156..0bdac43 100644 --- a/src/AppVerify.php +++ b/src/AppVerify.php @@ -30,9 +30,9 @@ class AppVerify */ public function __construct(string $scene = 'api') { - /* @var JWT $this->jwt */ - $this->jwt = make(JWT::class)->setScene($scene); - $this->request = make(RequestInterface::class); + /* @var JWT $this ->jwt */ + $this->jwt = static::getJwtGivenScene($scene); + $this->request = static::getRequest(); } /** @@ -74,7 +74,7 @@ public function getAppInfo(): array */ public function getApiId(): string { - $accessToken = $this->request->getQueryParams()['access_token'] ?? null; + $accessToken = $this->request->query('access_token') ?? null; return (string) $this->jwt->getParserData($accessToken)['id']; } @@ -98,11 +98,20 @@ public function getToken(array $apiInfo): string /** * 刷新token. - * @throws InvalidArgumentException */ public function refresh(): string { $accessToken = $this->request->getQueryParams()['access_token'] ?? null; return $this->jwt->refreshToken($accessToken); } + + protected static function getRequest(): RequestInterface + { + return make(RequestInterface::class); + } + + protected static function getJwtGivenScene(string $scene): JWT + { + return make(JWT::class)->setScene($scene); + } } diff --git a/src/ConsoleTable.php b/src/ConsoleTable.php index 9715538..597e7a0 100644 --- a/src/ConsoleTable.php +++ b/src/ConsoleTable.php @@ -35,7 +35,7 @@ class ConsoleTable protected array $header = []; /** - * 头部对齐方式 默认1 ALGIN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER. + * 头部对齐方式 默认1 ALIGN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER. */ protected int $headerAlign = 1; @@ -45,7 +45,7 @@ class ConsoleTable protected array $rows = []; /** - * 单元格对齐方式 默认1 ALGIN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER. + * 单元格对齐方式 默认1 ALIGN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER. */ protected int $cellAlign = 1; @@ -109,7 +109,7 @@ class ConsoleTable /** * 设置表格头信息 以及对齐方式. * @param array $header 要输出的Header信息 - * @param int $align 对齐方式 默认1 ALGIN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER + * @param int $align 对齐方式 默认1 ALIGN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER */ public function setHeader(array $header, int $align = 1): void { @@ -122,7 +122,7 @@ public function setHeader(array $header, int $align = 1): void /** * 设置输出表格数据 及对齐方式. * @param array $rows 要输出的表格数据(二维数组) - * @param int $align 对齐方式 默认1 ALGIN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER + * @param int $align 对齐方式 默认1 ALIGN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER */ public function setRows(array $rows, int $align = 1): void { @@ -136,7 +136,7 @@ public function setRows(array $rows, int $align = 1): void /** * 设置全局单元格对齐方式. - * @param int $align 对齐方式 默认1 ALGIN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER + * @param int $align 对齐方式 默认1 ALIGN_LEFT 0 ALIGN_RIGHT 2 ALIGN_CENTER * @return $this */ public function setCellAlign(int $align = 1) diff --git a/src/Id.php b/src/Id.php index 4fcff7c..1ca5275 100644 --- a/src/Id.php +++ b/src/Id.php @@ -22,25 +22,25 @@ class Id public const SEQUENCE_BITS = 5; // 毫秒内自增位 - private $workerId; // 工作机器ID + private int $workerId; // 工作机器ID - private $datacenterId; // 数据中心ID + private int $datacenterId; // 数据中心ID - private $sequence; // 毫秒内序列 + private int $sequence; // 毫秒内序列 - private $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 机器ID最大值 + private int $maxWorkerId = -1 ^ (-1 << self::WORKER_ID_BITS); // 机器ID最大值 - private $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 数据中心ID最大值 + private int $maxDatacenterId = -1 ^ (-1 << self::DATACENTER_ID_BITS); // 数据中心ID最大值 - private $workerIdShift = self::SEQUENCE_BITS; // 机器ID偏左移位数 + private int $workerIdShift = self::SEQUENCE_BITS; // 机器ID偏左移位数 - private $datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 数据中心ID左移位数 + private int $datacenterIdShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS; // 数据中心ID左移位数 - private $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 时间毫秒左移位数 + private int $timestampLeftShift = self::SEQUENCE_BITS + self::WORKER_ID_BITS + self::DATACENTER_ID_BITS; // 时间毫秒左移位数 - private $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩码 + private int $sequenceMask = -1 ^ (-1 << self::SEQUENCE_BITS); // 生成序列的掩码 - private $lastTimestamp = -1; // 上次生产id时间戳 + private int $lastTimestamp = -1; // 上次生产id时间戳 public function __construct($workerId = 1, $datacenterId = 1, $sequence = 0) { diff --git a/src/LoginUser.php b/src/LoginUser.php index 620c261..aede4fa 100644 --- a/src/LoginUser.php +++ b/src/LoginUser.php @@ -36,7 +36,7 @@ class LoginUser */ public function __construct(string $scene = 'default') { - /* @var JWT $this->jwt */ + /* @var JWT $this ->jwt */ $this->jwt = make(JWT::class)->setScene($scene); } @@ -139,9 +139,10 @@ public function isSuperAdmin(): bool */ public function isAdminRole(): bool { + $container = container(); return in_array( - container()->get(RoleServiceInterface::class)->read((int) env('ADMIN_ROLE'), ['code'])->code, - container()->get(UserServiceInterface::class)->getInfo()['roles'] + $container->get(RoleServiceInterface::class)->read((int) env('ADMIN_ROLE'), ['code'])->code, + $container->get(UserServiceInterface::class)->getInfo()['roles'] ); } @@ -156,7 +157,6 @@ public function getToken(array $user): string /** * 刷新token. - * @throws InvalidArgumentException */ public function refresh(): string { diff --git a/src/Str.php b/src/Str.php index ca8019c..62bdc87 100644 --- a/src/Str.php +++ b/src/Str.php @@ -25,17 +25,16 @@ class Str { - protected static $snakeCache = []; + protected static array $snakeCache = []; - protected static $camelCache = []; + protected static array $camelCache = []; - protected static $studlyCache = []; + protected static array $studlyCache = []; /** - * 检查字符串中是否包含某些字符串. - * @param array|string $needles + * @description 检查字符串中是否包含某些字符串. */ - public static function contains(string $haystack, $needles): bool + public static function contains(string $haystack, array|string $needles): bool { foreach ((array) $needles as $needle) { if ($needle != '' && mb_strpos($haystack, $needle) !== false) { @@ -47,11 +46,9 @@ public static function contains(string $haystack, $needles): bool } /** - * 检查字符串是否以某些字符串结尾. - * - * @param array|string $needles + * @description 检查字符串是否以某些字符串结尾. */ - public static function endsWith(string $haystack, $needles): bool + public static function endsWith(string $haystack, array|string $needles): bool { foreach ((array) $needles as $needle) { if ((string) $needle === static::substr($haystack, -static::length($needle))) { @@ -63,11 +60,9 @@ public static function endsWith(string $haystack, $needles): bool } /** - * 检查字符串是否以某些字符串开头. - * - * @param array|string $needles + * @description 检查字符串是否以某些字符串开头. */ - public static function startsWith(string $haystack, $needles): bool + public static function startsWith(string $haystack, array|string $needles): bool { foreach ((array) $needles as $needle) { if ($needle != '' && mb_strpos($haystack, $needle) === 0) { @@ -84,26 +79,14 @@ public static function startsWith(string $haystack, $needles): bool public static function random(int $length = 6, int $type = -1, string $addChars = ''): string { $str = ''; - switch ($type) { - case 0: - $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars; - break; - case 1: - $chars = str_repeat('0123456789', 3); - break; - case 2: - $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars; - break; - case 3: - $chars = 'abcdefghijklmnopqrstuvwxyz' . $addChars; - break; - case 4: - $chars = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书' . $addChars; - break; - default: - $chars = 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars; - break; - } + $chars = match ($type) { + 0 => 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' . $addChars, + 1 => str_repeat('0123456789', 3), + 2 => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' . $addChars, + 3 => 'abcdefghijklmnopqrstuvwxyz' . $addChars, + 4 => '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这主中人上为来分生对于学下级地个用同行面说种过命度革而多子后自社加小机也经力线本电高量长党得实家定深法表着水理化争现所二起政三好十战无农使性前等反体合斗路图把结第里正新开论之物从当两些还天资事队批点育重其思与间内去因件日利相由压员气业代全组数果期导平各基或月毛然如应形想制心样干都向变关问比展那它最及外没看治提五解系林者米群头意只明四道马认次文通但条较克又公孔领军流入接席位情运器并飞原油放立题质指建区验活众很教决特此常石强极土少已根共直团统式转别造切九你取西持总料连任志观调七么山程百报更见必真保热委手改管处己将修支识病象几先老光专什六型具示复安带每东增则完风回南广劳轮科北打积车计给节做务被整联步类集号列温装即毫知轴研单色坚据速防史拉世设达尔场织历花受求传口断况采精金界品判参层止边清至万确究书' . $addChars, + default => 'ABCDEFGHIJKMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789' . $addChars, + }; if ($length > 10) { $chars = $type == 1 ? str_repeat($chars, $length) : str_repeat($chars, 5); } @@ -219,12 +202,13 @@ public static function ipToRegion(string $ip): string /** * 秒数转时分格式. - * @param $time int */ public static function Sec2Time(int $time): string { $value = [ - 'years' => 0, 'days' => 0, 'hours' => 0, + 'years' => 0, + 'days' => 0, + 'hours' => 0, 'minutes' => 0, 'seconds' => 0, ]; @@ -268,7 +252,7 @@ public static function getUUID(): string } /** - * Replace the first occurrence of a given value in the string. + * @description Replace the first occurrence of a given value in the string. */ public static function replaceFirst(string $search, string $replace, string $subject, int &$offset = 0): string {