Skip to content

Commit

Permalink
Update some statments (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
BadJacky authored Jun 17, 2024
1 parent c787af9 commit 20a05b4
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 61 deletions.
19 changes: 14 additions & 5 deletions src/AppVerify.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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'];
}

Expand All @@ -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);
}
}
10 changes: 5 additions & 5 deletions src/ConsoleTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -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
{
Expand All @@ -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
{
Expand All @@ -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)
Expand Down
20 changes: 10 additions & 10 deletions src/Id.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
8 changes: 4 additions & 4 deletions src/LoginUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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']
);
}

Expand All @@ -156,7 +157,6 @@ public function getToken(array $user): string

/**
* 刷新token.
* @throws InvalidArgumentException
*/
public function refresh(): string
{
Expand Down
58 changes: 21 additions & 37 deletions src/Str.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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))) {
Expand All @@ -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) {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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,
];
Expand Down Expand Up @@ -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
{
Expand Down

0 comments on commit 20a05b4

Please sign in to comment.