Skip to content

Commit f04328a

Browse files
likeadeckofcardsSmef
authored andcommitted
Update where not logic and add the addArrayOfWheres method
1 parent f1abdb3 commit f04328a

File tree

2 files changed

+68
-21
lines changed

2 files changed

+68
-21
lines changed

src/Database/Query/FMBaseBuilder.php

+59-19
Original file line numberDiff line numberDiff line change
@@ -176,23 +176,21 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
176176
}
177177

178178
// This is an "orWhere" type query, so add a find request and then work from there
179-
if ($boolean === 'or' || $shouldBeOmit) {
179+
if ($boolean === 'or' || ($shouldBeOmit && ! $this->isCurrentFindAnOmit())) {
180180
$this->addFindRequest();
181+
}
181182

182183
if ($shouldBeOmit) {
183184
$this->omit();
184185
}
185-
}
186186

187187
// If the column is an array, we will assume it is an array of key-value pairs
188188
// and can add them each as a where clause. We will maintain the boolean we
189189
// received when the method was called and pass it into the nested where.
190190
//
191191
// If the first value is an array it means the second value is an omit for the whole request
192192
if (is_array($column)) {
193-
foreach ($column as $eachColumn => $eachValue) {
194-
$this->where($eachColumn, $eachValue);
195-
}
193+
$this->addArrayOfWheres($column, $boolean);
196194

197195
return $this;
198196
}
@@ -214,6 +212,19 @@ public function where($column, $operator = null, $value = null, $boolean = 'and'
214212
return $this;
215213
}
216214

215+
protected function addArrayOfWheres($column, $boolean, $method = 'where')
216+
{
217+
foreach ($column as $key => $value) {
218+
if (is_numeric($key) && is_array($value)) {
219+
$this->{$method}(...array_values($value));
220+
} else {
221+
$this->{$method}($key, '', $value, 'and');
222+
}
223+
}
224+
225+
return $this;
226+
}
227+
217228
/**
218229
* Delete records from the database.
219230
*
@@ -572,6 +583,15 @@ protected function getCurrentFind()
572583
return $this->wheres[$this->currentFindRequestIndex];
573584
}
574585

586+
protected function isCurrentFindAnOmit()
587+
{
588+
if ($this->currentFindRequestIndex === -1) {
589+
return false;
590+
}
591+
592+
return Arr::get($this->wheres, "{$this->currentFindRequestIndex}.omit", 'false') === 'true';
593+
}
594+
575595
protected function updateCurrentFind($find)
576596
{
577597
$this->wheres[$this->currentFindRequestIndex] = $find;
@@ -606,11 +626,11 @@ public function whereIn($column, $values, $boolean = 'and', $not = false)
606626
return $this;
607627
}
608628

609-
protected function computeWhereIns()
629+
public function computeWhereIns()
610630
{
611631
// If no where in clauses return
612632
if (empty($this->whereIns)) {
613-
return;
633+
return $this;
614634
}
615635

616636
$whereInRequests = collect($this->whereIns)->mapToGroups(function ($whereIn) {
@@ -643,37 +663,57 @@ protected function computeWhereIns()
643663
});
644664

645665
if ($this->isWheresEmpty()) {
646-
$this->wheres = $whereInRequests->flatten(2)->toArray();
666+
$this->wheres = $whereInRequests->map(function ($whereInRequest) {
667+
return Arr::crossJoin(...$whereInRequest->values());
668+
})->map(function ($findRequest) {
669+
return array_map(function ($clauses) {
670+
return array_merge(...$clauses);
671+
}, $findRequest);
672+
})->flatten(1)->toArray();
647673

648-
return;
674+
return $this;
649675
}
650676

651-
$newWheres = [];
677+
$newWheres = collect([]);
652678

653679
// loop through each where
654680
// If it is an omit, skip it
655681
// If the where in is an omit, skip it
656682
foreach ($this->wheres as $index => $where) {
657-
$whereInValues = $whereInRequests->get($index)?->first() ?? [];
683+
$whereInRequest = $whereInRequests->get($index) ?? [];
658684

659685
if (($where['omit'] ?? 'false') === 'true') {
660-
if (count(array_keys($where)) > 1 || (count(array_keys($where)) === 1 && (collect($whereInValues)->value('omit') ?? 'false') === 'false')) {
661-
$newWheres[] = $where;
686+
if (count(array_keys($where)) > 1 || (count(array_keys($where)) === 1 && (collect($whereInRequest)->value('omit') ?? 'false') === 'false')) {
687+
$newWheres->push($where);
662688

663689
continue;
664690
}
665691
}
666692

667-
if (empty($whereInValues)) {
668-
$newWheres[] = $where;
693+
if (empty($whereInRequest)) {
694+
$newWheres->push($where);
669695
} else {
670-
foreach ($whereInValues as $whereInValue) {
671-
$newWheres[] = array_merge($where, $whereInValue);
672-
}
696+
$newWheres = $newWheres->push(...Arr::crossJoin([$where], ...$whereInRequest->values()));
673697
}
674698
}
675699

676-
$this->wheres = $newWheres;
700+
$this->wheres = $newWheres
701+
->map(function ($findRequest) {
702+
if (Arr::isAssoc($findRequest)) {
703+
return $findRequest;
704+
}
705+
706+
return array_merge(...$findRequest);
707+
})->toArray();
708+
709+
return $this;
710+
}
711+
712+
public function getWheres()
713+
{
714+
$this->computeWhereIns();
715+
716+
return $this->wheres;
677717
}
678718

679719
/**

src/Services/FileMakerConnection.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use GuzzleHttp\Middleware;
1212
use Illuminate\Database\Connection;
1313
use Illuminate\Http\Client\PendingRequest;
14+
use Illuminate\Http\UploadedFile;
1415
use Illuminate\Support\Arr;
1516
use Illuminate\Support\Collection;
1617
use Illuminate\Support\Facades\Cache;
@@ -449,7 +450,7 @@ public function createRecord(FMBaseBuilder $query)
449450
return $response;
450451
}
451452

452-
protected function buildPostDataFromQuery(FMBaseBuilder $query)
453+
public function buildPostDataFromQuery(FMBaseBuilder $query)
453454
{
454455
$postData = [];
455456

@@ -553,14 +554,20 @@ protected function isContainer($field)
553554
}
554555

555556
// if it's an array, it could be a file => filename key-value pair.
556-
// it's a conainer if the first object in the array is a file
557+
// it's a container if the first object in the array is a file
557558
if (is_array($field) && count($field) === 2 && $this->isFile($field[0])) {
558559
return true;
559560
}
560561

561562
return false;
562563
}
563564

565+
protected function isFile($object)
566+
{
567+
return is_a($object, \Illuminate\Http\File::class) ||
568+
is_a($object, UploadedFile::class);
569+
}
570+
564571
public function executeScript(FMBaseBuilder $query)
565572
{
566573
$this->setLayout($query->from);

0 commit comments

Comments
 (0)