1. Go to this page and download the library: Download sterzik/expression 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/ */
sterzik / expression example snippets
use Sterzik\Expression\Parser;
use Sterzik\Expression\Parser;
$parser = new Parser();
#quotes are part of the string, backslash must be double encoded
#because of the php string syntax
$expr = $parser->parse('"Hello world!\\n"');
echo $expr->evaluate();
$parser = new Parser();
$expression = $parser->parse("1+2*3");
$dataStruct = $expr->dump(); #dumps as a php structure without objects (serializable to json)
$dataJson = $expr->dumpJson(); #dumps into a json encoded string
$dataB64 = $expr->dumpBase64(); #dumps into a base64 encoded string
# obtain the default evaluator
# (able to evaluate all operations from the default parser)
$evaluator = Evaluator::get("default");
# obtain the empty evaluator
# (no operations are defined there)
$evaluator = Evaluator::get("empty");
$varObject = new Variables(["a" => 1, "b" => 2, "c" => 3]);
$varObject["a"] = 2;
unset($varObject["c"]);
foreach($varObject as $variable => $value) {
#do something for all variables and its values
}
$varArray = $varObject->asArray(); #convert back to an ordinary array
$evaluator->defOpEx("=", function ($a, $b) {
$a->assign($b->value());
});
$evaluator->defNotLvalue(function () {
throw new Exception("LValue
function createVariableLValue($varName, $varObject)
{
$builder = new LValueBuilder();
$builder->value(function () use ($varName, $varObject) {
return $varObject[$varName];
});
$builder->assign(function ($value) use ($varName, $varObject) {
$varObject[$varName] = $value;
});
$builder->setDefaultCallback(function ($method) {
throw new Exception("Method ".$method." is not supported for that L-value");
});
return $builder->getLValue();
}
$parserSettings->addPostfixIndex('fn()', '(', ')', true);
#ensure the op ',' will have the lowest priority
$parserSettings->opPriority('0');
$parserSettings->addVariadicOp(',');
$parserSettings->setPostprocessOp('fn()', function ($expr, $op) {
# count($expr) is the number of arguments of the 'fn()' operation
# possibilities for the number of arguments:
# 0 - impossible
# 1 - fncall without any argument
# 2 - fncall with one or more arguments
# 3 or more - impossible
if (count($expr) != 2) {
return $expr;
}
$arg = $expr[1];
#if the second argument is not the ',' operator, we don't need to postprocess anything
if ($arg->type() != "op" || $arg->op() != ",") {
return $expr;
}
$newArguments = array_merge([$expr[0]], $arg->arguments());
return Expression::create("op", "fn()", $newArguments);
});
#convert all variables "parent" to some variable depending on the current context
$parserSettings->setPostprocessVar(function ($expr) {
if ($expr->name() == "parent") {
$parentVar = getCurrentParentName(); #obtain somehow the current parent name
return Expression::create("var", $parentVar);
}
return null;
});
#convert all non-string constants to strings
$parserSettings->setPostprocessConst(function ($expr) {
if (!is_string($expr->value())) {
return Expression::create("const", (string)$expr->value());
}
return null;
});