-
-
Notifications
You must be signed in to change notification settings - Fork 9
API Reference for the Filter Class
Jerome Thayananthajothy edited this page Apr 23, 2024
·
3 revisions
-
Namespace:
Filterable
BadMethodCallException
Carbon\Carbon
Closure
Illuminate\Contracts\Auth\Authenticatable
Illuminate\Contracts\Cache\Repository as Cache
Illuminate\Database\Eloquent\Builder
Illuminate\Http\Request
Illuminate\Support\Str
Psr\Log\LoggerInterface
The Filter
class provides a mechanism for applying dynamic and user-specific filters to Eloquent query builders. It utilizes HTTP request parameters to determine which filters to apply, supports optional caching for query results, and logs its operations when necessary. This class is intended to enhance query customization and optimize query performance for dynamic data retrieval scenarios.
-
$builder
: Instance ofIlluminate\Database\Eloquent\Builder
to which filters are applied. -
$request
: Current HTTP request used to extract filter parameters. -
$cache
: Optional caching mechanism for storing filtered query results. -
$logger
: Optional logging interface to log operations. -
$filters
: Array of registered filter keys this class can handle. -
$filterables
: Active filters and their respective values extracted from the request. -
$currentFilters
: List of filters currently being applied. -
$cacheExpiration
: Duration in minutes for cache validity. -
$forUser
: Optional authenticated user for user-specific filters. -
$preFilters
: Optional pre-filters for additional query manipulation. -
$options
: Miscellaneous options affecting filter processing. -
$filterMethodMap
: Mapping of filter keys to method names for dynamic invocation.
-
__construct(Request $request, Cache $cache = null, LoggerInterface $logger = null)
: Initializes a new filter instance with request, cache, and logger. -
apply(Builder $builder, ?array $options = [])
: Main method to apply filters to the builder. It handles user-specific filters, pre-filters, and dynamic filters based on active filterables. -
applyForUserFilter()
: Applies filters based on the authenticated user (if set). -
applyPreFilters()
: Applies pre-filters to the builder. -
applyFilterables()
: Applies all active filters to the builder. Utilizes caching if enabled. -
applyFilterable(string $filter, mixed $value)
: Dynamically applies a specified filter to the builder. -
getBuilder()
: Returns the current builder instance. -
setBuilder(Builder $builder)
: Sets the builder instance. -
getFilters()
: Returns all registered filter keys. -
getFilterables()
: Retrieves active filters with their values. -
getCurrentFilters()
: Returns the list of currently applied filters. -
appendFilterable(string $key, mixed $value)
: Adds or updates a filterable value. -
forUser(?Authenticatable $user)
: Sets an authenticated user for filtering. -
registerPreFilters(Closure $callback)
: Registers pre-filters for the query. -
getCacheExpiration()
: Gets the current cache expiration time. -
setCacheExpiration(int $value)
: Sets the cache expiration time. -
getOptions()
: Retrieves additional options for filtering. -
setOptions(array<string, mixed> $options)
: Sets additional options for filtering. -
getLogger()
: Gets the logger instance. -
setLogger(LoggerInterface $logger)
: Sets the logger instance. -
buildCacheKey()
: Constructs a unique cache key for storing results. -
clearCache()
: Clears the cache for the current filter. -
getCacheHandler()
: Returns the cache handler. -
setCacheHandler(Cache $cache)
: Sets the cache handler. -
enableLogging()
,disableLogging()
: Enables/disables logging. -
shouldLog()
: Returns whether logging is enabled. -
enableCaching(bool $useCache = true)
,disableCaching()
: Enables/disables caching. -
shouldCache()
: Returns whether caching is enabled.
The class is designed to be used in scenarios where Eloquent models need to be filtered dynamically based on user input, such as in APIs or complex data-driven applications. It leverages Laravel's Eloquent and caching capabilities to efficiently handle large datasets and complex filtering logic.