AthenaeumAthenaeum
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
Packages
  • next
  • current
  • v9.x
  • v8.x
  • v7.x
  • v6.x
  • v5.x
  • v4.x
  • v3.x
  • v2.x
  • v1.x
Changelog
GitHub
  • Version 5.x

    • Release Notes
    • Upgrade Guide
    • New to this...
    • Origin
  • ACL

    • Introduction
    • How to install
    • Setup
    • Permissions
    • Roles
    • Users
    • Cached Permissions
  • Circuits

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • Collections

    • Collections
    • How to install
    • Summation

      • Summation Collection
      • Items Processor
  • Config

    • Configuration Loader
    • How to install
    • Setup
    • Load Configuration Files
    • Custom File Parsers
  • Console

    • Command and Schedule Registration
    • How to install
    • Setup
    • Commands
    • Schedules
  • Container

    • IoC Service Container
    • How to install
    • Container
    • List Resolver
  • Core

    • Athenaeum Core Application
    • Prerequisite
    • How to install
    • Integration
    • Usage

      • Configuration
      • Service Providers
      • Service Container
      • Events
      • Caching
      • Logging
      • Console
      • Task Scheduling
      • Exception Handling
      • Extending Core Application
      • Testing
  • Database

    • Introduction
    • How to install
    • Models

      • Instantiatable
      • Sluggable
    • Query

      • Criteria (Query Filter)
  • Dto

    • Data Transfer Object (DTO)
    • How to install
    • Create Interface
    • Implement DTO
    • How to use
    • Populate
    • Export
    • Json
    • Serialization
    • Nested DTOs
    • Array DTO
  • Events

    • Register Listeners and Subscribers
    • How to install
    • Setup
    • Listeners
    • Subscribers
  • Filters

    • Search Filter Utilities
    • Prerequisites
    • How to install
    • Setup
    • Processor
    • Filters Builder
    • Predefined Resources

      • Search Processor
      • Sorting Processor
      • Constraints Processor
      • Matching Processor
    • Tip: Create a base builder
  • Http

    • Clients

      • Http Clients
      • How to install
      • Setup
      • Basic Usage
      • Available Methods

        • Fluent Api
        • Protocol Version
        • Base Uri
        • Http Method and Uri
        • Headers
        • Accept & Content-Type
        • Authentication
        • Http Query
        • Payload Format
        • Payload
        • Attachments
        • Cookies
        • Response Expectations
        • Middleware
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Debugging
        • Logging
        • Driver Options
        • Driver
      • Http Query Builder

        • Introduction
        • Select
        • Where
        • Dates
        • Include
        • Pagination
        • Sorting
        • Raw Expressions
        • Custom Grammar
    • Cookies

      • Http Cookies
      • How to install
      • Usage
    • Messages

      • Http Messages
      • How to install
      • Serializers
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • Redmine

    • Redmine Api Client
    • How to install
    • Setup
    • General Usage

      • Supported Operations
      • Fetch list of resources
      • Find
      • Fetch
      • Create new record
      • Update existing record
      • Delete existing record
      • Relations
    • Available Resources

      • Predefined Resources
      • Attachments
      • Enumerations
      • Issue Relations
      • Users
      • User Groups
      • Roles
      • Project Memberships
      • Versions (Milestones)
      • Issue Categories
      • Trackers
  • Service

    • Service Registrar
    • How to install
    • How to use
  • Support

    • Introduction
    • How to install
    • Laravel Aware-of Helpers

      • How to use
      • Enforce Via Interface
      • Custom Default
      • Pros and Cons
      • Available Helpers
    • Aware-of Properties

      • Generator
      • Available Aware-of Helpers
    • Live Templates
  • Testing

    • Introduction
    • How to install
    • Test Cases
    • Testing Aware-of Helpers
  • Utils

    • Introduction
    • How to install
    • Array
    • Duration
    • Json
    • Math
    • Method Helper
    • Populatable
    • Version
  • Validation

    • Introduction
    • How to install
    • Setup
    • Rules

      • Alpha-Dash-Dot
You are viewing documentation for an outdated version. It is no longer supported!

Tip: Create a base builder

You might find it useful to create a "base" builder, for you application. Doing so will allow you to specify common processors and configuration. The following shows a possible abstract builder, using the predefined processors that are available in this package.

  • Example: abstract builder
  • Example: concrete builder

Example: abstract builder

use Aedart\Filters\BaseBuilder as Builder;
use Aedart\Filters\Processors\MatchingProcessor;
use Aedart\Filters\Processors\SearchProcessor;
use Aedart\Filters\Processors\ConstraintsProcessor;
use Aedart\Filters\Processors\SortingProcessor;

abstract class BaseFiltersBuilder extends Builder
{
    public function processors(): array
    {
        return [
            'match' => MatchingProcessor::make(),

            'search' => SearchProcessor::make()
                ->columns($this->searchColumns()),

            'filter' => ConstraintsProcessor::make()
                ->filters($this->filters())
                ->propertiesToColumns($this->propertiesColumnsMap()),

            'sort' => SortingProcessor::make()
                ->sortable($this->sortable())
                ->propertiesToColumns($this->sortingPropertiesColumnsMap())
                ->defaultSort($this->defaultSorting())
                ->force()
        ];
    }

    /**
     * Get list of table columns that the search filter
     * must match search terms against
     *
     * @return string[]
     */
    abstract public function searchColumns(): array;

    /**
     * Get list of allowed filterable properties and
     * their corresponding filter to be used.
     *
     * @return array
     */
    abstract public function filters(): array;

    /**
     * Get map of properties and their corresponding table
     * column name.
     *
     * @return array
     */
    abstract public function propertiesColumnsMap(): array;

    /**
     * Map of properties and their corresponding table column name,
     * to be used for sorting.
     *
     * @see propertiesColumnsMap
     *
     * @return array
     */
    public function sortingPropertiesColumnsMap(): array
    {
        return $this->propertiesColumnsMap();
    }

    /**
     * Get list of sortable properties
     *
     * @return string[]
     */
    public function sortable(): array
    {
        return array_keys($this->filters());
    }

    /**
     * Get the default sorting value to be used, when
     * none is requested.
     *
     * @return string
     */
    abstract public function defaultSorting(): string;
} 

Example: concrete builder

class UsersFiltersBuilder extends BaseFiltersBuilder
{
    public function searchColumns(): array
    {
        return [
            'id',
            'name',
            'email',
        ];
    }

    public function filters(): array
    {
        return [
            'id' => NumericFilter::class,
            'name' => StringFilter::class,
            'email' => StringFilter::class,
            'administrator' => BooleanFilter::class,
            'email_verified_at' => DatetimeFilter::class,
            'created_at' => DatetimeFilter::class,
            'updated_at' => DatetimeFilter::class,
        ];
    }

    public function propertiesColumnsMap(): array
    {
        return [
            'administrator' => 'is_admin'
        ];
    }

    public function defaultSorting(): string
    {
        return 'id desc';
    }
}
Edit page
Last Updated: 05/04/2022, 21:01
Contributors: alin, Alin Eugen Deac