A formula-calculating library for php.
You can install this package with composer:
composer require puritandesigns/poindexter
Here are a few usage examples:
use Poindexter\Parsing\Parser;
// Simple arithmetic
Parser::calculate('1 + 1')->getValue(); // returns 2
Parser::calculate('2 * 2')->getValue(); // returns 4
// Comparisons
// returns 1 (truthy)
Parser::calculate('1 < 2')->getValue();
Parser::calculate('2 = 2')->getValue();
// returns 0 (falsy)
Parser::calculate('2 <= 1')->getValue();
Parser::calculate('1 = 2')->getValue();
// Logical Operators (returns binary value)
// and
Parser::calculate('1 & 1')->getValue(); // returns 1
Parser::calculate('1 & 0')->getValue(); // returns 0
Parser::calculate('0 & 0')->getValue(); // returns 0
// or
Parser::calculate('1 | 1')->getValue(); // returns 1
Parser::calculate('1 | 0')->getValue(); // returns 1
Parser::calculate('0 | 0')->getValue(); // returns 0
// Variables
Parser::calculate('x + y', ['x' => 1, 'y' => 2])->getValue();
Parser::calculate('long_name + y', ['long_name' => 1, 'y' => 2])->getValue();
// Simple parentheses
Parser::calculate('(1 + x) * (1 + y)', ['x' => 1, 'y' => 2])->getValue();
A couple of things to keep in mind....
It cannot parse nested parentheses in strings. It can with array inputs.
The Parser needs strings with space between elements:
- 'x+y'
would be understood as one variable instead of 3 distinct statements
- 'x + y'
would parse into the 3 statements: variable x, add, and variable y
use Poindexter\Interfaces\ResultInterface;
use Poindexter\Factors;
$factors = [
new Factors\Number(5),
new Factors\Add(),
new Factors\Parenthesis([
new Factors\Number(5),
new Factors\Add(),
new Factors\Parenthesis([
new Factors\Number(5),
new Factors\Add(),
new Factors\Number(5)
])
]),
new Factors\Divide(),
new Factors\Number(5)
];
$calculator = new \Poindexter\Calculator($factors, ResultInterface::INTEGER);
$calculator->calculate()->getValue();
See tests for more examples.
Incorporating <
, >
, =
, &
, etc... yields a binary (1 or 0) result for that particular expression. If you are not careful, that might change the intention of your formulas.