choate/matex

PHP 数学表达式解析和评估器

0.0.1 2022-11-19 03:54 UTC

This package is auto-updated.

Last update: 2024-09-19 11:20:29 UTC


README

License

Matex

PHP 数学表达式解析和评估器

特性

  • 快速评估
  • 紧凑的代码库
  • 运算符:+ - * / ^ % != == < <= > >= && ||
  • 运算符优先级:[PHP 运算符优先级](https://php.ac.cn/manual/en/language.operators.precedence.php)
  • 括号,嵌套,无限制级别
  • 变量:预定义或动态估计
  • 函数:预定义或动态连接
  • 函数中的字符串参数,如 field("name")
  • 字符串操作,目前支持连接

安装

使用 Composer 运行

$ composer require choate/matex

查看手册获取更多详情和选项。

使用方法

基本

$evaluator = new \choate\matex\Evaluator();
echo $evaluator->execute('1 + 2');

字符串连接

$evaluator = new \choate\matex\Evaluator();
echo $evaluator->execute('"String" + " " + "concatenation"');

变量

$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'b' => 2
	];
echo $evaluator->execute('a + b');

动态变量

public function doVariable($name, &$value) {
	switch ($name) {
		case 'b':
			$value = 2;
			break;
	}
}

$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1
	];
$evaluator->onVariable = [$this, 'doVariable'];
echo $evaluator->execute('a + b');

函数

static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}

$evaluator = new \choate\matex\Evaluator();
$evaluator->functions = [
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
];
echo $evaluator->execute('sum(1, 2, 3)');

更多

/*
Dynamic variable resolver
Invoked when the variable is not found in the cache
Returns the value by name
*/
public function doVariable($name, &$value) {
	switch ($name) {
		case 'zen':
			// Here may be a database request, or a function call
			$value = 999;
			break;
		case 'hit':
			$value = 666;
			break;
	}
}

/*
Dynamic function resolver
Invoked when the function is not found in the cache
Returns an associative array array with:
	ref - Function reference
	arc - Expected argument count
*/
public function doFunction($name, &$value) {
	switch ($name) {
		case 'cos':
			// Map to a system function
			$value = ['ref' => 'cos', 'arc' => 1];
			break;
		case 'minadd':
			// Map to a public object instance function
			$value = ['ref' => [$this, 'minAdd'], 'arc' => 2];
			break;
	}
}

/*
Custom functions, may be a
	- Built-in function
	- Global defined function
	- Static class function
	- Object instance function
*/
static function sum($arguments) {
	$result = 0;
	foreach ($arguments as $argument)
		$result += $argument;
	return $result;
}
// Just a sample custom function
function minAdd($a, $b) {
	$r = $a < 2 ? 2 : $a;
	return $r + $b;
}

// Let's do some calculations
$evaluator = new \choate\matex\Evaluator();
$evaluator->variables = [
	'a' => 1,
	'bet' => -10.59,
	'pi' => 3.141592653589
	];
$evaluator->onVariable = [$this, 'doVariable'];
$evaluator->functions = [
	'sin' => ['ref' => 'sin', 'arc' => 1],
	'max' => ['ref' => 'max', 'arc' => null],
	'sum' => ['ref' => '\\Space\\Class::sum', 'arc' => null]
	];
$evaluator->onFunction = [$this, 'doFunction'];
echo $evaluator->execute('a + MinAdd(PI * sin(zen), cos(-1.7 / pi)) / bet ^ ((A + 2) * 2) + sum(5, 4, max(6, hit))');

查看示例获取代码示例。

作者

Choate - choate.yao@gmail - https://github.com/choate

Dorin Marcoci - dorin.marcoci@gmail.com - https://www.marcodor.com

许可协议

Matex 在MIT 许可协议下分发。