Download the PHP package llpo/mathepa without Composer
On this page you can find all versions of the php package llpo/mathepa. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package mathepa
Short Description Mathpa is a library to evaluate PHP math expressions
License MIT
Homepage https://github.com/llpo/mathepa
Informations about the package mathepa
Mathepa
Mathepa is a parser for mathematical expressions using PHP syntax.
Why?
The idea arises from the need to save mathematical formulas into a database for a later usage in different backend processes.
It's strongly recommended to use Mathepa in both directions, before saving formulas into a database - or any kind of storage - as security constraint and validator, and after fetching, always acting as a proxy to prevent direct usage of eval.
Quick features list
- Express mathematical expression using one line PHP syntax
- Definition of variables to parametrize values
- Ternary operator supported
- Most common math functions are white listed
- Variables can contain complex expressions and reference other variables
- eval function will be called only with valid expressions
- No external dependencies required
How it works?
Mathepa uses the eval function only after expressions are syntactically and grammatically valid, and thus secure. An expression is considered as valid only when:
- Syntax is valid, i.e. all tokens are identified. E.g. "3.1.4" would be invalid
- Grammar is valid, i.e. a literal can't be next to a variable
- Functions are white listed
- Variables contains valid expressions
- No circular references found
Notice Mathepa does not check the number of formal parameters of a function. When the number of parameters mismatches, execution will just fail like in the PHP console:
Calling functions in Mathepa either with wrong number of parameters or wrong syntax will always throw an Exception. In the next examples eval is called because there's no invalid syntax, grammar or unknown function:
Following examples will throw an exception because of syntax error. In the following cases eval is never called:
See units tests for more examples
Examples
Usage of variables
Usage of functions
Complex expressions
The ternary operator in expressions is allowed, but only the long syntax is supported, i.e. (expr1) ? (expr2) : (expr3).
Installation
Development environment with docker
Install development packages:
Open a shell:
Run composer script inside a container:
Direct call of phpunit: