4
4
namespace BlueFeather \EloquentFileMaker \Database \Query ;
5
5
6
6
7
+ use _PHPStan_59fb0a3b2 \Symfony \Component \String \Exception \RuntimeException ;
7
8
use BlueFeather \EloquentFileMaker \Exceptions \FileMakerDataApiException ;
8
9
use DateTimeInterface ;
9
10
use Illuminate \Database \Query \Builder ;
10
11
use Illuminate \Http \File ;
11
12
use Illuminate \Http \UploadedFile ;
13
+ use Illuminate \Support \Arr ;
12
14
use Illuminate \Support \Collection ;
13
15
use InvalidArgumentException ;
14
16
@@ -124,6 +126,8 @@ class FMBaseBuilder extends Builder
124
126
public $ containerFieldName ;
125
127
public $ containerFile ;
126
128
129
+ protected $ whereIns = [];
130
+
127
131
128
132
/**
129
133
* Add a basic where clause to the query.
@@ -160,7 +164,7 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
160
164
// Create a new find array if null
161
165
$ count = sizeof ($ this ->wheres );
162
166
if ($ count == 0 ) {
163
- $ currentFind = collect ([]) ;
167
+ $ currentFind = [] ;
164
168
} else {
165
169
$ currentFind = $ this ->wheres [sizeof ($ this ->wheres ) - 1 ];
166
170
}
@@ -183,13 +187,13 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
183
187
public function delete ($ id = null ): int
184
188
{
185
189
// 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 )) {
187
191
$ this ->where ($ this ->defaultKeyName (), '= ' , $ id );
188
192
}
189
193
$ this ->applyBeforeQueryCallbacks ();
190
194
191
195
// 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 ) {
193
197
// There's no individual record ID to delete, so do a bulk delete
194
198
return $ this ->bulkDeleteFromQuery ();
195
199
}
@@ -403,6 +407,8 @@ public function layoutResponse($name): FMBaseBuilder
403
407
*/
404
408
public function get ($ columns = ['* ' ])
405
409
{
410
+ $ this ->computeWhereIns ();
411
+
406
412
// Run the query and catch a 401 error if there are no records found - just return an empty collection
407
413
try {
408
414
$ response = $ this ->connection ->performFind ($ this );
@@ -492,6 +498,58 @@ public function omit($boolean = true): FMBaseBuilder
492
498
return $ this ;
493
499
}
494
500
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
+
495
553
/**
496
554
* Retrieve the minimum value of a given column.
497
555
*
@@ -621,6 +679,8 @@ public function update(array $values)
621
679
622
680
$ this ->fieldData ($ values );
623
681
682
+ $ this ->computeWhereIns ();
683
+
624
684
return $ this ->connection ->update ($ this );
625
685
}
626
686
0 commit comments