Skip to content

Commit ba3a01a

Browse files
committed
update 修改语句解析方式 通过各自管理遍历的方式解析 为之后的操作符准备
1 parent 45ad7d9 commit ba3a01a

24 files changed

+333
-247
lines changed

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 19 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -2,61 +2,9 @@
22

33
namespace App\ApiJson\Entity;
44

5-
use App\ApiJson\Handle\FunctionLimitHandle;
6-
use App\ApiJson\Handle\FunctionOffsetHandle;
7-
use App\ApiJson\Interface\QueryInterface;
8-
use App\ApiJson\Handle\AbstractHandle;
9-
use App\ApiJson\Handle\FunctionColumnHandle;
10-
use App\ApiJson\Handle\FunctionGroupHandle;
11-
use App\ApiJson\Handle\FunctionHavingHandle;
12-
use App\ApiJson\Handle\FunctionOrderHandle;
13-
use App\ApiJson\Handle\WhereBetweenHandle;
14-
use App\ApiJson\Handle\WhereExistsHandle;
15-
use App\ApiJson\Handle\WhereHandle;
16-
use App\ApiJson\Handle\WhereInHandle;
17-
use App\ApiJson\Handle\WhereJsonContainsHandle;
18-
use App\ApiJson\Handle\WhereLikeHandle;
19-
use App\ApiJson\Handle\WhereRawHandle;
20-
use App\ApiJson\Handle\WhereRegexpHandle;
21-
use App\ApiJson\Method\AbstractMethod;
22-
use App\ApiJson\Replace\AbstractReplace;
23-
use App\ApiJson\Replace\KeywordCountReplace;
24-
use App\ApiJson\Replace\KeywordPageReplace;
25-
use App\ApiJson\Replace\QuoteReplace;
26-
275
class ConditionEntity
286
{
29-
/**
30-
* 替换规则
31-
* @var AbstractReplace[]
32-
*/
33-
protected array $replaceRules = [
34-
KeywordCountReplace::class,
35-
KeywordPageReplace::class,
36-
QuoteReplace::class,
37-
];
38-
39-
40-
/**
41-
* 匹配规则 根据从上自下优先先匹先出
42-
* @var AbstractHandle[]
43-
*/
44-
protected array $methodRules = [
45-
FunctionColumnHandle::class,
46-
FunctionHavingHandle::class,
47-
FunctionOffsetHandle::class,
48-
FunctionLimitHandle::class,
49-
FunctionGroupHandle::class,
50-
FunctionOrderHandle::class,
51-
WhereJsonContainsHandle::class,
52-
WhereBetweenHandle::class,
53-
WhereExistsHandle::class,
54-
WhereRegexpHandle::class,
55-
WhereLikeHandle::class,
56-
WhereRawHandle::class,
57-
WhereInHandle::class,
58-
WhereHandle::class,
59-
];
7+
protected array $changeLog = [];
608

619
/**
6210
* @param array $condition 条件
@@ -65,29 +13,27 @@ public function __construct(protected array $condition, protected array $extendD
6513
{
6614
}
6715

68-
protected function replaceHandle($key, $value, array $condition = []): array
16+
public function getExtendData(): array
6917
{
70-
foreach ($this->replaceRules as $rule) {
71-
/** @var AbstractReplace $replaceRule */
72-
$replaceRule = new $rule($key, $value, $condition, $this->extendData);
73-
$response = $replaceRule->handle();
74-
if (!is_null($response)) return $response;
75-
}
76-
return [$key, $value];
18+
return $this->extendData;
7719
}
7820

79-
/**
80-
* 整理语句
81-
*/
82-
public function setQueryCondition(QueryInterface $query)
21+
public function setCondition(array $condition)
22+
{
23+
$this->log($condition);
24+
$this->condition = $condition;
25+
}
26+
27+
public function getCondition(): array
28+
{
29+
return $this->condition;
30+
}
31+
32+
protected function log(array $condition)
8333
{
84-
foreach ($this->condition as $key => $value) {
85-
[$key, $value] = $this->replaceHandle($key, $value, $this->condition); //解决引用问题
86-
/** @var AbstractMethod $rule */
87-
foreach ($this->methodRules as $rule) {
88-
$methodRule = new $rule($query, $key, $value);
89-
if ($methodRule->handle()) break;
90-
}
91-
}
34+
$this->changeLog[] = [
35+
'old' => $this->condition,
36+
'new' => $condition
37+
];
9238
}
9339
}

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,44 @@
22

33
namespace App\ApiJson\Handle;
44

5+
use App\ApiJson\Entity\ConditionEntity;
56
use App\ApiJson\Interface\QueryInterface;
67

78
abstract class AbstractHandle
89
{
910
/** @var string 清洗后的查询key */
1011
protected string $sanitizeKey;
1112

12-
public function __construct(protected QueryInterface $query, protected string $key, protected $value)
13+
/** @var string 关键字 */
14+
protected string $keyWord;
15+
16+
protected array $unsetKey = [];
17+
18+
public function __construct(protected QueryInterface $query, protected ConditionEntity $condition)
1319
{
14-
preg_match('#(?<key>[a-zA-z0-9_]+)#', $this->key, $match);
15-
$this->sanitizeKey = $match['key'] ?? $this->key;
1620
}
1721

18-
public function handle(): bool
22+
protected function sanitizeKey(string $key): string
23+
{
24+
preg_match('#(?<key>[a-zA-z0-9_]+)#', $key, $match);
25+
return $match['key'] ?? $key;
26+
}
27+
28+
public function handle()
1929
{
20-
if (!$this->validateCondition()) return false;
2130
$this->buildModel();
22-
return true;
31+
$this->unsetKeySaveCondition();
2332
}
2433

25-
abstract protected function validateCondition(): bool;
34+
protected function unsetKeySaveCondition()
35+
{
36+
if (empty($this->unsetKey)) return;
37+
$condition = $this->condition->getCondition();
38+
foreach ($this->unsetKey as $key) {
39+
unset($condition[$key]);
40+
}
41+
$this->condition->setCondition($condition);
42+
}
2643

2744
abstract protected function buildModel();
2845
}

app/ApiJson/Handle/FunctionColumnHandle.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
class FunctionColumnHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@column';
10-
}
7+
protected string $keyWord = '@column';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$this->value = str_replace([';',':'], [',', ' AS '], $this->value);
15-
$this->query->select(explode(',', $this->value));
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$value = str_replace([';',':'], [',', ' AS '], $value);
19+
$this->query->select(explode(',', $value));
20+
$this->unsetKey[] = $this->keyWord;
21+
}
1622
}
1723
}

app/ApiJson/Handle/FunctionGroupHandle.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
class FunctionGroupHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@group';
10-
}
7+
protected string $keyWord = '@group';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$groupArr = explode(',', $this->value);
15-
$this->query->groupBy($groupArr);
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$groupArr = explode(',', $value);
19+
$this->query->groupBy($groupArr);
20+
$this->unsetKey[] = $this->keyWord;
21+
}
1622
}
1723
}

app/ApiJson/Handle/FunctionHavingHandle.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44

55
class FunctionHavingHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@having';
10-
}
7+
protected string $keyWord = '@having';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$havingArr = explode(';', $this->value);
15-
foreach ($havingArr as $having) {
16-
$this->query->having($having);
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$havingArr = explode(';', $value);
19+
foreach ($havingArr as $having) {
20+
$this->query->having($having);
21+
}
22+
$this->unsetKey[] = $this->keyWord;
1723
}
1824
}
1925
}

app/ApiJson/Handle/FunctionLimitHandle.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
class FunctionLimitHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@limit';
10-
}
7+
protected string $keyWord = '@limit';
118

129
protected function buildModel()
1310
{
14-
$this->query->limit((int)$this->value);
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$this->query->limit((int)$value);
19+
$this->unsetKey[] = $this->keyWord;
20+
}
1521
}
1622
}

app/ApiJson/Handle/FunctionOffsetHandle.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
class FunctionOffsetHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@offset';
10-
}
7+
protected string $keyWord = '@offset';
118

129
protected function buildModel()
1310
{
14-
$this->query->offset((int)$this->value);
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$this->query->offset((int)$value);
19+
$this->unsetKey[] = $this->keyWord;
20+
}
1521
}
1622
}

app/ApiJson/Handle/FunctionOrderHandle.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,22 @@
44

55
class FunctionOrderHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@order';
10-
}
7+
protected string $keyWord = '@order';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$orderArr = explode(',', $this->value);
15-
foreach ($orderArr as $order) {
16-
$this->query->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$orderArr = explode(',', $value);
19+
foreach ($orderArr as $order) {
20+
$this->query->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
21+
}
22+
$this->unsetKey[] = $this->keyWord;
1723
}
1824
}
1925
}

app/ApiJson/Handle/WhereBetweenHandle.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@
44

55
class WhereBetweenHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return str_ends_with($this->key, '$');
10-
}
11-
127
protected function buildModel()
138
{
14-
$value = !is_array($this->value) ? [$this->value] : $this->value;
15-
$sql = [];
16-
foreach ($value as $item) {
17-
$itemArr = explode(',', $item);
18-
$sql[] = sprintf("%s BETWEEN %s AND %s", $this->sanitizeKey, trim($itemArr[0]), trim($itemArr[1]));
9+
foreach (array_filter($this->condition->getCondition(), function($key){
10+
return str_ends_with($key, '$');
11+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
12+
{
13+
$value = !is_array($value) ? [$value] : $value;
14+
$sql = [];
15+
foreach ($value as $item) {
16+
$itemArr = explode(',', $item);
17+
$sql[] = sprintf("%s BETWEEN %s AND %s", $this->sanitizeKey($key), trim($itemArr[0]), trim($itemArr[1]));
18+
}
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
20+
$this->unsetKey[] = $key;
1921
}
20-
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
2122
}
2223
}

0 commit comments

Comments
 (0)