定制 mormat/php-formula-interpreter 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

mormat/php-formula-interpreter

Composer 安装命令:

composer require mormat/php-formula-interpreter

包简介

Formula interpreter for PHP

关键字:

README 文档

README

A stand-alone php library for parsing and running formulas

Installation via composer

composer require mormat/php-formula-interpreter

Usage

First, create an instance of \Mormat\FormulaInterpreter\Compiler

$compiler = new \Mormat\FormulaInterpreter\Compiler();

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

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

Finally run the formula from the executable :

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

Examples of formulas

// variables can be used
price + 2 

// parenthesis can be used
(1 + 2) * 3 

// default functions are available
sqrt(4) 

// complex formulas can be used
(((100 * 0.43075) * 1.1 * 1.5) / (1-0.425)) * 1.105 

// string are supported
lowercase('FOO')

// arrays are supported
count([2, 3, 4])

// custom functions can be registered
your_function_here(2) 

// use the `in` operator to check if an item is in array
1 in [1, 2, 3]  // returns true

// use the `in` operator to check if a substring is in a string
'Wars' in 'Star Wars'

// using logical operators (`and`, `or`, `not`)
1 < x and x < 10
1 < x or x < 10
not x > 0

Supported types in formulas

Numeric values

A numeric value can be an integer or a float

    2       // integer
    2.30    // float

String values

Use simple quote to delimiter strings

    'foobar'

Array values

Use comma to separate items and brackets to wrap the items

    [1, 2, 3]

Functions, strings and operations can be used as an item of an array

    [cos(0), 'foobar', 2 + 2]

Using operators

The following operators are available :

operator usage description
+ a + b Sum of a and b.
- a - b Difference of a and b.
* a * b Product of a and b.
/ a / b Quotient of a and b.
in a in b If a is an array, checks if b is an item of a. If a is a string, checks if b is a substring of a
and  a and b Returns true if both the operands are true
or  a or b Returns true if either of the operands is true
not  not a Returns true if the operand is false

The operators *, \ are being evaluated first, then the operators + and -

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)

Using variables

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

price * rate / 100

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

$variables = array(
   'price' => 40.2,
   'rate' => 12.8
);

$executable->run($variables);

Using functions

Availables functions

name allowed types description
pi Get value of pi
cos numeric Cosine
sin numeric Sine
sqrt numeric Square root
pow numeric,numeric Exponential expression
modulo numeric,numeric Remainder of first value divided by second value
lowercase string Converts to a string lowercase
uppercase string Converts to a string uppercase
capitalize string Make a string's first character uppercase
count string|array If value is an array, count the items in the array. If value is a string, count the characters in the string

How to register a custom function ?

Use the registerCustomFunction() method in the \Mormat\FormulaInterpreter\Compiler class.

The custom function must implement the \Mormat\FormulaInterpreter\Functions\FunctionInterface. This interface contains the methods below :

  • getName() returns the name of the function
  • supports($arguments) returns true if the $arguments send to the function are valid.
  • execute($arguments) executes the function and returns the value.

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.

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

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 237.76k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 9
  • 点击次数: 29
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 7
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2013-03-28