PHP code example of vestin / logic-checker

1. Go to this page and download the library: Download vestin/logic-checker 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/ */

    

vestin / logic-checker example snippets


use Vestin\Checker\CheckNotPassException;

class MathAddChecker implements CheckerInterface
{

    private $paramOne;
    private $paramTwo;
    private $result;

    public function __construct($paramOne, $paramTwo, $result)
    {
        $this->paramOne = $paramOne;
        $this->paramTwo = $paramTwo;
        $this->result = $result;
    }


    public function check() {
        if( ($this->paramOne + $this->paramTwo) != $this->result){
            throw new CheckNotPassException('calc error');
        }
    }

}



$dispatcher = new \Vestin\Checker\Dispatchers\SimpleDispatcher();
$checkerBus = new \Vestin\Checker\CheckerBus($dispatcher);

$checker = new MathAddChecker(1, 2, 3); // 1+2=3
$checker2 = new MathAddChecker(2, 3, 5); // 2+3=5

$checkerBus->addChecker($checker)
           ->addChecker($cherker2);
    
if($checkerBus->check()){
    // check pass
    echo 'check passed'; // this will called
}else{
    // check not pass
    $error = $checkerBus->getError();
}



$dispatcher = new \Vestin\Checker\Dispatchers\SimpleDispatcher();
$checkerBus = new \Vestin\Checker\CheckerBus($dispatcher);

$checker = new MathAddChecker(1, 2, 3); // 1+2=3
$checker2 = new MathAddChecker(2, 3, 6); // 2+3=5 this is wrong

$checkerBus->addChecker($checker)
           ->addChecker($cherker2);
    
if($checkerBus->check()){
    // check pass
    // echo 'check passed';
}else{
    // check not pass
    // this will called
    $error = $checkerBus->getError(); // $error will be a string 'calc error';
}