1+ <?php
2+
3+ namespace App \ApiJson \Handle ;
4+
5+ class FunctionCombineHandle extends AbstractHandle
6+ {
7+ protected string $ keyWord = '@combine ' ;
8+
9+ public function buildModel ()
10+ {
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+ $ conditionKeyArr = explode (', ' , $ value );
19+ $ op = [
20+ '& ' => [],
21+ '| ' => [],
22+ '! ' => []
23+ ];
24+ foreach ($ conditionKeyArr as $ conditionKey ) {
25+ if (str_starts_with ($ conditionKey , '& ' )) {
26+ $ op ['& ' ] = $ conditionKey ;
27+ } else if (str_starts_with ($ conditionKey , '! ' )) {
28+ $ op ['! ' ] = $ conditionKey ;
29+ } else {
30+ $ op ['| ' ] = $ conditionKey ;
31+ }
32+ }
33+ $ sql = [];
34+ $ bind = [];
35+ $ queryWhere = $ this ->condition ->getQueryWhere ();
36+ foreach ($ op as $ opKey => $ opValue ) {
37+ if (empty ($ value )) continue ;
38+ $ subSql = [];
39+ foreach ($ opValue as $ key ) {
40+ $ subSql [] = $ queryWhere [$ key ]['sql ' ];
41+ $ bind = array_merge ($ bind , $ queryWhere [$ key ]['bind ' ]);
42+ unset($ queryWhere [$ key ]);
43+ }
44+ $ boolean = ' OR ' ;
45+ if ($ opKey == '& ' ) $ boolean = ' AND ' ;
46+ $ pref = ($ opKey == '! ' ) ? '! ' : '' ;
47+ $ sql [] = sprintf ('%s(%s) ' , $ pref , join ($ boolean , $ subSql ));
48+ }
49+ $ queryWhere [$ this ->keyWord ] = [
50+ 'sql ' => join (' AND ' , $ sql ),
51+ 'bind ' => $ bind
52+ ];
53+ }
54+ }
55+ }
0 commit comments