forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractMethod.php
More file actions
69 lines (55 loc) · 1.83 KB
/
AbstractMethod.php
File metadata and controls
69 lines (55 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace App\ApiJson\Method;
use App\ApiJson\Entity\TableEntity;
use App\ApiJson\Interface\QueryInterface;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Utils\ApplicationContext;
abstract class AbstractMethod
{
/** @var QueryInterface $query */
protected QueryInterface $query;
/** @var bool $isQueryMany */
protected bool $isQueryMany = false;
/** @var bool $arrayQuery */
protected bool $arrayQuery = false;
public function __construct(protected TableEntity $tableEntity, protected string $method = 'GET')
{
$this->buildQuery();
$this->isQueryMany = str_ends_with($this->tableEntity->getTableName(), '[]');
}
public function handle(): ?array
{
if (!$this->validateCondition()) return null;
return $this->process();
}
protected function buildQuery()
{
$this->query = new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($this->tableEntity->getRealTableName(), $this->tableEntity->getConditionEntity());
}
protected function parseManyResponse(array $ids, bool $isQueryMany = false): array
{
if ($isQueryMany) {
$response = [
'id[]' => $ids,
'count' => count($ids)
];
} else {
$response['id'] = current($ids) ?: 0;
}
return $response;
}
public function setQueryMany(bool $isQueryMany = false)
{
$this->isQueryMany = $isQueryMany;
}
public function setArrayQuery(bool $arrayQuery = false)
{
$this->arrayQuery = $arrayQuery;
}
protected function isQueryMany(): bool
{
return $this->isQueryMany; //可能有不止一个因素影响
}
abstract protected function validateCondition(): bool;
abstract protected function process();
}