PHPackages                             andydune/conditional-execution - 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. andydune/conditional-execution

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

andydune/conditional-execution
==============================

It allows for conditional execution of code fragments more beautiful and testable.

v1.9.0(8y ago)04984MITPHPPHP ^5.6 || ^7CI failing

Since Mar 6Pushed 6y ago1 watchersCompare

[ Source](https://github.com/AndyDune/ConditionalExecution)[ Packagist](https://packagist.org/packages/andydune/conditional-execution)[ Docs](https://github.com/AndyDune/ConditionalExecution)[ RSS](/packages/andydune-conditional-execution/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (1)Versions (14)Used By (4)

ConditionalExecution
====================

[](#conditionalexecution)

[![Build Status](https://camo.githubusercontent.com/c8d286f1e9635cfdef1fcf59767845ea253969809cb326d85b24e91d3c14e28c/68747470733a2f2f7472617669732d63692e6f72672f416e647944756e652f436f6e646974696f6e616c457865637574696f6e2e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AndyDune/ConditionalExecution)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/0186650beea113f878f004379677196aa0c280bd1c01a13c6dbf95a53f367306/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e647964756e652f636f6e646974696f6e616c2d657865637574696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/conditional-execution)[![Total Downloads](https://camo.githubusercontent.com/6567c7116edafda0f2fa8e4219e6bfd64637d6bcc7a9c9390aa3a4eed7c64a3a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e647964756e652f636f6e646974696f6e616c2d657865637574696f6e2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/conditional-execution)

It allows for conditional execution of code fragments more beautiful and testable. Line code with no indentation prevents appearance of errors and improves readability.

Requirements
------------

[](#requirements)

- PHP version &gt;= 5.6
- A candy for your friend in store

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

[](#installation)

Installation using composer:

```
composer require andydune/conditional-execution

```

Or if composer was not installed globally:

```
php composer.phar require andydune/conditional-execution

```

Or edit your `composer.json`:

```
"require" : {
     "andydune/conditional-execution": "^1"
}

```

And execute command:

```
php composer.phar update

```

See a problem
-------------

[](#see-a-problem)

Here condition for execution. I meet something like this it in many CMS ofter:

```
if ((empty($arParams["PAGER_PARAMS_NAME"]) || !preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["PAGER_PARAMS_NAME"]))
    && $arParams["SECTION_ID"] > 0 && $arParams["SECTION_ID"]."" != $arParams["~SECTION_ID"]
)
{
	// some php code
	// fuction call or method
}
```

It's difficult to read, search error or edit.

This is better way library offers:

```
use AndyDune\ConditionalExecution\ConditionHolder;

$instanceOr = new ConditionHolder();
$instanceOr->bindOr()
->add(empty($arParams["PAGER_PARAMS_NAME"]))
->add(!preg_match("/^[A-Za-z_][A-Za-z01-9_]*$/", $arParams["PAGER_PARAMS_NAME"]));

$instanceTotal =  = new ConditionHolder(); // default bind AND
$instanceTotal->executeIfTrue(function(){
	// some php code
	// fuction call or method
});
$instanceTotal->add($instanceOr)
->add($arParams["SECTION_ID"] > 0)
->add($arParams["SECTION_ID"]."" != $arParams["~SECTION_ID"]);

$result = $instanceTotal->doIt(); // yes, do it!
```

Methods
-------

[](#methods)

### add($condition)

[](#addcondition)

It adds condition to a queue. Condition is not check immediately. It can be callable.

### bindAnd()

[](#bindand)

Change bind of conditions to AND logic. AND is used as default.

### bindOr()

[](#bindor)

Change bind of conditions to OR logic.

### check()

[](#check)

It executes check of all collected conditions.

### doIt()

[](#doit)

It checks of all collected conditions and execute appropriate function and triggers.

Benefit
-------

[](#benefit)

Simple add or delete conditions
-------------------------------

[](#simple-add-or-delete-conditions)

You don't need to count brackets. Add, remove conditions is simple.

```
use AndyDune\ConditionalExecution\ConditionHolder;

$instance = new ConditionHolder();
$instance->add($val1 > $val2);
$instance->add($someObject->isGood());

$instance->check(); // true

$instance->add('');
$instance->check(); // false

$instance->bindOr();
$instance->check(); // true
```

Closure as condition and params
-------------------------------

[](#closure-as-condition-and-params)

You can use closure as condition. Function can receive params witch was inserted with `doIt` or `check` methods.

```
$instance = new ConditionHolder();
$instance->add(function ($value) {
    if ($value > 2) {
        return true;
    }
    return false;
});
$instance->executeIfTrue(function () {
    return 'Y';
});

$instance->executeIfFalse(function () {
    return 'N';
});

$instance->doIt(3); // returns 'Y'
$instance->chack(3); // returns true
$instance->doIt(1); // returns 'N'
$instance->chack(1); // returns false
```

Check classes for checks of any complexity
------------------------------------------

[](#check-classes-for-checks-of-any-complexity)

There is mechanic for check of any complexity. It is describes as instance of `AndyDune\ConditionalExecution\Check\CheckAbstract`.

You may create your own custom class and use it.

### ArrayValueWithKeyNotEmpty

[](#arrayvaluewithkeynotempty)

It checks array value with key is not empty.

```
use AndyDune\ConditionalExecution\Check\ArrayValueWithKeyNotEmpty;
use AndyDune\ConditionalExecution\ConditionHolder;

// Source array
$array = [
    'one' => 1
];

$condition = new ConditionHolder();
$condition->add(new ArrayValueWithKeyNotEmpty('one'));
$condition->check($array); // result is true
$condition->setNegative();
$condition->check($array);  // result is false
```

### ArrayHasNotEmptyValueOrKeyNotExist

[](#arrayhasnotemptyvalueorkeynotexist)

Given array key must not exist or keep value *== true*

```
use AndyDune\ConditionalExecution\Check\ArrayHasNotEmptyValueOrKeyNotExist;
use AndyDune\ConditionalExecution\ConditionHolder;

$array = [
    'one' => 1,
    'two' => '',
    'three' => 0
];

$condition = new ConditionHolder();
$condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('one'))->check($array); // true

$condition = new ConditionHolder();
$condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('two'))->check($array); // false

$condition = new ConditionHolder();
$condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('three'))->check($array); // false

$condition = new ConditionHolder();
$condition->add(new ArrayHasNotEmptyValueOrKeyNotExist('four'))->check($array); // true
```

Execute functions in list for get first result
----------------------------------------------

[](#execute-functions-in-list-for-get-first-result)

```
use AndyDune\ConditionalExecution\GetFirstSuccessResult;
$instance = new GetFirstSuccessResult();
$instance->add(function () {
    return '';
});
$instance->add(function () {
    return 'two';
});

$instance->get(); // resturns 'two'
```

With params:

```
$instance = new GetFirstSuccessResult();
$instance->add(function ($string, $length = 5) {
    if (strlen($string) < $length) {
        return $string . '';
    }
    return false;
});

$instance->get('two', 4); // returns 'two'
$instance->get('onetwo', 4); // returns onetwo>
$instance->get('tw', 2); returns false
```

###  Health Score

31

—

LowBetter than 66% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity65

Established project with proven stability

 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 ~9 days

Total

13

Last Release

2930d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/79da3b2173a2cefb36abc9b4707cf2c633df8f2c748633ccf64186f5c0e7be6c?d=identicon)[AndyDune](/maintainers/AndyDune)

---

Top Contributors

[![AndyDune](https://avatars.githubusercontent.com/u/3772910?v=4)](https://github.com/AndyDune "AndyDune (30 commits)")

---

Tags

executionphpphp7phpmiddleware

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andydune-conditional-execution/health.svg)

```
[![Health](https://phpackages.com/badges/andydune-conditional-execution/health.svg)](https://phpackages.com/packages/andydune-conditional-execution)
```

###  Alternatives

[imanghafoori/laravel-anypass

A minimal yet powerful package to help you in development.

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

PHPackages © 2026

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