44namespace BlueFeather \EloquentFileMaker \Database \Query ;
55
66
7+ use _PHPStan_59fb0a3b2 \Symfony \Component \String \Exception \RuntimeException ;
78use BlueFeather \EloquentFileMaker \Exceptions \FileMakerDataApiException ;
89use DateTimeInterface ;
910use Illuminate \Database \Query \Builder ;
1011use Illuminate \Http \File ;
1112use Illuminate \Http \UploadedFile ;
13+ use Illuminate \Support \Arr ;
1214use Illuminate \Support \Collection ;
1315use InvalidArgumentException ;
1416
@@ -124,6 +126,8 @@ class FMBaseBuilder extends Builder
124126 public $ containerFieldName ;
125127 public $ containerFile ;
126128
129+ protected $ whereIns = [];
130+
127131
128132 /**
129133 * Add a basic where clause to the query.
@@ -160,7 +164,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
160164 // Create a new find array if null
161165 $ count = sizeof ($ this ->wheres );
162166 if ($ count == 0 ) {
163- $ currentFind = collect ([]) ;
167+ $ currentFind = [] ;
164168 } else {
165169 $ currentFind = $ this ->wheres [sizeof ($ this ->wheres ) - 1 ];
166170 }
@@ -183,13 +187,13 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
183187 public function delete ($ id = null ): int
184188 {
185189 // If an ID is passed to the method we will delete the record with this internal FileMaker record ID
186- if (! is_null ($ id )) {
190+ if (!is_null ($ id )) {
187191 $ this ->where ($ this ->defaultKeyName (), '= ' , $ id );
188192 }
189193 $ this ->applyBeforeQueryCallbacks ();
190194
191195 // Check if we have a record ID to delete or if this is a query for a bulk delete
192- if ($ this ->getRecordId () === null ){
196+ if ($ this ->getRecordId () === null ) {
193197 // There's no individual record ID to delete, so do a bulk delete
194198 return $ this ->bulkDeleteFromQuery ();
195199 }
@@ -403,6 +407,8 @@ public function layoutResponse($name): FMBaseBuilder
403407 */
404408 public function get ($ columns = ['* ' ])
405409 {
410+ $ this ->computeWhereIns ();
411+
406412 // Run the query and catch a 401 error if there are no records found - just return an empty collection
407413 try {
408414 $ response = $ this ->connection ->performFind ($ this );
@@ -492,6 +498,58 @@ public function omit($boolean = true): FMBaseBuilder
492498 return $ this ;
493499 }
494500
501+ public function whereIn ($ column , $ values , $ boolean = 'and ' , $ not = false )
502+ {
503+ throw_if ($ boolean === 'or ' , new \RuntimeException ('Eloquent FileMaker does not currently support or within a where in ' ));
504+
505+ $ this ->whereIns [] = [
506+ 'column ' => $ this ->getMappedFieldName ($ column ),
507+ 'values ' => $ values ,
508+ 'boolean ' => $ boolean ,
509+ 'not ' => $ not
510+ ];
511+
512+ return $ this ;
513+ }
514+
515+ protected function computeWhereIns ()
516+ {
517+ // If no where in clauses return
518+ if (empty ($ this ->whereIns )) {
519+ return ;
520+ }
521+
522+ $ whereIns = array_map (function ($ whereIn ) {
523+ $ finds = [];
524+
525+ foreach ($ whereIn ['values ' ] as $ value ) {
526+ $ find = [
527+ $ whereIn ['column ' ] => $ value ,
528+ ];
529+
530+ if ($ whereIn ['not ' ]) {
531+ $ find ['omit ' ] = true ;
532+ }
533+
534+ $ finds [] = $ find ;
535+ }
536+
537+ return $ finds ;
538+ }, $ this ->whereIns );
539+
540+ if (empty ($ this ->wheres )) {
541+ $ this ->wheres = Arr::flatten ($ whereIns , 1 );
542+ return ;
543+ }
544+
545+ $ arr = Arr::crossJoin ($ this ->wheres , ...$ whereIns );
546+ $ function = function ($ conditions ) {
547+ return array_merge (...array_values ($ conditions ));
548+ };
549+
550+ $ this ->wheres = array_map ($ function , $ arr );
551+ }
552+
495553 /**
496554 * Retrieve the minimum value of a given column.
497555 *
@@ -621,6 +679,8 @@ public function update(array $values)
621679
622680 $ this ->fieldData ($ values );
623681
682+ $ this ->computeWhereIns ();
683+
624684 return $ this ->connection ->update ($ this );
625685 }
626686
0 commit comments