99use App \ApiJson \Method \HeadMethod ;
1010use App \ApiJson \Method \PostMethod ;
1111use App \ApiJson \Method \PutMethod ;
12- use App \Constants \ResponseCode ;
1312
1413class 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}
0 commit comments