PHP code example of softonic / jaxer

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

    

softonic / jaxer example snippets




use \Softonic\Jaxer\Rules\Rule;

class IsNumberHigherThanFive implements Rule {
    /**
     * We receive the parameters from constructor.
     * You could also use specific setters or direct access to attribute.
     */
    public function __construct(private int $number)
    {
    }

    /**
     * Business logic to be evaluated.
     */
    public function isValid(): bool
    {
        return $this->number > 5;
    }
    
    /**
     * [Tracing] Context information to provide in case to know what happened in the evaluation.
     */
    public function getContext(): array
    {
        return [
            'number' => $this->number,
        ];
    }
}



use Softonic\Jaxer\Rules\AndRule;
use Softonic\Jaxer\Rules\NotRule;
use Softonic\Jaxer\Rules\OrRule;
use Softonic\Jaxer\Rules\Rule;
use Softonic\Jaxer\Validation;

class FalseRule implements Rule {
    public function isValid(): bool
    {
        return false;
    }

    public function getContext(): array
    {
        return [];
    }
}

class TrueRule implements Rule {
    public function isValid(): bool
    {
        return true;
    }

    public function getContext(): array
    {
        return [];
    }
}

$logicalRule = new OrRule( //true
    new AndRule( //false
        new TrueRule(),
        new FalseRule(),
        new TrueRule(),
    ),
    new AndRule( //true
        new NotRule( // true
            new FalseRule()
        ),
        new TrueRule()
    )
);

$validation = new Validation($logicalRule);
$validation->isValid(); // true
$validation->getContext(); // ['rule' => \Softonic\Jaxer\Rules\OrRule, 'context' => [...], 'evaluated' => true, 'isValid' => true]



namespace Softonic\Jaxer\Rules;

use Softonic\Jaxer\Validation;

class NotRule implements Rule
{
    // Validation decorator
    private Validation $validation;

    public function __construct(private Rule $rule)
    {
        // Encapsulate rule inside a validation
        $this->validation = new Validation($rule);
    }

    public function isValid(): bool
    {
        // Validate the rule
        return !$this->validation->isValid();
    }

    public function getContext(): array
    {
        // Get the validation context
        return $this->validation->getContext();
    }
}