PHP code example of andydune / conditional-execution

1. Go to this page and download the library: Download andydune/conditional-execution library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

andydune / conditional-execution example snippets


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
}

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!

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

$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

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

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

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

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

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

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

php composer.phar 

php composer.phar update