2017 © Pedro Peláez
 

library laravel-criteria

Minimal interface for criteria pattern

image

baethon/laravel-criteria

Minimal interface for criteria pattern

  • Friday, June 29, 2018
  • by radmen
  • Repository
  • 1 Watchers
  • 0 Stars
  • 63 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

baethon/laravel-criteria

Minimal implementation of criteria pattern for Laravel., (*1)

What is criteria?

Criteria is an implementation of \Baethon\LaravelCriteria\CriteriaInterface which is responsible for only one thing - modify Laravels query in a specified way., (*2)

This allows to decouple and re-use query modifiers., (*3)

Criteria are very similar to Eloquent scopes. The main difference is that they can be applied to different models across the application., (*4)

Installation

composer require baethon/laravel-criteria

Requirements

  • PHP >= 7.1

Laravel compatibility

This library has no dependencies to Laravel itself. It should work with all versions of Laravel., (*5)

Usage

First, you need to create an implementation of CriteriaInterface:, (*6)

<?php

use Baethon\LaravelCriteria\CriteriaInterface;

class CompareCriteria implements CriteriaInterface
{
    private $field;

    private $value;

    public function __construct(string $field, string $value)
    {
        $this->field = $field;
        $this->value = $value;
    }

    public function apply($query)
    {
        $query->where($this->field, $this->value);
    }
}

Now, you can apply it to the query:, (*7)

$query = User::query();

(new CompareCriteria('name', 'Jon'))->apply($query);

$jon = $query->first();

AppliesCriteria trait

To simplify things it's possible to use AppliesCriteria trait in a model., (*8)

use Baethon\LaravelCriteria\Traits\AppliesCriteria;

class User extends Model
{
    // model body stripped for better readability

    use AppliesCriteria;
}

$jon = User::query()
    ->apply(new CompareCriteria('name', 'Jon'))
    ->first();

Collections

The package provides collections which allow applying multiple criteria at once., (*9)

To apply group of criteria use \Baethon\LaravelCriteria\Collections\CriteriaCollection:, (*10)

$jonSnow = User::query()
    ->apply(CriteriaCollection::create([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]))
    ->first();

// same result, without CriteriaCollection
$jonSnow = User::query()
    ->apply(new CompareCriteria('name', 'Jon'))
    ->apply(new CompareCriteria('lastname', 'Snow'))
    ->first();

If you need to be sure that all criteria will be fulfilled you can use CriteriaCollection::allOf():, (*11)

User::query()
    ->apply(CriteriaCollection::allOf([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]));

// same as
User::query()
    ->where(function ($query) {
        $query->apply(new CompareCriteria('name', 'Jon'))
            ->apply(new CompareCriteria('lastname', 'Snow'));
    });

Also, you can group criteria using logical OR join:, (*12)

User::query()
    ->apply(CriteriaCollection::oneOf([
        new CompareCriteria('name', 'Jon'),
        new CompareCriteria('lastname', 'Snow'),
    ]));

// same as
User::query()
    ->where(function ($query) {
        $query->where('name', 'Jon')
            ->orWhere('lastname', 'Snow');
    });

Testing

Run tests with:, (*13)

./vendor/bin/phpunit

Changelog

Please see CHANGELOG for more information what has changed recently., (*14)

License

The MIT License (MIT). Please see License File for more information., (*15)

The Versions

29/06 2018

dev-master

9999999-dev

Minimal interface for criteria pattern

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Radoslaw Mejer

29/06 2018

1.0.2

1.0.2.0

Minimal interface for criteria pattern

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Radoslaw Mejer

28/06 2018

1.0.1

1.0.1.0

Minimal interface for criteria pattern

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by Radoslaw Mejer

28/06 2018

1.0.0

1.0.0.0

Minimal interface for criteria pattern

  Sources   Download

MIT

The Development Requires

by Radoslaw Mejer