PHPackages                             tklein/php-combine-conditions - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. tklein/php-combine-conditions

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

tklein/php-combine-conditions
=============================

Generic representation of conditional structure/tree library for PHP.

0.2.0(4y ago)12241[1 issues](https://github.com/thomas-kl1/php-combine-conditions/issues)[2 PRs](https://github.com/thomas-kl1/php-combine-conditions/pulls)MITPHPPHP ^8.1CI failing

Since Mar 12Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/thomas-kl1/php-combine-conditions)[ Packagist](https://packagist.org/packages/tklein/php-combine-conditions)[ Docs](https://github.com/thomas-kl1/php-combine-conditions)[ Fund](https://paypal.me/thomaskl1)[ Patreon](https://www.patreon.com/thomas_klein)[ RSS](/packages/tklein-php-combine-conditions/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (5)Dependencies (7)Versions (7)Used By (0)

PHP Combine Conditions
======================

[](#php-combine-conditions)

[![Codacy Badge](https://camo.githubusercontent.com/23cb06cfadaee3ff96ac2711ae30ce567ae59ace2a42020e5b6f715a2fed8ffc/68747470733a2f2f696d672e736869656c64732e696f2f636f646163792f67726164652f34376433623265343262646234316630613162373133613331336465613837302f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://app.codacy.com/app/thomas-klein/php-combine-conditions)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/4374722feb7134fc35b570c8358905138985de7c8000eb421fa4d8a6d0a7b1da/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f74686f6d61732d6b6c312f7068702d636f6d62696e652d636f6e646974696f6e732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/thomas-kl1/php-combine-conditions/?branch=master)[![Code Climate](https://camo.githubusercontent.com/af1e232ce4a5387efe79fe0b90c42bf35a1858ad30183cf481c537b38ffca69b/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636c696d6174652f6d61696e7461696e6162696c6974792f74686f6d61732d6b6c312f7068702d636f6d62696e652d636f6e646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://codeclimate.com/github/thomas-kl1/php-combine-conditions/maintainability)[![Latest Stable Version](https://camo.githubusercontent.com/61540b0ef2665984766fe14200a4f5e4da3dab5928ac49ed2809acf89e3f5bab/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f746b6c65696e2f7068702d636f6d62696e652d636f6e646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tklein/php-combine-conditions)[![PHP from Packagist](https://camo.githubusercontent.com/83f55029f1a2b830301c70a83858d8ee2062c54d248f40feeb66390bba1dd5d9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f746b6c65696e2f7068702d636f6d62696e652d636f6e646974696f6e732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/tklein/php-combine-conditions)[![License: MIT](https://camo.githubusercontent.com/b1f201aceec51e1d856fdc58546d4014fac079562bcf44e5e07a3e5153d5457d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f74686f6d61732d6b6c312f7068702d636f6d62696e652d636f6e646974696f6e732e7376673f7374796c653d666c61742d737175617265)](./LICENSE)

Generic representation of conditional structure/tree library for PHP.

- [Installation](#installation)
- [Getting Started](#getting-started)
    - [Main Library Entrance](#main-library-entrance)
    - [OperatorPool](#operatorpool)
        - [Comparator Operator](#comparator-operator)
        - [Logical Operator](#logical-operator)
        - [Override/Add new Operators](#override/add-new-operators)
    - [Condition and Combine](#condition-and-combine)
        - [Condition](#condition)
        - [Combine](#combine)
    - [Execute the Combine Conditions](#execute-the-combine-conditions)
    - [Export the Combine Conditions to Format](#export-the-combine-conditions-to-format)
- [Examples](#examples)
- [Running the tests](#running-the-tests)
- [Contributing](#contributing)
- [Authors](#authors)
- [License](#license)

Installation
------------

[](#installation)

### Composer (recommended)

[](#composer-recommended)

This package is available through Composer/Packagist:

```
composer require tklein/php-combine-conditions

```

Getting Started
---------------

[](#getting-started)

The combine conditions library for PHP is a tool which helps you to build dynamically structured (as tree or graph) conditions.
These conditions are able to be combined and customized operators can be provided.
This library can be used in many context, to execute advanced combination of conditions, to generate some complex SQL conditions, or to be exported to multiple formats as JSON, XML, FULLTEXT...
An other case to use this library is to execute and process frontend conditional tree (Eg: a user build his conditional tree and would like to process the result).

The library has been written in order to allows you to implement it to the right way you want. So, if you implement the interfaces from the public API, you can build your own combine conditions structure and continue to execute it via the public API services.

The library is written to follow the PSR12 coding standards.

### Main Library Entrance

[](#main-library-entrance)

For a simply use, you'll only need to use the following class: `\LogicTree\LogicTreeFacade`.
This class allows you to use the mainly features of the library, as to build a combined conditions, to execute some, and exports them.

### OperatorPool

[](#operatorpool)

What's an `OperatorPool`? It's a class which regroup the whole operator instances used in the library.
You can register or retrieve `OperatorInterface` from this class. The goal is to use them in your combination of conditions.
The operators are divided in two large groups:

- The comparator operator.
- The logical operator.

#### Comparator Operator

[](#comparator-operator)

The comparator operator is used to determine the result of a boolean expression.
*Eg:
Does `'A'` and `'B'` are the same thing? I can test with an equality operator: `'A' == 'B'`).*

The default comparator operators are available in: `src/Operator/Comparator`.
It's used in the `ConditionInterface` definition, known as the `Condition` object.

#### Logical Operator

[](#logical-operator)

The logical operator is used to commute many boolean expression (at least two).
*Eg:
`'A' != 'B'` result should be the same as `'C' != 'D'`.
I can check with the following expression `'A' != 'B' AND 'C' != 'D'`.*

The default logical operators are available in: `src/Operator/Logical`.
It's used in the `CombineInterface` definition, known as the `Combine` object.

#### Override/Add new Operators

[](#overrideadd-new-operators)

The library allows you to override the default operators and/or to provide new ones:

- You should instantiate the `OperatorPool` class.
- Create your operators. They must implement the interface `OperatorInterface`.
- Add your operators (two available types: `logical` and `comparator`) to the `OperatorPool` object.
- Finally, pass the `OperatorPool` object in the construct of the `ConditionManager` class.

*Eg: In the following example, we override the default comparators with ours and add a new one.*

```
$operatorPool = new \LogicTree\Operator\OperatorPool();

$operatorPool->addOperator(
    \LogicTree\Operator\OperatorType::Logical,
    \LogicTree\Operator\Logical\LogicalOperatorConverter::CODE,
    new \My\Class\AndOperator()
);
$operatorPool->addOperator(
    \LogicTree\Operator\OperatorType::Comparator,
    \LogicTree\Operator\Comparator\EqOperator::CODE,
    new \My\Class\EqOperator()
);
$operatorPool->addOperator(
    \LogicTree\Operator\OperatorType::Comparator,
    'my_custom_operator',
    new \My\Class\MyCustomOperator()
);

$conditionManager = new \LogicTree\Service\ConditionManager($operatorPool);
```

Now we are able to use these operators in our code and are available everywhere in the library.

### Condition and Combine

[](#condition-and-combine)

These class always implement the `NodeInterface`. It represent the base model of the library.
Because of the composition pattern, you may want to know if the current object is the root or a node, you should use the following methods:

- `hasParent()` : check if the current object has parent or not.
- `getParent()` : retrieve the current object's parent.
- `ConditionInterface` : is an instance of, represent the last/final node.
- `CombineInterface` : is an instance of, represent the container node.

#### Condition

[](#condition)

`ConditionInterface`: it represents an expression with a comparator operator.
The interface is: `\LogicTree\Model\ConditionInterface`.
The concrete default class is: `\LogicTree\Model\Condition`.
It requires a mixed value to compared by an operator to a value from the data source.

The operator is a `string` which is the code of the wanted operator. The first value is a `string` which is the identifier of the value from the `DataSource` object.
The value to compare must be a `mixed` value.

#### Combine

[](#combine)

`Combine`: it represents an expression with a logical operator.
The interface is: `\LogicTree\Model\CombineInterface`. The concrete default class is: `\LogicTree\Model\Combine`.
It requires at least two conditions and/or combines to commute with a logical operator.

The operator is a `string` which is the code of the wanted operator.
The conditions/combine must be an instance of `ConditionInterface`.

You can add a `NodeInterface` to the `CombineInterface` with the following methods:

- `addChild(NodeInterface $node)` : add a new node child to the parent node object.
- `setChildren(array $nodes)` : replace the children by the new ones.

If you want to invert the result of the combination of conditions, you can specify to the `CombineInterface`object that the result should actually be inverted:

- `setIsInvert(bool $isInvert)`.

### Execute the Combine Conditions

[](#execute-the-combine-conditions)

In order to execute the combined conditions, you must use the `\LogicTree\Service\ConditionManager` class.
This service allows to perform many actions on your `NodeInterface` object.
*Actually only the `execute` method is available from the public API.*
Anyway, the `execute` method allows you to determine the final result of your combination of conditions.

### Export the Combine Conditions to Format

[](#export-the-combine-conditions-to-format)

*No documentation available yet!*

Examples
--------

[](#examples)

Examples of code usage cases are available in this [file](./examples/index.php).

Running the tests
-----------------

[](#running-the-tests)

The tests are placed in the `tests` directory, at the root of the library.

*No tests available yet!*

Contributing
------------

[](#contributing)

Any help is welcome, feel free to raise issues or to open pull requests. Further details in the [CONTRIBUTING.md](.github/CONTRIBUTING.md).

### TODO List

[](#todo-list)

Implement the following comparator operators:

```
  - array("from" => $fromValue, "to" => $toValue)
  - array("like" => $likeValue)
  - array("moreq" => $moreOrEqualValue)
  - array("finset" => $valueInSet)
  - array("seq" => $stringValue)
  - array("sneq" => $stringValue)

```

ToString methods, in order to render the combine conditions in full text / different formats.
Have to: AdapterPool (toFormat) - ConverterPool (getSymbol, getExpression, getFullExpression).
Possibility to add custom type node and process them in the services.
Study Namespace and Lib Name: [trends](https://trends.google.fr/trends/explore?q=filter%20criteria,group%20conditions,complex%20conditions)

Todo Builders (setter... getter... then reset)
-&gt; possibility of immutable implementation (do not break it!)

Add tests and examples: from scratch, with and without new operators, and execution (with expected results).

Authors
-------

[](#authors)

- **Thomas Klein** - *Initial work* - [It's me!](https://github.com/thomas-kl1)

See also the list of [contributors](https://github.com/thomas-kl1/php-combine-conditions/contributors) who participated in this project.

License
-------

[](#license)

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

###  Health Score

39

—

LowBetter than 86% of packages

Maintenance52

Moderate activity, may be stable

Popularity14

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 95.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~351 days

Total

5

Last Release

1580d ago

PHP version history (3 changes)0.1.0-alphaPHP ^7.1

0.1.0PHP ^8.0

0.2.0-alphaPHP ^8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/95669?v=4)[Thom](/maintainers/thomasklein)[@thomasklein](https://github.com/thomasklein)

---

Top Contributors

[![thomas-kl1](https://avatars.githubusercontent.com/u/20971693?v=4)](https://github.com/thomas-kl1 "thomas-kl1 (67 commits)")[![alphaitai](https://avatars.githubusercontent.com/u/39675014?v=4)](https://github.com/alphaitai "alphaitai (2 commits)")[![codacy-badger](https://avatars.githubusercontent.com/u/23704769?v=4)](https://github.com/codacy-badger "codacy-badger (1 commits)")

---

Tags

combinedcomparator-operatorsconditionconditionalconditionalsconditionsfiltergroupgroup-filterphptree-libraryphpcombinelogicconditionoperator

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/tklein-php-combine-conditions/health.svg)

```
[![Health](https://phpackages.com/badges/tklein-php-combine-conditions/health.svg)](https://phpackages.com/packages/tklein-php-combine-conditions)
```

###  Alternatives

[sebastiaanluca/php-pipe-operator

Method chaining (or fluent expressions) for any value using any method.

288467.7k2](/packages/sebastiaanluca-php-pipe-operator)[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

21421.6k](/packages/imanghafoori-laravel-anypass)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
