diff --git a/src/Service/ExtendService.php b/src/Service/ExtendService.php index 3c290f1..5a9c636 100644 --- a/src/Service/ExtendService.php +++ b/src/Service/ExtendService.php @@ -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|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)); } } diff --git a/src/Service/UtilsService.php b/src/Service/UtilsService.php index 161efc6..7298e2d 100644 --- a/src/Service/UtilsService.php +++ b/src/Service/UtilsService.php @@ -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]); + } } diff --git a/tests/Unit/ExtendServiceTest.php b/tests/Unit/ExtendServiceTest.php index 22f9aad..a864579 100644 --- a/tests/Unit/ExtendServiceTest.php +++ b/tests/Unit/ExtendServiceTest.php @@ -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) { diff --git a/tests/Unit/UtilsServiceTest.php b/tests/Unit/UtilsServiceTest.php index 599b582..90f4472 100644 --- a/tests/Unit/UtilsServiceTest.php +++ b/tests/Unit/UtilsServiceTest.php @@ -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();