@@ -16,40 +16,111 @@ public function __construct(Connection $connection, $conditions)
1616
1717 public function where ($ where )
1818 {
19- if (!$ this ->isWhereInConditions ($ where )) {
20- return $ this ;
21- }
22- return parent ::where ($ where );
19+ if (!$ this ->isWhereInConditions ($ where )) {
20+ return $ this ;
21+ }
22+
23+ return parent ::where ($ where );
2324 }
2425
2526 public function andWhere ($ where )
2627 {
27- if (!$ this ->isWhereInConditions ($ where )) {
28- return $ this ;
29- }
28+ if (!$ this ->isWhereInConditions ($ where )) {
29+ return $ this ;
30+ }
31+
32+ if ($ this ->isInCondition ($ where )) {
33+ $ where = $ this ->whereIn ($ where );
34+ if (!$ where ) {
35+ return $ this ;
36+ }
37+ }
38+
3039 return parent ::andWhere ($ where );
3140 }
3241
42+ public function orWhere ($ where )
43+ {
44+ if (!$ this ->isWhereInConditions ($ where )) {
45+ return $ this ;
46+ }
47+
48+ if ($ this ->isInCondition ($ where )) {
49+ $ where = $ this ->whereIn ($ where );
50+ if (!$ where ) {
51+ return $ this ;
52+ }
53+ }
54+
55+ return parent ::orWhere ($ where );
56+ }
57+
3358 public function andStaticWhere ($ where )
3459 {
35- return parent ::andWhere ($ where );
60+ return parent ::andWhere ($ where );
61+ }
62+
63+ protected function whereIn ($ where )
64+ {
65+ $ conditionName = $ this ->getConditionName ($ where );
66+
67+ if (empty ($ this ->conditions [$ conditionName ]) || !is_array ($ this ->conditions [$ conditionName ])) {
68+ return false ;
69+ }
70+
71+ $ this ->conditions [$ conditionName ] = array_unique ($ this ->conditions [$ conditionName ]);
72+
73+ $ marks = array ();
74+
75+ foreach (array_values ($ this ->conditions [$ conditionName ]) as $ index => $ value ) {
76+ $ marks [] = ": {$ conditionName }_ {$ index }" ;
77+ $ this ->conditions ["{$ conditionName }_ {$ index }" ] = $ value ;
78+ }
79+
80+ $ where = str_replace (": {$ conditionName }" , join (', ' , $ marks ), $ where );
81+
82+ return $ where ;
3683 }
3784
3885 public function execute ()
3986 {
40- foreach ($ this ->conditions as $ field => $ value ) {
41- $ this ->setParameter (": {$ field }" , $ value );
42- }
43- return parent ::execute ();
87+ foreach ($ this ->conditions as $ field => $ value ) {
88+ $ this ->setParameter (": {$ field }" , $ value );
89+ }
90+
91+ return parent ::execute ();
92+ }
93+
94+ protected function isInCondition ($ where )
95+ {
96+ $ matched = preg_match ('/\s+(IN)\s+/ ' , $ where , $ matches );
97+
98+ if (empty ($ matched )) {
99+ return false ;
100+ } else {
101+ return true ;
102+ }
44103 }
45104
46- private function isWhereInConditions ($ where )
105+ protected function getConditionName ($ where )
47106 {
48- $ matched = preg_match ('/:([a-zA-z0-9_]+)/ ' , $ where , $ matches );
49- if (empty ($ matched )) {
50- return false ;
51- }
107+ $ matched = preg_match ('/:([a-zA-z0-9_]+)/ ' , $ where , $ matches );
108+
109+ if (empty ($ matched )) {
110+ return false ;
111+ }
112+
113+ return $ matches [1 ];
114+ }
115+
116+ protected function isWhereInConditions ($ where )
117+ {
118+ $ conditionName = $ this ->getConditionName ($ where );
119+
120+ if (!$ conditionName ) {
121+ return false ;
122+ }
52123
53- return array_key_exists ($ matches [ 1 ] , $ this ->conditions ) && !is_null ($ this ->conditions [$ matches [ 1 ] ]);
124+ return array_key_exists ($ conditionName , $ this ->conditions ) && !is_null ($ this ->conditions [$ conditionName ]);
54125 }
55- }
126+ }
0 commit comments