1111
1212abstract class BaseFilter implements FilterInterface
1313{
14+ private const LIST_MODE_AND = 'AND ' ;
15+ private const LIST_MODE_OR = 'OR ' ;
16+
1417 protected ModCollection $ collection ;
1518
1619 /**
@@ -33,6 +36,9 @@ abstract class BaseFilter implements FilterInterface
3336 */
3437 private array $ authors = array ();
3538
39+ private string $ tagsMode = self ::LIST_MODE_AND ;
40+ private string $ authorsMode = self ::LIST_MODE_AND ;
41+
3642 public function __construct (ModCollection $ collection )
3743 {
3844 $ this ->collection = $ collection ;
@@ -155,10 +161,61 @@ private function appendTagFilters(array &$filters) : void
155161 return ;
156162 }
157163
158- $ filters [] = sprintf (
159- "%s IN('%s') " ,
164+ $ filters [] = $ this ->renderListMode (
160165 $ this ->getTagsKeyName (),
161- implode ("',' " , array_map ('addslashes ' , $ this ->tags ))
166+ $ this ->tagsMode ,
167+ $ this ->tags
168+ );
169+ }
170+
171+ /**
172+ * @param string $keyName
173+ * @param string $mode
174+ * @param string[] $items
175+ * @return string
176+ */
177+ private function renderListMode (string $ keyName , string $ mode , array $ items ) : string
178+ {
179+ if ($ mode === self ::LIST_MODE_OR ) {
180+ return $ this ->renderListOR ($ keyName , $ items );
181+ } else {
182+ return $ this ->renderListAND ($ keyName , $ items );
183+ }
184+ }
185+
186+ /**
187+ * @param string $keyName
188+ * @param string[] $items
189+ * @return string
190+ */
191+ private function renderListOR (string $ keyName , array $ items ) : string
192+ {
193+ return sprintf (
194+ "%s IN('%s') " ,
195+ $ keyName ,
196+ implode ("',' " , array_map ('addslashes ' , $ items ))
197+ );
198+ }
199+
200+ /**
201+ * @param string $keyName
202+ * @param string[] $items
203+ * @return string
204+ */
205+ private function renderListAND (string $ keyName , array $ items ) : string
206+ {
207+ $ list = array ();
208+ foreach ($ items as $ item ) {
209+ $ list [] = sprintf (
210+ "%s = '%s' " ,
211+ $ keyName ,
212+ addslashes ($ item )
213+ );
214+ }
215+
216+ return sprintf (
217+ "(%s) " ,
218+ implode (' AND ' , $ list )
162219 );
163220 }
164221
@@ -172,10 +229,10 @@ private function appendAuthorFilters(array &$filters) : void
172229 return ;
173230 }
174231
175- $ filters [] = sprintf (
176- "%s IN('%s') " ,
232+ $ filters [] = $ this ->renderListMode (
177233 ModIndex::KEY_MOD_AUTHORS ,
178- implode ("',' " , array_map ('addslashes ' , $ this ->authors ))
234+ $ this ->authorsMode ,
235+ $ this ->authors
179236 );
180237 }
181238
0 commit comments