PHP Classes

File: wiki/Laravel-Implementation.md

Recommend this page to a friend!
  Classes of Unay Santisteban   PHP Criteria   wiki/Laravel-Implementation.md   Download  
File: wiki/Laravel-Implementation.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: PHP Criteria
Compose several filters using fluent interface
Author: By
Last change:
Date: 6 months ago
Size: 2,007 bytes
 

Contents

Class file image Download

HTTP Request as Criteria source

Let's see a brief example of how to implement a CriteriaSource along with Laravel HTTP Request.

First, create a Form Request to implement the CriteriaSource interface.

php artisan make:request SearchUserRequest

Next, implement the interface:

namespace LaravelMade\Http\Requests;

use ComplexHeart\Domain\Criteria\Contracts\CriteriaSource;
use Illuminate\Foundation\Http\FormRequest;

class SearchUserRequest extends FormRequest implements CriteriaSource
{
    public function filterGroups(): array
    {
        // get the filter groups from the request.
        // you can also return N groups of filters (OR).
        return [$this->input('filters', [])];
    }

    public function orderType(): string
    {
        return $this->input('order', 'none');
    }

    public function orderBy(): string
    {
        return $this->input('orderBy', '');
    }

    public function pageLimit(): int
    {
        return $this->input('limit', 25);
    }

    public function pageOffset(): int
    {
        return $this->input('offset', 0);
    }

    public function pageNumber(): int
    {
        return $this->input('page', 0);
    }
}

Done, now you only need to call the fromSource method of the Criteria object.

Route::get('users', function (SearchUserRequest $request): JsonResponse {
    $criteria = Criteria::fromSource($request);
    
    // use criteria to fetch the users.
});

Additionally, you can add rules to the FormRequest object to ensure the Criteria is properly instantiated. If the Criteria object cannot be instantiated a CriteriaError will be thrown.

Route::get('users', function (SearchUserRequest $request): JsonResponse {
    try {
        $criteria = Criteria::fromSource($request);
    } catch (CriteriaError $e) {
        // handle the Criteria error
    }
    
    // use criteria to fetch the users.
});