22
33namespace App \ApiJson \Entity ;
44
5- use App \ApiJson \Handle \FunctionLimitHandle ;
6- use App \ApiJson \Handle \FunctionOffsetHandle ;
7- use App \ApiJson \Interface \QueryInterface ;
8- use App \ApiJson \Handle \AbstractHandle ;
9- use App \ApiJson \Handle \FunctionColumnHandle ;
10- use App \ApiJson \Handle \FunctionGroupHandle ;
11- use App \ApiJson \Handle \FunctionHavingHandle ;
12- use App \ApiJson \Handle \FunctionOrderHandle ;
13- use App \ApiJson \Handle \WhereBetweenHandle ;
14- use App \ApiJson \Handle \WhereExistsHandle ;
15- use App \ApiJson \Handle \WhereHandle ;
16- use App \ApiJson \Handle \WhereInHandle ;
17- use App \ApiJson \Handle \WhereJsonContainsHandle ;
18- use App \ApiJson \Handle \WhereLikeHandle ;
19- use App \ApiJson \Handle \WhereRawHandle ;
20- use App \ApiJson \Handle \WhereRegexpHandle ;
21- use App \ApiJson \Method \AbstractMethod ;
22- use App \ApiJson \Replace \AbstractReplace ;
23- use App \ApiJson \Replace \KeywordCountReplace ;
24- use App \ApiJson \Replace \KeywordPageReplace ;
25- use App \ApiJson \Replace \QuoteReplace ;
26-
275class ConditionEntity
286{
7+ protected array $ changeLog = [];
8+
299 /**
30- * 替换规则
31- * @var AbstractReplace[]
10+ * @var array
3211 */
33- protected array $ replaceRules = [
34- KeywordCountReplace::class,
35- KeywordPageReplace::class,
36- QuoteReplace::class,
37- ];
12+ protected array $ where = [];
3813
14+ protected int $ limit = 0 ;
15+ protected int $ offset = 0 ;
16+ protected array $ column = ['* ' ];
17+ protected array $ group = [];
18+ protected array $ order = [];
19+ protected array $ having = [];
3920
4021 /**
41- * 匹配规则 根据从上自下优先先匹先出
42- * @var AbstractHandle[]
22+ * @param array $condition 条件
4323 */
44- protected array $ methodRules = [
45- FunctionColumnHandle::class,
46- FunctionHavingHandle::class,
47- FunctionOffsetHandle::class,
48- FunctionLimitHandle::class,
49- FunctionGroupHandle::class,
50- FunctionOrderHandle::class,
51- WhereJsonContainsHandle::class,
52- WhereBetweenHandle::class,
53- WhereExistsHandle::class,
54- WhereRegexpHandle::class,
55- WhereLikeHandle::class,
56- WhereRawHandle::class,
57- WhereInHandle::class,
58- WhereHandle::class,
59- ];
24+ public function __construct (protected array $ condition , protected array $ extendData = [])
25+ {
26+ }
27+
28+ public function getExtendData (): array
29+ {
30+ return $ this ->extendData ;
31+ }
32+
33+ public function setCondition (array $ condition )
34+ {
35+ $ this ->log ($ condition );
36+ $ this ->condition = $ condition ;
37+ }
38+
39+ public function getCondition (): array
40+ {
41+ return $ this ->condition ;
42+ }
43+
44+ public function getQueryWhere (): array
45+ {
46+ return $ this ->where ;
47+ }
48+
49+ public function setQueryWhere (array $ where )
50+ {
51+ $ this ->where = $ where ;
52+ }
53+
54+ public function addQueryWhere (string $ key , string $ sql , array $ bindArgs = [])
55+ {
56+ $ this ->where [$ key ] = [
57+ 'sql ' => $ sql ,
58+ 'bind ' => $ bindArgs
59+ ];
60+ }
6061
6162 /**
62- * @param array $condition 条件
63+ * @param array|string[] $column
6364 */
64- public function __construct ( protected array $ condition , protected array $ extendData = [])
65+ public function setColumn ( array $ column ): void
6566 {
67+ $ this ->column = $ column ;
6668 }
6769
68- protected function replaceHandle ($ key , $ value , array $ condition = []): array
70+ /**
71+ * @param array $group
72+ */
73+ public function setGroup (array $ group ): void
6974 {
70- foreach ($ this ->replaceRules as $ rule ) {
71- /** @var AbstractReplace $replaceRule */
72- $ replaceRule = new $ rule ($ key , $ value , $ condition , $ this ->extendData );
73- $ response = $ replaceRule ->handle ();
74- if (!is_null ($ response )) return $ response ;
75- }
76- return [$ key , $ value ];
75+ $ this ->group = $ group ;
7776 }
7877
7978 /**
80- * 整理语句
79+ * @param array $having
8180 */
82- public function setQueryCondition (QueryInterface $ query )
83- {
84- foreach ($ this ->condition as $ key => $ value ) {
85- [$ key , $ value ] = $ this ->replaceHandle ($ key , $ value , $ this ->condition ); //解决引用问题
86- /** @var AbstractMethod $rule */
87- foreach ($ this ->methodRules as $ rule ) {
88- $ methodRule = new $ rule ($ query , $ key , $ value );
89- if ($ methodRule ->handle ()) break ;
90- }
91- }
81+ public function setHaving (array $ having ): void
82+ {
83+ $ this ->having = $ having ;
84+ }
85+
86+ /**
87+ * @param int $limit
88+ */
89+ public function setLimit (int $ limit ): void
90+ {
91+ $ this ->limit = $ limit ;
92+ }
93+
94+ /**
95+ * @param int $offset
96+ */
97+ public function setOffset (int $ offset ): void
98+ {
99+ $ this ->offset = $ offset ;
100+ }
101+
102+ /**
103+ * @param array $order
104+ */
105+ public function setOrder (array $ order ): void
106+ {
107+ $ this ->order = $ order ;
108+ }
109+
110+ /**
111+ * @return array
112+ */
113+ public function getColumn (): array
114+ {
115+ return $ this ->column ;
116+ }
117+
118+ /**
119+ * @return array
120+ */
121+ public function getGroup (): array
122+ {
123+ return $ this ->group ;
124+ }
125+
126+ /**
127+ * @return array
128+ */
129+ public function getHaving (): array
130+ {
131+ return $ this ->having ;
132+ }
133+
134+ /**
135+ * @return int
136+ */
137+ public function getLimit (): int
138+ {
139+ return $ this ->limit ;
140+ }
141+
142+ /**
143+ * @return int
144+ */
145+ public function getOffset (): int
146+ {
147+ return $ this ->offset ;
148+ }
149+
150+ /**
151+ * @return array
152+ */
153+ public function getOrder (): array
154+ {
155+ return $ this ->order ;
156+ }
157+
158+ protected function log (array $ condition )
159+ {
160+ $ this ->changeLog [] = [
161+ 'old ' => $ this ->condition ,
162+ 'new ' => $ condition
163+ ];
92164 }
93165}
0 commit comments