public function setFilterTypes(array $allowedTypes) { $this->allowedFilterTypes = []; foreach ($allowedTypes as $index => $type) { if ($type instanceof Filter) { $filter = $type; } else { $filter = new Filter($type); if (!is_numeric($index)) { $filter->setField($index); } } $this->allowedFilterTypes[$filter->getName()] = $filter; } return $this; }
public function applyFilter(Filter $filter, $operator, array $params) { $this->collection->filter(function ($item) use($filter, $operator, $params) { $field = $filter->getField(); switch ($operator) { // Zero-parameter operators case 'empty': return empty($item->{$field}); case 'not-empty': return !empty($item->{$field}); // Single-parameter operators // Single-parameter operators case 'eq': return $item->{$field} == $params[0]; case 'ne': return $item->{$field} != $params[0]; case 'lt': return $item->{$field} < $params[0]; case 'gt': return $item->{$field} > $params[0]; case 'lte': return $item->{$field} <= $params[0]; case 'gte': return $item->{$field} >= $params[0]; case 'starts-with': return preg_match("/^" . preg_quote($params[0]) . "/i", $item->{$field}); case 'ends-with': return preg_match("/" . preg_quote($params[0]) . "\$/i", $item->{$field}); case 'contains': return preg_match("/" . preg_quote($params[0]) . "/i", $item->{$field}); case 'not-starts-with': return !preg_match("/^" . preg_quote($params[0]) . "/i", $item->{$field}); case 'not-ends-with': return !preg_match("/" . preg_quote($params[0]) . "\$/i", $item->{$field}); case 'not-contains': return !preg_match("/" . preg_quote($params[0]) . "/i", $item->{$field}); // Dual-parameter operators // Dual-parameter operators case 'between': return $item->{$field} >= $params[0] && $item->{$field} <= $params[1]; case 'not-between': return $item->{$field} < $params[0] || $item->{$field} > $params[1]; // Unlimited-parameter operators // Unlimited-parameter operators case 'in': foreach ($params as $value) { if ($item->{$field} == $value) { return true; } } return false; case 'not-in': foreach ($params as $value) { if ($item->{$field} == $value) { return false; } } return true; } }); return $this; }
public function applyFilter(Filter $filter, $operator, array $params) { $column = $filter->getField(); $sqlLikeEscapes = ['%' => '\\%', '_' => '\\_']; switch ($operator) { // Zero-parameter operators case 'empty': $this->query->where(function ($query) use($column) { $query->whereIn($column, ['', 0])->orWhereNull($column); }); break; case 'not-empty': $this->query->where(function ($query) use($column) { $query->whereNotIn($column, ['', 0])->whereNotNull($column); }); break; // Single-parameter operators // Single-parameter operators case 'eq': $this->query->where($column, $params[0]); break; case 'ne': $this->query->where(function ($query) use($column, $params) { $query->where($column, '!=', $params[0])->orWhereNull($column); }); break; case 'lt': $this->query->where($column, '<', $params[0]); break; case 'gt': $this->query->where($column, '>', $params[0]); break; case 'lte': $this->query->where($column, '<=', $params[0]); break; case 'gte': $this->query->where($column, '>=', $params[0]); break; case 'starts-with': $this->query->where($column, 'LIKE', strtr($params[0], $sqlLikeEscapes) . '%'); break; case 'ends-with': $this->query->where($column, 'LIKE', '%' . strtr($params[0], $sqlLikeEscapes)); break; case 'contains': $this->query->where($column, 'LIKE', '%' . strtr($params[0], $sqlLikeEscapes) . '%'); break; case 'not-starts-with': $this->query->where(function ($query) use($column, $params, $sqlLikeEscapes) { $query->where($column, 'NOT LIKE', strtr($params[0], $sqlLikeEscapes) . '%')->orWhereNull($column); }); break; case 'not-ends-with': $this->query->where(function ($query) use($column, $params, $sqlLikeEscapes) { $query->where($column, 'NOT LIKE', '%' . strtr($params[0], $sqlLikeEscapes))->orWhereNull($column); }); break; case 'not-contains': $this->query->where(function ($query) use($column, $params, $sqlLikeEscapes) { $query->where($column, 'NOT LIKE', '%' . strtr($params[0], $sqlLikeEscapes) . '%')->orWhereNull($column); }); break; // Dual-parameter operators // Dual-parameter operators case 'between': $this->query->whereBetween($column, $params); break; case 'not-between': $this->query->where(function ($query) use($column, $params, $sqlLikeEscapes) { $query->whereNotBetween($column, $params)->orWhereNull($column); }); break; // Unlimited-parameter operators // Unlimited-parameter operators case 'in': $this->query->whereIn($column, $params); break; case 'not-in': $this->query->whereNotIn($column, $params); break; } return $this; }
public function applyFilter(Filter $filter, $operator, array $params) { $this->query->where($filter->getField(), $operator, $params); return $this; }