Skip to content

Commit e85f9b6

Browse files
committed
Merge branch 'master' of github.com:kvnZero/hyperf-APIJSON
2 parents 7c1a658 + b0f552e commit e85f9b6

30 files changed

+849
-298
lines changed

app/ApiJson/ApiJson.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,13 @@
99

1010
class ApiJson
1111
{
12-
/** @var array $request */
13-
protected $request;
14-
15-
/** @var string $method */
16-
protected $method;
17-
18-
public function __construct(RequestInterface $request, string $method)
12+
public function __construct(protected RequestInterface $request, protected string $method)
1913
{
20-
$this->request = $request;
21-
$this->method = $method;
2214
}
2315

2416
public function Query(): array
2517
{
26-
$parse = new Parse($this->request->getBody()->getContents(), $this->method, $this->request->input('tag', ''));
18+
$parse = new Parse(json_decode($this->request->getBody()->getContents(), true), $this->method, $this->request->input('tag', ''));
2719
return array_merge([
2820
'code' => ResponseCode::SUCCESS,
2921
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS)
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\ApiJson\Entity;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
use App\ApiJson\Handle\AbstractHandle;
7+
use App\ApiJson\Handle\FunctionColumnHandle;
8+
use App\ApiJson\Handle\FunctionGroupHandle;
9+
use App\ApiJson\Handle\FunctionHavingHandle;
10+
use App\ApiJson\Handle\FunctionOrderHandle;
11+
use App\ApiJson\Handle\WhereBetweenHandle;
12+
use App\ApiJson\Handle\WhereExistsHandle;
13+
use App\ApiJson\Handle\WhereHandle;
14+
use App\ApiJson\Handle\WhereInHandle;
15+
use App\ApiJson\Handle\WhereJsonContainsHandle;
16+
use App\ApiJson\Handle\WhereLikeHandle;
17+
use App\ApiJson\Handle\WhereRawHandle;
18+
use App\ApiJson\Handle\WhereRegexpHandle;
19+
20+
class ConditionEntity
21+
{
22+
/**
23+
* 匹配规则 根据从上自下优先先匹先出
24+
* @var AbstractHandle[]
25+
*/
26+
protected array $methodRules = [
27+
FunctionColumnHandle::class,
28+
FunctionHavingHandle::class,
29+
FunctionGroupHandle::class,
30+
FunctionOrderHandle::class,
31+
WhereJsonContainsHandle::class,
32+
WhereBetweenHandle::class,
33+
WhereExistsHandle::class,
34+
WhereRegexpHandle::class,
35+
WhereLikeHandle::class,
36+
WhereRawHandle::class,
37+
WhereInHandle::class,
38+
WhereHandle::class,
39+
];
40+
41+
/**
42+
* @param array $condition 条件
43+
*/
44+
public function __construct(protected array $condition)
45+
{
46+
}
47+
48+
/**
49+
* 整理语句
50+
*/
51+
public function setQueryCondition(QueryInterface $query)
52+
{
53+
foreach ($this->condition as $key => $value) {
54+
foreach ($this->methodRules as $rule) {
55+
$methodRule = new $rule($query, $key, $value);
56+
if ($methodRule->handle()) break;
57+
}
58+
}
59+
}
60+
}

app/ApiJson/Entity/TableEntity.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace App\ApiJson\Entity;
4+
5+
class TableEntity
6+
{
7+
/** @var ConditionEntity $ConditionEntity */
8+
protected ConditionEntity $conditionEntity;
9+
10+
/** @var string $realTableName 真实表名 */
11+
protected string $realTableName;
12+
13+
/** @var array $content 表名对应的数据 */
14+
protected array $content;
15+
16+
/**
17+
* @param string $tableName 表名
18+
* @param array $jsonContent json源数据
19+
*/
20+
public function __construct(protected string $tableName, protected array $jsonContent)
21+
{
22+
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
23+
$this->realTableName = $sanitizeTableName;
24+
$this->content = $this->getContentByTableName();
25+
$this->parseConditionEntity();
26+
}
27+
28+
public function getTableName(): string
29+
{
30+
return $this->tableName;
31+
}
32+
33+
public function getRealTableName(): string
34+
{
35+
return $this->realTableName;
36+
}
37+
38+
public function getContent(): array
39+
{
40+
return $this->content;
41+
}
42+
43+
public function getConditionEntity(): ConditionEntity
44+
{
45+
return $this->conditionEntity;
46+
}
47+
48+
protected function getContentByTableName(): array
49+
{
50+
$content = $this->jsonContent[$this->tableName];
51+
if (isset($content[$this->realTableName])) $content = $content[$this->realTableName];
52+
return $content;
53+
}
54+
55+
protected function parseConditionEntity()
56+
{
57+
$entity = new ConditionEntity($this->content);
58+
$this->conditionEntity = $entity;
59+
}
60+
}

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
use App\ApiJson\Interface\QueryInterface;
6+
7+
abstract class AbstractHandle
8+
{
9+
/** @var string 清洗后的查询key */
10+
protected string $sanitizeKey;
11+
12+
public function __construct(protected QueryInterface $query, protected string $key, protected $value)
13+
{
14+
preg_match('#(?<key>[a-zA-z0-9_]+)#', $this->key, $match);
15+
$this->sanitizeKey = $match['key'] ?? $this->key;
16+
}
17+
18+
public function handle(): bool
19+
{
20+
if (!$this->validateCondition()) return false;
21+
$this->buildModel();
22+
return true;
23+
}
24+
25+
abstract protected function validateCondition(): bool;
26+
27+
abstract protected function buildModel();
28+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionColumnHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@column';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->value = str_replace([';',':'], [',', ' AS '], $this->value);
15+
$this->query->select(explode(',', $this->value));
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionGroupHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@group';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$groupArr = explode(',', $this->value);
15+
$this->query->groupBy($groupArr);
16+
}
17+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionHavingHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@having';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$havingArr = explode(';', $this->value);
15+
foreach ($havingArr as $having) {
16+
$this->query->having($having);
17+
}
18+
}
19+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionOrderHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return $this->key === '@order';
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$orderArr = explode(',', $this->value);
15+
foreach ($orderArr as $order) {
16+
$this->query->orderBy(str_replace(['-', '+'], '', $order), str_ends_with($order, '-') ? 'desc' : 'asc');
17+
}
18+
}
19+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereBetweenHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return str_ends_with($this->key, '$');
10+
}
11+
12+
protected function buildModel()
13+
{
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]));
19+
}
20+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
use Hyperf\Database\Query\Builder;
6+
7+
class WhereExistsHandle extends AbstractHandle
8+
{
9+
protected function validateCondition(): bool
10+
{
11+
return str_ends_with($this->key, '}{@');
12+
}
13+
14+
protected function buildModel()
15+
{
16+
$this->query->whereExists(function(Builder $query) {
17+
$query = $query->from($this->value['from']);
18+
19+
//这里应该再接入处理列表
20+
foreach ($this->value[$this->value['from']] as $k => $v) {
21+
$query->where($k, $v);
22+
}
23+
});
24+
}
25+
}

app/ApiJson/Handle/WhereHandle.php

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 WhereHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return true;
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->query->where($this->sanitizeKey, $this->value);
15+
}
16+
}

app/ApiJson/Handle/WhereInHandle.php

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 WhereInHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return str_ends_with($this->key, '{}') && is_array($this->value);
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$this->query->whereIn($this->sanitizeKey, $this->value);
15+
}
16+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereJsonContainsHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return str_ends_with($this->key, '<>');
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$value = !is_array($this->value) ? [$this->value] : $this->value;
15+
$sql = [];
16+
foreach ($value as $item) {
17+
$sql[] = sprintf("json_contains(%s,%s)", $this->sanitizeKey, trim($item));
18+
}
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereLikeHandle extends AbstractHandle
6+
{
7+
protected function validateCondition(): bool
8+
{
9+
return str_ends_with($this->key, '$');
10+
}
11+
12+
protected function buildModel()
13+
{
14+
$value = !is_array($this->value) ? [$this->value] : $this->value;
15+
$sql = [];
16+
foreach ($value as $item) {
17+
$sql[] = sprintf("%s LIKE %s", $this->sanitizeKey, trim($item));
18+
}
19+
$this->query->whereRaw(join(' OR ', $sql)); //3.2.3
20+
}
21+
}

0 commit comments

Comments
 (0)