feat: 增加了inArray、hasExtends方法

This commit is contained in:
李东云
2023-01-19 17:43:57 +08:00
parent 3e2d3400b4
commit c83728cad2
4 changed files with 62 additions and 10 deletions

View File

@@ -10,6 +10,13 @@ use Psr\Http\Message\ServerRequestInterface;
*/
class ExtendService
{
private UtilsService $utils;
public function __construct(UtilsService $utils)
{
$this->utils = $utils;
}
/**
* @param ServerRequestInterface|null $request
* @param array<string, string>|null $params
@@ -23,7 +30,9 @@ class ExtendService
$extends = $params['extends'] ?? null;
if (!empty($extends)) {
$extends = explode(',', $extends);
return Context::set(self::class, array_map('trim', $extends));
$extends = array_map('trim', $extends);
Context::set(self::class, $extends);
return $extends;
}
return [];
@@ -34,7 +43,13 @@ class ExtendService
*/
public function getExtends(): array
{
return Context::get(self::class) ?? [];
return Context::get(self::class);
}
public function hasExtends(): bool
{
$extends = Context::get(self::class);
return is_array($extends) && count($extends) > 0;
}
/**
@@ -44,8 +59,8 @@ class ExtendService
*
* @return bool
*/
public function hasExtends(string $field): bool
public function hasExtend(string $field): bool
{
return Context::has(self::class) && isset(array_flip(Context::get(self::class))[$field]);
return $this->utils->inArray($field, Context::get(self::class));
}
}

View File

@@ -329,4 +329,16 @@ class UtilsService
'unit' => $unitToUpper ? strtoupper($unit[$i]) : $unit[$i],
];
}
/**
* 更快的判断数组元素方法
* @param string|int|float $needle
* @param array $haystack
* @return bool
*/
public function inArray(mixed $needle, array $haystack)
{
$list = array_flip($haystack);
return isset($list[$needle]);
}
}

View File

@@ -9,10 +9,11 @@
namespace Singularity\HDK\Test\Core\Unit;
use Hyperf\Utils\ApplicationContext;
use Singularity\HDK\Core\Service\ExtendService;
use Singularity\HDK\Core\Service\UtilsService;
$service = new ExtendService();
$service = make(ExtendService::class, ['utils' => new UtilsService()]);
it('asserts query parameters can be parsed.', function () use ($service) {
$result = $service->parse(
null,
@@ -23,10 +24,15 @@ it('asserts query parameters can be parsed.', function () use ($service) {
);
expect($result)->toBeArray()->toHaveCount(2)->toBe(['a', 'b']);
});
it('asserts has extends', function () use ($service) {
expect($service->hasExtends('a'))->toBeTrue()
->and($service->hasExtends('b'))->toBeTrue()
->and($service->hasExtends('c'))->toBeFalse();
expect($service->hasExtends())->toBeTrue();
});
it('asserts has specify extend', function () use ($service) {
expect($service->hasExtend('a'))->toBeTrue()
->and($service->hasExtend('b'))->toBeTrue()
->and($service->hasExtend('c'))->toBeFalse();
})->depends('it asserts query parameters can be parsed.');
it('asserts parsed extends', function () use ($service) {

View File

@@ -58,5 +58,24 @@ test('断言可以根据参数构建 URL', function (string $url, array $params,
['c' => 'd'],
true,
'http://username:password@127.0.0.1/git/resp?id=1#/page?a=b&c=d',
]
],
])->group('pure', 'utils');
test('断言可以判断是否是数组中的元素', function (
mixed $needle,
array $haystack,
bool $expect,
bool $exceptions = false
) use ($utils) {
try {
expect($utils->inArray($needle, $haystack))->toBe($expect);
} catch (Throwable $e) {
if ($exceptions) {
expect($e::class)->toBe(TypeError::class);
}
}
})->with([
[1, ['1', 2, 3], true, false],
['1', ['1', 2, 3], true, false],
[[1, 2, 3], ['1', 2, 3], false, true],
])->group('pure', 'utils')->only();