Skip to content

Commit 77477bb

Browse files
committed
update 实现数组查询 (这个实现方法有点奇怪 应分开结构)
1 parent 6152907 commit 77477bb

11 files changed

+132
-57
lines changed

app/ApiJson/ApiJson.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
use App\ApiJson\Parse\Parse;
66
use App\Constants\ResponseCode;
77
use Hyperf\HttpServer\Contract\RequestInterface;
8-
use Psr\Log\LogLevel;
98

109
class ApiJson
1110
{
@@ -15,6 +14,12 @@ public function __construct(protected RequestInterface $request, protected strin
1514

1615
public function Query(): array
1716
{
17+
if (!is_array(json_decode($this->request->getBody()->getContents(), true))) {
18+
return [
19+
'code' => ResponseCode::SERVER_ERROR,
20+
'msg' => ResponseCode::getMessage(ResponseCode::SERVER_ERROR)
21+
];
22+
}
1823
$parse = new Parse(json_decode($this->request->getBody()->getContents(), true), $this->method, $this->request->input('tag', ''));
1924
return array_merge([
2025
'code' => ResponseCode::SUCCESS,

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\ApiJson\Entity;
44

5+
use App\ApiJson\Handle\FunctionLimitHandle;
6+
use App\ApiJson\Handle\FunctionOffsetHandle;
57
use App\ApiJson\Interface\QueryInterface;
68
use App\ApiJson\Handle\AbstractHandle;
79
use App\ApiJson\Handle\FunctionColumnHandle;
@@ -18,6 +20,8 @@
1820
use App\ApiJson\Handle\WhereRegexpHandle;
1921
use App\ApiJson\Method\AbstractMethod;
2022
use App\ApiJson\Replace\AbstractReplace;
23+
use App\ApiJson\Replace\KeywordCountReplace;
24+
use App\ApiJson\Replace\KeywordPageReplace;
2125
use App\ApiJson\Replace\QuoteReplace;
2226

2327
class ConditionEntity
@@ -27,6 +31,8 @@ class ConditionEntity
2731
* @var AbstractReplace[]
2832
*/
2933
protected array $replaceRules = [
34+
KeywordCountReplace::class,
35+
KeywordPageReplace::class,
3036
QuoteReplace::class,
3137
];
3238

@@ -38,6 +44,8 @@ class ConditionEntity
3844
protected array $methodRules = [
3945
FunctionColumnHandle::class,
4046
FunctionHavingHandle::class,
47+
FunctionOffsetHandle::class,
48+
FunctionLimitHandle::class,
4149
FunctionGroupHandle::class,
4250
FunctionOrderHandle::class,
4351
WhereJsonContainsHandle::class,
@@ -57,14 +65,13 @@ public function __construct(protected array $condition, protected array $extendD
5765
{
5866
}
5967

60-
protected function replaceHandle($key, $value): array
68+
protected function replaceHandle($key, $value, array $condition = []): array
6169
{
6270
foreach ($this->replaceRules as $rule) {
6371
/** @var AbstractReplace $replaceRule */
64-
$replaceRule = new $rule($key, $value);
72+
$replaceRule = new $rule($key, $value, $condition, $this->extendData);
6573
$response = $replaceRule->handle();
66-
if (is_null($response)) break;
67-
return $response;
74+
if (!is_null($response)) return $response;
6875
}
6976
return [$key, $value];
7077
}
@@ -75,10 +82,9 @@ protected function replaceHandle($key, $value): array
7582
public function setQueryCondition(QueryInterface $query)
7683
{
7784
foreach ($this->condition as $key => $value) {
85+
[$key, $value] = $this->replaceHandle($key, $value, $this->condition); //解决引用问题
7886
/** @var AbstractMethod $rule */
7987
foreach ($this->methodRules as $rule) {
80-
[$key, $value] = $this->replaceHandle($key, $value); //解决引用问题
81-
8288
$methodRule = new $rule($query, $key, $value);
8389
if ($methodRule->handle()) break;
8490
}

app/ApiJson/Entity/TableEntity.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TableEntity
1717
* @param string $tableName 表名
1818
* @param array $jsonContent json源数据
1919
*/
20-
public function __construct(protected string $tableName, protected array $jsonContent, protected array $extendData = [])
20+
public function __construct(protected string $tableName, protected array $jsonContent, protected array $globalArgs, protected array $extendData = [])
2121
{
2222
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
2323
$this->realTableName = $sanitizeTableName;
@@ -54,7 +54,7 @@ protected function getContentByTableName(): array
5454

5555
protected function parseConditionEntity()
5656
{
57-
$entity = new ConditionEntity($this->content, $this->extendData);
57+
$entity = new ConditionEntity(array_merge($this->globalArgs, $this->content), $this->extendData);
5858
$this->conditionEntity = $entity;
5959
}
6060
}

app/ApiJson/Handle/WhereHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ protected function validateCondition(): bool
1111

1212
protected function buildModel()
1313
{
14-
$this->query->where($this->sanitizeKey, $this->value);
14+
$this->query->where($this->sanitizeKey, '=', $this->value);
1515
}
1616
}

app/ApiJson/Method/AbstractMethod.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,13 @@ abstract class AbstractMethod
1313
/** @var QueryInterface $query */
1414
protected QueryInterface $query;
1515

16+
/** @var bool $isQueryMany */
17+
protected bool $isQueryMany = false;
18+
1619
public function __construct(protected TableEntity $tableEntity, protected string $method = 'GET')
1720
{
1821
$this->buildQuery();
22+
$this->isQueryMany = str_ends_with($this->tableEntity->getTableName(), '[]');
1923
}
2024

2125
public function handle(): ?array
@@ -32,24 +36,25 @@ protected function buildQuery()
3236

3337
protected function parseManyResponse(array $ids, bool $isQueryMany = false): array
3438
{
35-
$response = [
36-
'code' => !empty($ids) ? ResponseCode::SUCCESS : ResponseCode::SERVER_ERROR,
37-
'msg' => ResponseCode::getMessage(!empty($ids) ? ResponseCode::SUCCESS : ResponseCode::SERVER_ERROR),
38-
];
3939
if ($isQueryMany) {
40-
$response = array_merge($response, [
40+
$response = [
4141
'id[]' => $ids,
4242
'count' => count($ids)
43-
]);
43+
];
4444
} else {
4545
$response['id'] = current($ids) ?: 0;
4646
}
4747
return $response;
4848
}
4949

50+
public function setQueryMany(bool $isQueryMany = false)
51+
{
52+
$this->isQueryMany = $isQueryMany;
53+
}
54+
5055
protected function isQueryMany(): bool
5156
{
52-
return str_ends_with($this->tableEntity->getTableName(), '[]');
57+
return $this->isQueryMany;
5358
}
5459

5560
abstract protected function validateCondition(): bool;

app/ApiJson/Method/GetMethod.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,18 @@ protected function process()
1616
$conditionEntity = $this->tableEntity->getConditionEntity();
1717
$conditionEntity->setQueryCondition($this->query);
1818

19-
$queryMany = str_ends_with($this->tableEntity->getTableName(), '[]');
19+
$queryMany = $this->isQueryMany();
2020
if (!$queryMany) {
2121
$this->query->limit(1);
2222
}
2323
$result = $this->query->all();
24-
!$queryMany && $result = current($result);
25-
24+
if ($queryMany) {
25+
foreach ($result as $key => $item) {
26+
$result[$key] = [$this->tableEntity->getTableName() => $item];
27+
}
28+
} else {
29+
$result = current($result);
30+
}
2631
return $result ?: [];
2732
}
2833
}

app/ApiJson/Parse/Parse.php

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
use App\ApiJson\Method\HeadMethod;
1010
use App\ApiJson\Method\PostMethod;
1111
use App\ApiJson\Method\PutMethod;
12-
use App\Constants\ResponseCode;
1312

1413
class Parse
1514
{
1615
protected array $tagColumn = [
1716
'tag' => null
1817
];
1918

19+
protected array $globalKey = [
20+
'count' => null,
21+
'page' => null,
22+
];
23+
2024
protected array $supMethod = [
2125
GetMethod::class,
2226
HeadMethod::class,
@@ -31,21 +35,35 @@ public function __construct(protected array $json, protected string $method = 'G
3135
{
3236
}
3337

34-
public function handle(): array
38+
public function handle(bool $isQueryMany = false): array
3539
{
36-
$result = [
37-
'code' => ResponseCode::SUCCESS,
38-
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS),
39-
];
40-
$this->jsonParse();
40+
$result = [];
4141
foreach ($this->json as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
42-
$this->tableEntities[$tableName] = new TableEntity($tableName, $this->json, $result);
42+
if (in_array($tableName, $this->filterKey())) {
43+
$this->tagColumn[$tableName] = $condition;
44+
continue;
45+
}
46+
if (in_array($tableName, $this->globalKey())) {
47+
$this->globalKey[$tableName] = $condition;
48+
continue;
49+
}
50+
if ($tableName == '[]') {
51+
$parse = new self($condition, $this->method, $condition['tag'] ?? '');
52+
$result[$tableName] = $parse->handle(true); //提供行为
53+
continue; //跳出不往下执行
54+
}
55+
$this->tableEntities[$tableName] = new TableEntity($tableName, $this->json, $this->getGlobalArgs(), $result);
4356
foreach ($this->supMethod as $methodClass) {
4457
/** @var AbstractMethod $method */
4558
$method = new $methodClass($this->tableEntities[$tableName], $this->method);
59+
$method->setQueryMany($isQueryMany);
4660
$response = $method->handle();
4761
if (!is_null($response)) {
48-
$result[$condition['*'] ?? $tableName] = $response;
62+
if ($isQueryMany) {
63+
$result = $response;
64+
} else {
65+
$result[$tableName] = $response;
66+
}
4967
break;
5068
}
5169
}
@@ -54,34 +72,18 @@ public function handle(): array
5472
return $result;
5573
}
5674

57-
/**
58-
* 整理不同形式的json数据到统一格式再处理
59-
*/
60-
protected function jsonParse()
75+
protected function filterKey(): array
6176
{
62-
foreach ($this->json as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
63-
if (in_array($tableName, $this->filterKey())) {
64-
$this->tagColumn[$tableName] = $condition; //特殊
65-
unset($this->json[$tableName]);
66-
break;
67-
}
68-
if ($tableName == '[]') {
69-
$count = (int)$condition['count'] ?? 10;
70-
$page = (int)$condition['page'] ?? 1; //赋予默认值
71-
$tableName = array_key_first($condition); //剩下的值为table
72-
$condition = $condition[$tableName]; //替换条件
73-
$condition = array_merge($condition, [
74-
'@limit' => $count, //查询长度
75-
'@offset' => $page * $count, //查询起始长度
76-
'*' => '[]' //赋予替换表明的标志
77-
]);
78-
$this->json[$tableName . '[]'] = $condition;
79-
}
80-
}
77+
return array_keys($this->tagColumn);
8178
}
8279

83-
protected function filterKey(): array
80+
protected function getGlobalArgs(): array
8481
{
85-
return array_keys($this->tagColumn);
82+
return array_filter($this->globalKey);
83+
}
84+
85+
protected function globalKey(): array
86+
{
87+
return array_keys($this->globalKey);
8688
}
8789
}

app/ApiJson/Replace/AbstractReplace.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
abstract class AbstractReplace
66
{
7-
public function __construct(protected string $key, protected $value, protected array $extendData)
7+
public function __construct(protected string $key, protected $value, protected $condition, protected array $extendData)
88
{
99
}
1010

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App\ApiJson\Replace;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
use Hyperf\Utils\Arr;
7+
8+
class KeywordCountReplace extends AbstractReplace
9+
{
10+
protected function validateCondition(): bool
11+
{
12+
return $this->key == 'count';
13+
}
14+
15+
protected function process()
16+
{
17+
$this->key = "@limit";
18+
return [$this->key, $this->value]; //必须
19+
}
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace App\ApiJson\Replace;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
use Hyperf\Utils\Arr;
7+
8+
class KeywordPageReplace extends AbstractReplace
9+
{
10+
protected function validateCondition(): bool
11+
{
12+
return $this->key == 'page';
13+
}
14+
15+
protected function process()
16+
{
17+
$count = 10;
18+
if (!empty($this->condition['count'])) {
19+
$count = (int) $this->condition['count'];
20+
} else if (!empty($this->condition['limit'])) {
21+
$count = (int) $this->condition['limit'];
22+
} else if (!empty($this->condition['@limit'])) {
23+
$count = (int) $this->condition['@limit'];
24+
} else if (!empty($this->condition['@count'])) {
25+
$count = (int) $this->condition['@count'];
26+
}
27+
28+
$this->key = '@offset';
29+
$this->value = ((int)$this->value - 1) * $count;
30+
return [$this->key, $this->value]; //必须
31+
}
32+
}

app/ApiJson/Replace/QuoteReplace.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class QuoteReplace extends AbstractReplace
99
{
1010
protected function validateCondition(): bool
1111
{
12-
return str_ends_with($this->key, '@');
12+
return str_ends_with($this->key, '@') && !str_ends_with($this->key, '}{@');
1313
}
1414

1515
protected function process()
1616
{
1717
$path = str_replace('/', '.', $this->value);
1818
$this->value = data_get($this->extendData, $path);
19-
$this->key = str_replace('@', '', $this->key);
19+
$this->key = substr($this->key, 0, strlen($this->key) - 1);
2020

2121
return [$this->key, $this->value]; //必须
2222
}

0 commit comments

Comments
 (0)