PHP Classes

Complex Heart PHP Criteria: Compose several filters using fluent interface

Recommend this page to a friend!
  Info   View files Documentation   View files View files (23)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-02-24 (1 month ago) RSS 2.0 feedNot enough user ratingsTotal: 36 This week: 1All time: 10,919 This week: 140Up
Version License PHP version Categories
php-criteria 1.0Custom (specified...8.1Databases, PHP 8
Description 

Author

This package can compose query conditions from criteria clauses.

It provides several classes that can be used to compose conditions by implementing several types of condition clauses.

Currently, the package supports conditions clauses of type:

- Filter

- Filter group

- Operator

- Order

- Order type

- Page

The Criteria Design Pattern is a potent instrument for crafting an application's adaptable and recyclable query specifications.

When integrated with the tenets of Clean Architecture, this pattern advocates for the segregation of concerns and enhances maintainability.

Please note that this package only provides the Criteria object.

Implementing the Criteria Parser is outside the scope of this package.

You can implement as many criteria parsers as possible, one per database system.

The Criteria object does not contain any references to tables; the repository is responsible for applying it within the Aggregate context.

Innovation Award
PHP Programming Innovation award nominee
March 2024
Nominee
Vote
When developers need to perform queries to databases or object repositories to access data records that match a given criteria, they need to define the conditions the data must match.

For relational databases, developers use SQL to define the record-matching conditions.

Another type of query condition definition needs to be used for other types of object repositories.

This package implements an abstraction to the query condition criteria that uses different objects to define the types of query condition criteria.

This way, the package can define criteria to query different types of object repositories besides relational databases.

Manuel Lemos
Picture of Unay Santisteban
  Performance   Level  
Name: Unay Santisteban <contact>
Classes: 7 packages by
Country: Spain Spain
Innovation award
Innovation award
Nominee: 3x

Winner: 1x

Documentation

Criteria

Test Quality Gate Status Coverage

Small implementation of a criteria pattern in PHP for Complex Heart SDK. Compose several filters using fluent interface.

Installation

Just install the package from Packagist using composer:

composer require complex-heart/criteria

Usage

Import the class and use the fluent interface:

namespace ComplexHeart\Domain\Criteria;

// Match the users with status active and more than 7k followers and from Spain and France
$g1 = FilterGroup::create()        
    ->addFilterEqual('status', 1)
    ->addFilterGreaterThan('followers', 7000)
    ->addFilterIn('country', ['es', 'fr']);

$criteria = Criteria::default()
    ->withFilterGroup($g1)
    ->withOrderBy('surname')
    ->withOrderType('asc')
    ->withPageLimit(25)
    ->withPageOffset(50);

$users = $repository->match($criteria);

// alternative, same as above
$criteria = Criteria::default()
    ->withFilterGroup(FilterGroup::create()
        ->addFilterEqual('status', 1)
        ->addFilterGreaterThan('followers', 7000)
        ->addFilterIn('country', ['es', 'fr']))
    ->withOrderBy('surname')
    ->withOrderType('asc')
    ->withPageLimit(25)
    ->withPageOffset(50);

// In SQL, we may have something like:
// WHERE status = 1 AND followers >= 700 AND country in ('es', 'fr')

$users = $repository->match($criteria);

A FilterGroup is a set of filters or conditions that must match all together (AND). To match one group or another (OR), just add more FilterGroup.

// Match articles with given term in title, or in tagline, or in content.
$criteria = Criteria::default()
    ->withFilterGroup(FilterGroup::create()->addFilterContains('title', $term))
    ->withFilterGroup(FilterGroup::create()->addFilterContains('tagline', $term))
    ->withFilterGroup(FilterGroup::create()->addFilterContains('content', $term))
    ->withOrderBy('created_at')
    ->withOrderType(Order::TYPE_ASC)
    ->withPageNumber(3);


  Files folder image Files  
File Role Description
Files folder image.github (1 file, 2 directories)
Files folder imagesrc (7 files, 2 directories)
Files folder imagetests (3 files)
Files folder imagewiki (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation
Accessible without login Plain text file sonar-project.properties Data Auxiliary data

  Files folder image Files  /  .github  
File Role Description
Files folder imageISSUE_TEMPLATE (2 files)
Files folder imageworkflows (2 files)
  Accessible without login Plain text file CODEOWNERS Data Auxiliary data

  Files folder image Files  /  .github  /  ISSUE_TEMPLATE  
File Role Description
  Accessible without login Plain text file bug_report.md Data Auxiliary data
  Accessible without login Plain text file feature_request.md Data Auxiliary data

  Files folder image Files  /  .github  /  workflows  
File Role Description
  Accessible without login Plain text file documentation.yml Data Auxiliary data
  Accessible without login Plain text file test.yml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imageContracts (1 file)
Files folder imageErrors (1 file)
  Plain text file Criteria.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file FilterGroup.php Class Class source
  Accessible without login Plain text file Operator.php Aux. Auxiliary script
  Plain text file Order.php Class Class source
  Accessible without login Plain text file OrderType.php Aux. Auxiliary script
  Plain text file Page.php Class Class source

  Files folder image Files  /  src  /  Contracts  
File Role Description
  Plain text file CriteriaSource.php Class Class source

  Files folder image Files  /  src  /  Errors  
File Role Description
  Plain text file CriteriaError.php Class Class source

  Files folder image Files  /  tests  
File Role Description
  Plain text file CriteriaTest.php Class Class source
  Accessible without login Plain text file FilterGroupTest.php Example Example script
  Accessible without login Plain text file Pest.php Aux. Auxiliary script

  Files folder image Files  /  wiki  
File Role Description
  Accessible without login Plain text file Laravel-Implementation.md Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:36
This week:1
All time:10,919
This week:140Up