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 4.x

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

    • Circuits
    • How to install
    • Setup
    • Usage
    • Events
  • 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
    • registerAsApplication()
    • destroy()
  • 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
  • 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
  • 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
        • Conditions
        • Criteria
        • Redirects
        • Timeout
        • Driver Options
        • Driver
      • Http Query Builder

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

      • Http Cookies
      • How to install
      • Usage
  • Properties

    • Properties Overload
    • How to install
    • Usage
    • Naming Convention
    • Properties Visibility
  • 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
    • Json
    • Math
    • Method Helper
    • Populatable
    • Version
You are viewing documentation for an outdated version. It is no longer supported!

Custom Grammar

When the default provided Http Query Grammars prove to be insufficient, then you can choose to create a custom grammar.

  • Extend Existing Grammars
  • From Scratch

Extend Existing Grammars

If you only require a few tweaks, e.g. when you just wish to alter the "limit" and "offset" keywords, then it's probably easiest to just extend one of the existing grammars.

<?php

namespace Acme\Http\Query\Grammars;

use Aedart\Http\Clients\Requests\Query\Grammars\DefaultGrammar;

class MyCustomGrammar extends DefaultGrammar
{
    protected string $limitKey = 'take';

    protected string $offsetKey = 'skip';

    // ... etc
}

Once you have performed your adaptations, simply create a new grammar profile in your configs/http-clients.php and make use of it.

<?php

return [
    // ... previous not shown ...

    'grammars' => [

        'profiles' => [

            'custom' => [
                'driver' => \Acme\Http\Query\Grammars\MyCustomGrammar::class,
                'options' => [

                    // ... remaining not shown ...
                ]
            ],

            // ... remaining not shown ...
        ]
    ]
];

From Scratch

Alternatively, you can also create an entire grammar by inheriting from the Grammar and Identifiers interfaces. You must then implement a compile() method, which handles all the available methods provided by the Http Query Builder. The following example show how you could get started.

<?php

namespace Acme\Http\Query\Grammars;

use Aedart\Contracts\Http\Clients\Requests\Query\Builder;
use Aedart\Contracts\Http\Clients\Requests\Query\Identifiers;
use Aedart\Contracts\Http\Clients\Requests\Query\Grammar;

class MyCustomGrammar implements Grammar,
    Identifiers
{
    public function compile(Builder $builder): string
    {
        $parts = $builder->toArray();
        if (empty($parts)) {
            return '';
        }

        $selects = $parts[self::SELECTS];

        // ... remaining not shown...
    }
}
Edit page
Last Updated: 08/09/2020, 18:52
Contributors: Alin Eugen Deac
Prev
Raw Expressions