Skip to content
This repository was archived by the owner on Aug 27, 2024. It is now read-only.

Commit 124ecde

Browse files
author
Eugene Mosunov
committed
Add ability to use or boolean between filters groups
1 parent 153fcb2 commit 124ecde

File tree

1 file changed

+38
-9
lines changed

1 file changed

+38
-9
lines changed

src/Repositories/Repository.php

+38-9
Original file line numberDiff line numberDiff line change
@@ -431,24 +431,31 @@ protected function getNestedWhereConditions(QueryBuilder $builder, array $criter
431431
$subQuery = $builder->forNestedWhere();
432432
foreach ($criteria as $key => $criterionData) {
433433
switch (true) {
434+
case $criterionData instanceof Criterion:
435+
$criterion = $criterionData;
436+
break;
434437
case is_string($key)
435438
&& ((!is_array($criterionData) && !is_object($criterionData))
436439
|| $criterionData instanceof Carbon):
437440
$criterion = new Criterion([Criterion::ATTRIBUTE => $key, Criterion::VALUE => $criterionData]);
438441
break;
439-
case $criterionData instanceof Criterion:
440-
$criterion = $criterionData;
441-
break;
442-
case is_int($key) && is_array($criterionData) && !empty($criterionData):
443-
$criterion = $this->parseCriterion($criterionData);
444-
break;
442+
case is_int($key) && is_array($criterionData) && $this->isNestedCriteria($criterionData):
443+
$boolean = 'and';
444+
if (isset($criterionData[Criterion::BOOLEAN])) {
445+
$boolean = $criterionData[Criterion::BOOLEAN];
446+
unset($criterionData[Criterion::BOOLEAN]);
447+
}
448+
$subQuery->addNestedWhereQuery(
449+
$this->getNestedWhereConditions($subQuery, $criterionData),
450+
$boolean
451+
);
452+
continue 2;
445453
default:
446-
throw new BadCriteriaException($this);
454+
$criterion = $this->parseCriterion($criterionData);
447455
}
448456

449457
if (!$this->isCriterionValid($criterion)) {
450-
$subQuery->addNestedWhereQuery($this->getNestedWhereConditions($subQuery, $criterionData));
451-
continue;
458+
throw new BadCriteriaException($this);
452459
}
453460

454461
switch ($criterion->operator) {
@@ -472,6 +479,28 @@ protected function getNestedWhereConditions(QueryBuilder $builder, array $criter
472479
return $subQuery;
473480
}
474481

482+
/**
483+
* Shows whether given criterion data is nested.
484+
*
485+
* @param array $criterionData Criterion data to check
486+
*
487+
* @return boolean
488+
*/
489+
protected function isNestedCriteria(array $criterionData): bool
490+
{
491+
$isValid = true;
492+
493+
foreach ($criterionData as $key => $possibleCriterion) {
494+
$isValid = $isValid &&
495+
(
496+
(is_int($key) && is_array($possibleCriterion)) ||
497+
($key === Criterion::BOOLEAN && in_array($possibleCriterion, ['and', 'or']))
498+
);
499+
}
500+
501+
return $isValid;
502+
}
503+
475504
/**
476505
* Transforms criterion data into DTO.
477506
*

0 commit comments

Comments
 (0)