Skip to content

Commit 6152907

Browse files
committed
update 完善数组查询运算符 & 实现一对一关联
1 parent b319b3b commit 6152907

File tree

10 files changed

+148
-15
lines changed

10 files changed

+148
-15
lines changed

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,21 @@
1616
use App\ApiJson\Handle\WhereLikeHandle;
1717
use App\ApiJson\Handle\WhereRawHandle;
1818
use App\ApiJson\Handle\WhereRegexpHandle;
19+
use App\ApiJson\Method\AbstractMethod;
20+
use App\ApiJson\Replace\AbstractReplace;
21+
use App\ApiJson\Replace\QuoteReplace;
1922

2023
class ConditionEntity
2124
{
25+
/**
26+
* 替换规则
27+
* @var AbstractReplace[]
28+
*/
29+
protected array $replaceRules = [
30+
QuoteReplace::class,
31+
];
32+
33+
2234
/**
2335
* 匹配规则 根据从上自下优先先匹先出
2436
* @var AbstractHandle[]
@@ -41,17 +53,32 @@ class ConditionEntity
4153
/**
4254
* @param array $condition 条件
4355
*/
44-
public function __construct(protected array $condition)
56+
public function __construct(protected array $condition, protected array $extendData = [])
4557
{
4658
}
4759

60+
protected function replaceHandle($key, $value): array
61+
{
62+
foreach ($this->replaceRules as $rule) {
63+
/** @var AbstractReplace $replaceRule */
64+
$replaceRule = new $rule($key, $value);
65+
$response = $replaceRule->handle();
66+
if (is_null($response)) break;
67+
return $response;
68+
}
69+
return [$key, $value];
70+
}
71+
4872
/**
4973
* 整理语句
5074
*/
5175
public function setQueryCondition(QueryInterface $query)
5276
{
5377
foreach ($this->condition as $key => $value) {
78+
/** @var AbstractMethod $rule */
5479
foreach ($this->methodRules as $rule) {
80+
[$key, $value] = $this->replaceHandle($key, $value); //解决引用问题
81+
5582
$methodRule = new $rule($query, $key, $value);
5683
if ($methodRule->handle()) break;
5784
}

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)
20+
public function __construct(protected string $tableName, protected array $jsonContent, 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);
57+
$entity = new ConditionEntity($this->content, $this->extendData);
5858
$this->conditionEntity = $entity;
5959
}
6060
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionLimitHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@limit';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->query->limit((int)$this->value);
15+
}
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionOffsetHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@offset';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->query->offset((int)$this->value);
15+
}
16+
}

app/ApiJson/Interface/QueryInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public function count($columns = '*'): int;
3232

3333
public function limit(int $value): self;
3434

35+
public function offset(int $value): self;
36+
3537
public function insertGetId(array $values, $sequence = null): int;
3638

3739
public function update(array $values): bool;

app/ApiJson/Model/MysqlQuery.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,12 @@ public function limit(int $value): self
120120
return $this;
121121
}
122122

123+
public function offset(int $value): self
124+
{
125+
$this->db->offset($value);
126+
return $this;
127+
}
128+
123129
public function all(): array
124130
{
125131
return $this->db->get()->all();

app/ApiJson/Parse/Parse.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,15 @@ public function handle(): array
3737
'code' => ResponseCode::SUCCESS,
3838
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS),
3939
];
40-
foreach ($this->json as $tableName => $condition) {
41-
if (in_array($tableName, $this->filterKey())) {
42-
$this->tagColumn[$tableName] = $condition; //特殊
43-
break;
44-
}
45-
$this->tableEntities[$tableName] = new TableEntity($tableName, $this->json);
40+
$this->jsonParse();
41+
foreach ($this->json as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
42+
$this->tableEntities[$tableName] = new TableEntity($tableName, $this->json, $result);
4643
foreach ($this->supMethod as $methodClass) {
4744
/** @var AbstractMethod $method */
4845
$method = new $methodClass($this->tableEntities[$tableName], $this->method);
4946
$response = $method->handle();
5047
if (!is_null($response)) {
51-
$result[$tableName] = $response;
48+
$result[$condition['*'] ?? $tableName] = $response;
5249
break;
5350
}
5451
}
@@ -57,6 +54,32 @@ public function handle(): array
5754
return $result;
5855
}
5956

57+
/**
58+
* 整理不同形式的json数据到统一格式再处理
59+
*/
60+
protected function jsonParse()
61+
{
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+
}
81+
}
82+
6083
protected function filterKey(): array
6184
{
6285
return array_keys($this->tagColumn);
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+
abstract class AbstractReplace
6+
{
7+
public function __construct(protected string $key, protected $value, protected array $extendData)
8+
{
9+
}
10+
11+
public function handle(): ?array
12+
{
13+
if (!$this->validateCondition()) return null;
14+
return $this->process();
15+
}
16+
17+
abstract protected function validateCondition(): bool;
18+
19+
abstract protected function process();
20+
}

app/ApiJson/Replace/QuoteReplace.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace App\ApiJson\Replace;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
use Hyperf\Utils\Arr;
7+
8+
class QuoteReplace extends AbstractReplace
9+
{
10+
protected function validateCondition(): bool
11+
{
12+
return str_ends_with($this->key, '@');
13+
}
14+
15+
protected function process()
16+
{
17+
$path = str_replace('/', '.', $this->value);
18+
$this->value = data_get($this->extendData, $path);
19+
$this->key = str_replace('@', '', $this->key);
20+
21+
return [$this->key, $this->value]; //必须
22+
}
23+
}

config/routes.php

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

1414
Router::addRoute(['GET', 'POST', 'HEAD', 'PUT', 'DELETE'], '/', 'App\Controller\IndexController@index');
1515

16-
Router::addRoute(['GET'], '/get/[{tag}]', 'App\Controller\IndexController@get');
17-
Router::addRoute(['POST'], '/head/[{tag}]', 'App\Controller\IndexController@head');
18-
Router::addRoute(['POST'], '/post/[{tag}]', 'App\Controller\IndexController@post');
19-
Router::addRoute(['POST', 'PUT'], '/put/[{tag}]', 'App\Controller\IndexController@put');
20-
Router::addRoute(['POST', 'DELETE'], '/delete/[{tag}]', 'App\Controller\IndexController@delete');
16+
Router::addRoute(['POST'], '/get[/{tag}]', 'App\Controller\IndexController@get');
17+
Router::addRoute(['POST'], '/head[/{tag}]', 'App\Controller\IndexController@head');
18+
Router::addRoute(['POST'], '/post[/{tag}]', 'App\Controller\IndexController@post');
19+
Router::addRoute(['POST'], '/put[/{tag}]', 'App\Controller\IndexController@put');
20+
Router::addRoute(['POST'], '/delete[/{tag}]', 'App\Controller\IndexController@delete');
2121

2222
Router::get('/favicon.ico', function () {
2323
return '';

0 commit comments

Comments
 (0)