2017 © Pedro Peláez
 

library conditional-execution

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

image

andydune/conditional-execution

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

  • Friday, June 22, 2018
  • by AndyDune
  • Repository
  • 1 Watchers
  • 0 Stars
  • 122 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 14 Versions
  • 11 % Grown

The README.md

ConditionalExecution

Build Status Software License Packagist Version Total Downloads, (*1)

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

Requirements

  • PHP version >= 5.6
  • A candy for your friend in store

Installation

Installation using composer:, (*3)

composer require andydune/conditional-execution 

Or if composer was not installed globally:, (*4)

php composer.phar require andydune/conditional-execution

Or edit your composer.json:, (*5)

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

And execute command:, (*6)

php composer.phar update

See a problem

Here condition for execution. I meet something like this it in many CMS ofter:, (*7)

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., (*8)

This is better way library offers:, (*9)

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

add($condition)

It adds condition to a queue. Condition is not check immediately. It can be callable., (*10)

bindAnd()

Change bind of conditions to AND logic. AND is used as default., (*11)

bindOr()

Change bind of conditions to OR logic., (*12)

check()

It executes check of all collected conditions., (*13)

doIt()

It checks of all collected conditions and execute appropriate function and triggers., (*14)

Benefit

Simple add or delete conditions

You don't need to count brackets. Add, remove conditions is simple., (*15)

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

You can use closure as condition. Function can receive params witch was inserted with doIt or check methods., (*16)

$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

There is mechanic for check of any complexity. It is describes as instance of AndyDune\ConditionalExecution\Check\CheckAbstract., (*17)

You may create your own custom class and use it., (*18)

ArrayValueWithKeyNotEmpty

It checks array value with key is not empty., (*19)

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

Given array key must not exist or keep value == true, (*20)

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

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

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

With params:, (*21)

$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

The Versions

22/06 2018

dev-master

9999999-dev https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

22/06 2018

v1.9.0

1.9.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

30/05 2018

v1.8.0

1.8.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

23/05 2018

v1.7.1

1.7.1.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

23/05 2018

v1.7.0

1.7.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

07/05 2018

v1.6.0

1.6.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

09/04 2018

v1.5.0

1.5.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

16/03 2018

v1.4.0

1.4.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

13/03 2018

v1.3.2

1.3.2.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

13/03 2018

v1.3.1

1.3.1.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

13/03 2018

v1.3.0

1.3.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

06/03 2018

v1.2.0

1.2.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

06/03 2018

v1.1.0

1.1.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php

06/03 2018

v1.0.0

1.0.0.0 https://github.com/AndyDune/ConditionalExecution

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

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7

 

The Development Requires

middleware php