https://github.com/softonic/jaxer
PHP Engine Rule to rule them all
https://github.com/softonic/jaxer
Last synced: about 1 year ago
JSON representation
PHP Engine Rule to rule them all
- Host: GitHub
- URL: https://github.com/softonic/jaxer
- Owner: softonic
- License: other
- Created: 2022-03-01T10:57:23.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2022-03-02T10:41:57.000Z (over 4 years ago)
- Last Synced: 2025-03-24T19:39:54.394Z (about 1 year ago)
- Language: PHP
- Size: 13.7 KB
- Stars: 3
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: CODEOWNERS
Awesome Lists containing this project
README
Jaxer
====================
[](https://github.com/softonic/jaxer/releases)
[](LICENSE.md)
[](https://github.com/softonic/jaxer/actions/workflows/php.yml)
[](https://packagist.org/packages/softonic/jaxer)
[](http://isitmaintained.com/project/softonic/jaxer "Average time to resolve an issue")
[](http://isitmaintained.com/project/softonic/jaxer "Percentage of issues still open")
**One rule engine to rule them all**
**Jaxer** allows you to evaluate complex rules in an easy way and know the reason behind the validation result.
Main features
-------------
* Evaluate rules.
* Rules evaluation tracing.
* Provide generic rules:
* [Logical And](https://en.wikipedia.org/wiki/Truth_table#Logical_conjunction_(AND))
* [Logical Or](https://en.wikipedia.org/wiki/Truth_table#Logical_disjunction_(OR))
* [Logical Not](https://en.wikipedia.org/wiki/Truth_table#Logical_negation)
Installation
-------------
You can require the last version of the package using composer
```bash
composer require softonic/jaxer
```
### Usage
The rules are classes that needs to implement `\Softonic\Jaxer\Rules\Rule` interface. They usually get their input
parameters through constructor like for example:
```php
number > 5;
}
/**
* [Tracing] Context information to provide in case to know what happened in the evaluation.
*/
public function getContext(): array
{
return [
'number' => $this->number,
];
}
}
```
As you can see there is a part dedicated to the business logic in the `isValid` method and a `getContext` method that
allows you to track the rule information.
On the other hand, this library provide generic rules, which help to evaluate complex rules.
These logical operation are: [AND](./src/Rules/AndRule.php), [OR](./src/Rules/OrRule.php) and [NOT](./src/Rules/NotRule.php).
#### Example using AndRule, OrRule and NotRule
```php
isValid(); // true
$validation->getContext(); // ['rule' => \Softonic\Jaxer\Rules\OrRule, 'context' => [...], 'evaluated' => true, 'isValid' => true]
```
After a validation is evaluated, you can execute `isValid` as many times you want **without performance impact** because it
is evaluated only the first time it is called.
### Advanced Usage
#### Rules with inner rules
Sometimes you need to create a rule that contain other rules,
like for example [AND](./src/Rules/AndRule.php), [OR](./src/Rules/OrRule.php) and [NOT](./src/Rules/NotRule.php).
Those rules contain other rules that need to be evaluated in validation time.
To evaluate a rule you need to encapsulate it in a `Validation` object. It will decorate the rule adding state and a common context format.
Logical Not Example:
```php
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();
}
}
```
Testing
-------
`softonic/jaxer` has a [PHPUnit](https://phpunit.de) test suite, and a coding style compliance test suite using [PHP CS Fixer](http://cs.sensiolabs.org/).
To run the tests, run the following command from the project folder.
``` bash
$ make tests
```
To open a terminal in the dev environment:
``` bash
$ make debug
```
License
-------
The Apache 2.0 license. Please see [LICENSE](LICENSE) for more information.