forked from kvnZero/hyperf-APIJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractHandle.php
More file actions
77 lines (65 loc) · 2.34 KB
/
AbstractHandle.php
File metadata and controls
77 lines (65 loc) · 2.34 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
70
71
72
73
74
75
76
77
<?php
namespace App\ApiJson\Handle;
use App\ApiJson\Entity\ConditionEntity;
use App\ApiJson\Entity\TableEntity;
use App\ApiJson\Interface\QueryInterface;
use App\Event\ApiJson\QueryHandleAfter;
use App\Event\ApiJson\QueryHandleBefore;
use Hyperf\Utils\ApplicationContext;
use Psr\EventDispatcher\EventDispatcherInterface;
use App\ApiJson\Parse\Handle;
use Hyperf\Contract\ConfigInterface;
abstract class AbstractHandle
{
/** @var string 关键字 */
protected string $keyWord;
protected array $unsetKey = [];
public function __construct(protected ConditionEntity $condition)
{
}
protected function sanitizeKey(string $key): string
{
preg_match('#(?<key>[a-zA-z0-9_]+)#', $key, $match);
return $match['key'] ?? $key;
}
public function handle()
{
$this->handleBefore();
$this->buildModel();
$this->unsetKeySaveCondition();
$this->handleAfter();
}
protected function unsetKeySaveCondition()
{
if (empty($this->unsetKey)) return;
$condition = $this->condition->getCondition();
foreach ($this->unsetKey as $key) {
unset($condition[$key]);
}
$this->condition->setCondition($condition);
}
protected function handleBefore()
{
$event = new QueryHandleBefore($this->condition);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
$this->condition = $event->condition;
}
protected function handleAfter()
{
$event = new QueryHandleAfter($this->condition);
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);
$this->condition = $event->condition;
}
protected function subTableQuery(array $data): QueryInterface
{
$tableName = $data['from'];
$tableEntity = new TableEntity($tableName, $data);
$conditionEntity = $tableEntity->getConditionEntity();
$conditionEntity->setLimit(0);
$handle = new Handle($conditionEntity, $tableEntity);
$handle->buildQuery();
/** @var QueryInterface $query */
return new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName(), $tableEntity->getConditionEntity());
}
abstract protected function buildModel();
}