diff --git a/src/Exceptions/InvalidFilterException.php b/src/Exceptions/InvalidFilterException.php index 39fa17a84..79f3e6fe5 100644 --- a/src/Exceptions/InvalidFilterException.php +++ b/src/Exceptions/InvalidFilterException.php @@ -1,8 +1,28 @@ getFilters(), 'and') && !property_exists($this->getFilters(), 'or')) + if (! property_exists($this->getFilters(), 'and') && ! property_exists($this->getFilters(), 'or')) throw new InvalidFilterException('root filter configuration is not a rule group'); $query = new QueryGroupBuilder($member->newQuery(), true); // make sure we only allow results of the entity we are checking count $query->where(function (Builder $inner_query) use ($member) { - $inner_query->where($member->getKeyName(),$member->getKey()); + $inner_query->where($member->getKeyName(), $member->getKey()); }); // wrap this in an inner query to ensure it is '(correct_entity_check) AND (rule1 AND/OR rule2)' - $query->where(function ($inner_query){ + $query->where(function ($inner_query) { $this->applyGroup($inner_query, $this->getFilters()); }); @@ -68,9 +71,11 @@ final public function isEligible(Model $member): bool } /** - * Applies a filter group to $query - * @param Builder $query the query to add the filter group to - * @param stdClass $group the filter group configuration + * Applies a filter group to $query. + * + * @param Builder $query the query to add the filter group to + * @param stdClass $group the filter group configuration + * * @throws InvalidFilterException */ private function applyGroup(Builder $query, stdClass $group): void @@ -81,7 +86,7 @@ private function applyGroup(Builder $query, stdClass $group): void foreach ($rules as $rule){ // check if this is a nested group or not - if(property_exists($rule,'path')){ + if(property_exists($rule, 'path')){ $this->applyRule($query_group, $rule); } else { // this is a nested group @@ -93,30 +98,32 @@ private function applyGroup(Builder $query, stdClass $group): void } /** - * Applies a rule to a query group - * @param QueryGroupBuilder $query the query to add the rule to - * @param stdClass $rule the rule configuration + * Applies a rule to a query group. + * + * @param QueryGroupBuilder $query the query to add the rule to + * @param stdClass $rule the rule configuration + * * @throws InvalidFilterException */ private function applyRule(QueryGroupBuilder $query, stdClass $rule): void { // 'is' operator - if($rule->operator === '=' || $rule->operator === '<' || $rule->operator==='>'){ + if($rule->operator === '=' || $rule->operator === '<' || $rule->operator === '>'){ // normal comparison operations need to relation to exist $query->whereHas($rule->path, function (Builder $inner_query) use ($rule) { - $inner_query->where($rule->field,$rule->operator, $rule->criteria); + $inner_query->where($rule->field, $rule->operator, $rule->criteria); }); - } else if ($rule->operator === '<>' || $rule->operator === '!=') { + } elseif ($rule->operator === '<>' || $rule->operator === '!=') { // not equal is special cased since a missing relation is the same as not equal $query->whereDoesntHave($rule->path, function (Builder $inner_query) use ($rule) { $inner_query->where($rule->field, $rule->criteria); }); - } else if($rule->operator === 'contains'){ + } elseif($rule->operator === 'contains'){ // contains is maybe a misleading name, since it actually checks if json contains a value $query->whereHas($rule->path, function (Builder $inner_query) use ($rule) { - $inner_query->whereJsonContains($rule->field,$rule->criteria); + $inner_query->whereJsonContains($rule->field, $rule->criteria); }); } else { - throw new InvalidFilterException(sprintf('Unknown rule operator: \'%s\'',$rule->operator)); + throw new InvalidFilterException(sprintf('Unknown rule operator: \'%s\'', $rule->operator)); } } } diff --git a/src/Models/QueryGroupBuilder.php b/src/Models/QueryGroupBuilder.php index 1ee489c36..e74a8b5e7 100644 --- a/src/Models/QueryGroupBuilder.php +++ b/src/Models/QueryGroupBuilder.php @@ -1,11 +1,32 @@ query = $query; $this->is_and_group = $is_and_group; } @@ -44,9 +65,11 @@ public function getUnderlyingQuery(): Builder } /** - * Either adds a 'where' or 'orWhere' to the query, depending on if it is an AND linked group or not - * @param Closure $callback a callback to add constraints + * Either adds a 'where' or 'orWhere' to the query, depending on if it is an AND linked group or not. + * + * @param Closure $callback a callback to add constraints * @return $this + * * @see Builder::where * @see Builder::orWhere */ @@ -56,14 +79,17 @@ public function where(Closure $callback): QueryGroupBuilder { } else { $this->query->orWhere($callback); } + return $this; } /** - * Either adds a 'whereHas' or 'orWhereHas' to the query, depending on if it is an AND linked group or not - * @param string $relation the relation to check for existence - * @param Closure $callback a callback to add more constraints + * Either adds a 'whereHas' or 'orWhereHas' to the query, depending on if it is an AND linked group or not. + * + * @param string $relation the relation to check for existence + * @param Closure $callback a callback to add more constraints * @return $this + * * @see Builder::whereHas * @see Builder::orWhereHas */ @@ -73,14 +99,17 @@ public function whereHas(string $relation, Closure $callback): QueryGroupBuilder } else { $this->query->orWhereHas($relation, $callback); } + return $this; } /** - * Either adds a 'whereDoesntHave' or 'orWhereDoesntHave' to the query, depending on if it is an AND linked group or not - * @param string $relation the relation to check for absence - * @param Closure $callback a callback to add more constraints + * Either adds a 'whereDoesntHave' or 'orWhereDoesntHave' to the query, depending on if it is an AND linked group or not. + * + * @param string $relation the relation to check for absence + * @param Closure $callback a callback to add more constraints * @return $this + * * @see Builder::whereDoesntHave * @see Builder::orWhereDoesntHave */ @@ -90,6 +119,7 @@ public function whereDoesntHave(string $relation, Closure $callback): QueryGroup } else { $this->query->orWhereDoesntHave($relation, $callback); } + return $this; } -} \ No newline at end of file +}