PHPackages                             hdgarau/logic-gate - 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. hdgarau/logic-gate

ActiveComposer-module[Utility &amp; Helpers](/categories/utility)

hdgarau/logic-gate
==================

Set a reference value and compare with another. Evaluate some coditions as tree (AND/OR) and return true/false.

v1.0.3(4y ago)08MITPHP

Since Aug 21Pushed 4y ago1 watchersCompare

[ Source](https://github.com/hdgarau/logic-gate)[ Packagist](https://packagist.org/packages/hdgarau/logic-gate)[ RSS](/packages/hdgarau-logic-gate/feed)WikiDiscussions master Synced yesterday

READMEChangelog (3)Dependencies (2)Versions (6)Used By (0)

logic-gate
==========

[](#logic-gate)

Description:
------------

[](#description)

With LogicGate you can define an evaluable-logic-tree then evaluate a value or filter an array. This could be useful when you need to do one or more complex filters. A LogicRoot object has one o more LogicGate (or any object that implements iEvaluable interface, even another LogicRoot object) objects. You can evaluate a single LogicGate object too.

Use:
----

[](#use)

### LogicGate Operators

[](#logicgate-operators)

There are ford operators allowed:

- LogicGate::OP\_EQ (=)
- LogicGate::OP\_GT (&gt;)
- LogicGate::OP\_LT (&lt;)
- LogicGate::OP\_REGEX (~) You can add LogicGate::OP\_NOT (!) Before it to negate condition. You can use a callable object instead an operator

#### Examples

[](#examples)

```
//Using '!='
$gate1 = new LogicGate(4,LogicGate::OP_NOT . LogicGate::OP_EQ);
$gate1->test(1); //true
$gate1->test(4); //false

//Using '>' in a array
$gate2 = new LogicGate(18,LogicGate::OP_GT);
$arr_filtered = $gate2->filter([21,3,33]); // [0=>21,2=>33]

//Using a function
//the function must have two arguments:
// - the value to check
// - value of gate
$fx = function($val, $ref) { return $val->prop1 == $ref;};
$gate3 = new LogicGate(5,$fx);
$gate3->test((object)['prop1'=>5]); //true

```

### How to use

[](#how-to-use)

- You can define a RootGate (tree) with three different ways:
    - Using methods 'addAND' and 'addOR'
    - Using an array.
    - Using a string.

### Using 'add' methods

[](#using-add-methods)

That is the best way for a healthy order and to do some branches with some different conditions.
You can add a LogicGate or a LogicGateRoot (or any class that implement iIsEvaluable)

#### Example

[](#example)

```
 //two-digit number greater than 45 or one-digit number greater than five
        $gatesRoot = new LogicGatesRoot();
        $gatesRoot->addAND (new LogicGate('^\\d\\d$', LogicGate::OP_REGEX));
        $gatesRoot->addAND (new LogicGate(45, LogicGate::OP_GT));
        $gatesRoot->addOR(new LogicGate(5, LogicGate::OP_GT));
        $gatesRoot->addAND(new LogicGate('^\\d$', LogicGate::OP_REGEX));
        $gatesRoot->filter([11,8,178,88,97]); //[1=>8,3=>88,4=>97]

        $gatesRoot2 = new LogicGatesRoot();
        $gatesRoot2->addAND (new LogicGate('^8', LogicGate::OP_REGEX));
        $gatesRoot2->addAND ($gatesRoot);
        $gatesRoot2->filter([11,8,178,88,97]); //[1=>8,3=>88]

```

### Using an array

[](#using-an-array)

If you have an array with conditions you can make the tree using it.
The keys are:

- operator (optional) default regex
- value (required): value of reference
- next\_gate (required) (OR/AND)

#### Example

[](#example-1)

```
 $fx = function ($el, $ref) { return $el %2 ==0;};
        $arr = [
            ['value' => '^\\d+$', 'next_gate' => 'AND'] ,
            ['operator' => $fx, 'value' => null, 'next_gate' => 'OR'] ,
            ['operator' => LogicGate::OP_GT, 'value' => 60, 'next_gate' => 'anything']
        ];
        $gate = new LogicGatesRoot($arr);
        $gate->test(61);//true
        $gate->test(6);//true
        $gate->test(57);//false

```

### Using an string

[](#using-an-string)

the minimalist mode to make a complex tree is with a strong query.

The rules are:

- {:operator}:"{:value}" {AND/OR}
- support parenthesis query
- operator can be &lt;,&gt;,=,~ or a name function.
- use char prefix ! to negate condition (not operator)
- value only can be a string

#### Example

[](#example-2)

```

        //(~:"2$" OR ~:"comodin) AND (~:"1" OR =:"comodin21") AND !=:"comodin12"
        //(finish with two or contains "comodin") and (contains 1 OR is equal "comodin21")
        $lg = new LogicGatesRoot('(' . LogicGate::OP_REGEX . ':"2$" OR ' . LogicGate::OP_REGEX . ':"comodin") '.
            'AND (' . LogicGate::OP_REGEX . ':"1" OR ' . LogicGate::OP_EQ . ':"comodin23") AND '
            . LogicGate::OP_NOT .  LogicGate::OP_EQ . ':"comodin211"');

        $lg->test('comodin21'); //true
        $lg->test('comodin22'); //false
        $lg->test('comodin12'); //false
        $lg->test('comodin103'); //true
        $lg->test('12'); //true
        $lg->test('comodin211'); //false

```

Examples
--------

[](#examples-1)

- See TestCases for more examples.

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity4

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% 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 ~60 days

Total

4

Last Release

1541d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/b82f1ccca8e4810ca13dc7bb571cc91922964f419aa2e194500e411976ff8ca6?d=identicon)[hgarau2](/maintainers/hgarau2)

---

Top Contributors

[![hdgarau](https://avatars.githubusercontent.com/u/5494311?v=4)](https://github.com/hdgarau "hdgarau (14 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/hdgarau-logic-gate/health.svg)

```
[![Health](https://phpackages.com/badges/hdgarau-logic-gate/health.svg)](https://phpackages.com/packages/hdgarau-logic-gate)
```

PHPackages © 2026

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