PHP code example of dex / curio

1. Go to this page and download the library: Download dex/curio library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

dex / curio example snippets


use Dex\Laravel\Curio\Query\PaginateQuery;

class PostQuery extends PaginateQuery
{
    public function filterBy(): array
    {
        return [
            'title' => ['string'],
            'published_at' => ['date'],
            'author_id' => ['integer'],
        ];
    }

    public function sortBy(): array
    {
        return ['id', 'title', 'published_at', 'author.name'];
    }

    public function 

use Dex\Laravel\Curio\Extensions\PaginateRequest;
use Illuminate\Foundation\Http\FormRequest;

class PostPaginateRequest extends FormRequest
{
    use PaginateRequest;

    public function getPaginateQuery(): string
    {
        return PostQuery::class;
    }
}

class PostPaginateController
{
    public function __invoke(PostPaginateRequest $request): PostResourceCollection
    {
        return new PostResourceCollection(
            $request->paginate(Post::query())
        );
    }
}

Route::get('posts', PostPaginateController::class);

use Dex\Laravel\Curio\Language\Preset;

Preset::register('active', 'status:active');
Preset::register('vip', '@active plan:premium'); // presets can reference other presets
Preset::register('year', fn (string $year) => "created_at:{$year}-01-01..{$year}-12-31");

public function defaultFilter(): string
{
    return 'author_id:$author_id!';
}

public function searchBy(): array
{
    return ['title', 'content'];
}

public function searchMode(): string
{
    return 'trgm';
}

public function castBy(): array
{
    return ['ranking'];
}

public function castMutator(): array
{
    return [
        'rankingTier' => [10 => 'bronze', 20 => 'silver'],
    ];
}

use Dex\Laravel\Curio\Extensions\AggregateResource;

class AuthorResource extends JsonResource
{
    use AggregateResource;

    public function toArray(Request $request): array
    {
        return [
            'id' => $this->id,
            'ranking' => $this->ranking,
        ];
    }
}

public function replaceBy(): array
{
    return [
        'author' => 'author.name',      // filter=author:jane / sort=author
        'authorName' => 'author.name',
        'date-of-birth' => 'date_of_birth',  // also resolves in select=/aggregate=/cast=
    ];
}

use Dex\Laravel\Curio\YourCuriosity;

class Post extends Model
{
    use YourCuriosity;

    public function filterBy(): array
    {
        return [
            'title' => ['string'],
            'published_at' => ['date'],
        ];
    }

    public function sortBy(): array
    {
        return ['title', 'published_at'];
    }
}

Post::curio()->filter('title:*laravel*')->sort('-published_at')->get();

Post::curio()->filter('content:*laravel*')->get(); // ValidationException: The field 'content' is not allowed on query filter.

use Dex\Laravel\Curio\Contracts\Searchable;
use Dex\Laravel\Curio\Query\SearchBy;

class Post extends Model implements Searchable
{
    use SearchBy;
    use YourCuriosity;

    public function searchBy(): array
    {
        return ['title'];
    }
}

Post::curio()->filter('laravel')->get(); // free text, LIKE across title

// config/curio.php
return [
    'query' => [
        'aggregate' => 'aggregate',
        'cast' => 'cast',
        'filter' => 'filter',
        'efault_page_number' => 1,
        'default_page_size' => 25,
        'default_max_page_size' => 250,
    ],

    'filter' => [
        // Maximum number of clauses in `filter=`, counted after presets are expanded.
        'max_clauses' => 50,
        // Maximum number of relations a filter key may go through.
        'max_depth' => 3,
    ],

    'sort' => [
        // Maximum number of fields in `sort=`.
        'max_fields' => 5,
    ],

    'aggregate' => [
        // Maximum number of items in `aggregate=`.
        'max_items' => 10,
    ],

    '

use Dex\Laravel\Curio\Events\ApplyPending;
use Illuminate\Support\Facades\Event;

Event::listen(ApplyPending::class, function (ApplyPending $event) {
    Log::info('curio query', [
        'model' => $event->builder->getModel()::class,
        'types' => array_unique(array_column($event->tokens, 'type')),
    ]);
});