PHP Classes

Complex Heart PHP Criteria: Compose several filters using fluent interface

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2024-02-24 (7 months ago) RSS 2.0 feedNot enough user ratingsTotal: 42 All time: 10,853 This week: 73Up
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 winner
March 2024
Winner
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 is available for providing paid consulting. Contact Unay Santisteban .
Classes: 7 packages by
Country: Spain Spain
Age: 35
All time rank: 73316 in Spain Spain
Week rank: 22 Up1 in Spain Spain Up
Innovation award
Innovation award
Nominee: 3x

Winner: 2x

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 (23)  
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 (23)  /  .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 (23)  /  .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 (23)  /  .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 (23)  /  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 (23)  /  src  /  Contracts  
File Role Description
  Plain text file CriteriaSource.php Class Class source

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

  Files folder image Files (23)  /  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 (23)  /  wiki  
File Role Description
  Accessible without login Plain text file Laravel-Implementation.md Data Auxiliary data

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:42
This week:0
All time:10,853
This week:73Up