Files
hdk-core/src/Service/ExtendService.php

60 lines
1.3 KiB
PHP

<?php
namespace Singularity\HDK\Core\Service;
use Hyperf\Context\Context;
use Psr\Http\Message\ServerRequestInterface;
/**
* 资源扩展
*/
class ExtendService
{
public function __construct(private UtilsService $utils)
{
}
/**
* @param ServerRequestInterface|null $request
* @param array<string, string>|null $params
* @return string[]
*/
public function parse(
?ServerRequestInterface $request,
array|string|null $params = null
): array {
$params ??= $request?->getQueryParams();
$extends = explode(',', $params['extends'] ?? '');
$extends = array_map('trim', $extends);
return Context::set(self::class, array_filter($extends));
}
/**
* @return array<string, string>
*/
public function getExtends(): array
{
return Context::get(self::class) ?? [];
}
public function hasExtends(): bool
{
$extends = Context::get(self::class);
return is_array($extends) && count($extends) > 0;
}
/**
* 判断是否传入了此扩展
*
* @param string $field
*
* @return bool
*/
public function hasExtend(string $field): bool
{
return $this->utils->inArray($field, Context::get(self::class) ?? []);
}
}