承接 khumbal/php-formula-interpreter 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

khumbal/php-formula-interpreter

Composer 安装命令:

composer require khumbal/php-formula-interpreter

包简介

Formula interpreter for PHP

关键字:

README 文档

README

A formula interpreter for php

Why this library ?

Some user could wants to perform a simple calculation and being able to change it as much as he can. Before using a library, you could use the eval function. But this method has two major drawbacks :

  • Security. A php script is being evaluated by the eval function. Php is a very powerful language, perhaps too powerful for a user especially when the user wants to inject malicious code.

  • Complexity. Php is also complex for someone who doesn't understand programming language. It could be nice to interpret an excel-like formula instead.

Installing

Use Composer to install it:

composer require khumbal/php-formula-interpreter

How does it work ?

First, create an instance of FormulaInterpreter\Compiler

$compiler = new FormulaInterpreter\Compiler();

Then use the compile()method to parse the formula you want to interpret. It will return an instance of FormulaInterpreter\Executable :

$executable = $compiler->compile('2 + 2');

Finally run the formula from the executable :

$result = $executable->run();
// $result equals 4

Using operators

Operator multiplication (*) and division () are being evaluted first, then addition (+) and subtraction (-)

You can also force the prioriry of an expression by using parentheses like this

'2 * (3 + 2)'

You can use as many parentheses as you like.

'2 * (2 * (3 + 2 * (3 + 2)) + 2)'

Comparison operators

Supported comparison operators are =, !=, <>, >=, >, <=, '<'.

'1 = 1' //true
'1 != 1' //false
'1 != 2' //true
'1 < 2' //true

Boolean operators

Supported boolean operators are AND and OR.

'(4 > 0) AND (3 > 2)' //true
'(4 > 0) AND (3 < 2)' //false
'(4 > 0) OR (3 < 2)' //true
'(4 < 0) AND (3 < 2)' //false

Future operators

Others operators like modulo, power, etc. will be implemented in the future as functions.

Using variables

A variable is just a word inside your formula like this :

'price * rate / 100'

Just before executing a formula, make sure to inject all the required variables in an array

$variables = [
   'price' => 40.2,
   'rate' => 12.8
];

$executable->run($variables);

Variables now supports dot . as the name.

$variables = [
   'user.name' => 'John Doe'
   'user.age' => '23'
];

$executable->run($variables);

String support

Variables can contain string. Use double quote (") for denote a string.

'a = "Hello world"'

Using functions

Here is an example of expression using a function :

   'cos(0)'

Available functions : pi, pow, cos, sin, sqrt & modulo

You can embed functions as much as you like

   'pow(sqrt(4), 2)'

Custom functions

Now function can be added before compiling should you want to add more variables.

$compiler = new Compiler();
$compiler->registerFunction('foobar', function ($a, $b) {
    return $a + $b;
});
$executable = $compiler->compile("foobar(foo, bar)");
$params = [
    'foo' => 1,
    'bar' => 2
];
$executable->run($params);

Listing parameters

You can get a list of all variables that must be provided to the run() method in order to run the executable:

$executable = $compiler->compile('foo + bar');

print_r($executable->getParameters());

This will output:

    Array
    (
        [0] => foo
        [0] => bar
    )

khumbal/php-formula-interpreter 适用场景与选型建议

khumbal/php-formula-interpreter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 492 次下载、GitHub Stars 达 1, 最近一次更新时间为 2021 年 02 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「php」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 khumbal/php-formula-interpreter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 khumbal/php-formula-interpreter 我们能提供哪些服务?
定制开发 / 二次开发

基于 khumbal/php-formula-interpreter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 492
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-11