定制 leongrdic/smplang 二次开发

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

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

leongrdic/smplang

Composer 安装命令:

composer require leongrdic/smplang

包简介

A simple language written in PHP that evaluates expressions without eval

README 文档

README

release php-version license run-tests

try

SMPLang is a simple expression language written in PHP. It can be considered similar to PHP's native eval() function but SMPLang has its own syntax and the expressions are evaluated in sort-of a sandbox with access to only vars and functions/closures that you pass into it.

The language is partly inspired by Symfony Expression Language but there are some major differences like array unpacking, named arguments and easier function definition, thus SMPLang may not be a replacement for some use cases.

Install:

composer require leongrdic/smplang

To use SMPLang, create a new instance of the \Le\SMPLang\SMPLang class and pass in an associative array of vars that you want to use in your expressions.

$smpl = new \Le\SMPLang\SMPLang([
    'variableName' => 'value',
]);

You can then call the evaluate() method on the instance, passing in an expression string. This will return the result of the expression.

$result = $smpl->evaluate($expression);

Optionally, you can pass in an associative array to the second parameter to provide additional local vars to use in the expression:

$result = $smpl->evaluate($expression, [
    'localVariable' => 123,
]);

Vars passed this way will override vars passed in the constructor and will only be available in this specific expression, in case multiple expressions are evaluated from the same object.

In case of an exception, \Le\SMPLang\Exception will be thrown with a short description of the error.

Expression syntax

Vars are accessed by only their name. If a var is not defined in neither constructor or evaluate(), an exception will be thrown.

Supported literals

  • null
  • booleans (true and false)
  • strings ("string" or 'string' or `string`)
  • numbers (1, 1.2, -1, -1.2)
  • arrays ([23, 'string'] or ["key": 23, 'key2': 'string'])
  • objects ({foo: "bar", baz: 23})

Arrays

Array definition: [element1, element2]

Associative array definition: ["key": element, string_variable: element2]

You can define associative arrays with dynamic keys by using string vars in place of keys.

Array unpacking is supported: [element1, ...array, ...array2]

Access array elements using either of the following syntaxes: array.key.0 or array['key'][0] (which allows for dynamic array access).

Objects

Object definition: {foo: "bar", baz: 23} (supports array unpacking)

Object property access: object.property

Object method call: object.method(parameters)

Function / closure call

Call a function or closure var: closure_var(param1, param2).

Named arguments: foo(search: value, count: 1).

Function / closure calls support array unpacking: bar(param1, ...array, ...array2)

Arithmetic operators

  • +: addition
  • -: subtraction
  • *: multiplication
  • /: division
  • %: modulo (a*b%c*d == (a*b)%(c*d))
  • **: exponentiation (a*b**c*d == (a*b)**(c*d))

Comparison operators

  • ===: strict equality
  • !==: strict inequality
  • ==: equality
  • !=: inequality
  • >: greater than
  • <: less than
  • >=: greater than or equal to
  • <=: less than or equal to

Logical operators

  • &&: logical and
  • ||: logical or
  • !: logical not

String concatenation

  • ~: string concatenation

Ternary expressions

  • a ? b : c
  • a ?: b (is equivalent to a ? a : b)
  • a ? b (is equivalent to a ? b : null)

Examples

$smpl = new \Le\SMPLang\SMPLang();
$result = $smpl->evaluate('(1 + 2 * 3) / 7');
// $result will be 1

try

$smpl = new \Le\SMPLang\SMPLang([
    'foo' => 'bar',
    'arr' => [1, 2, 3],
    'hash' => ['a' => 'b'],
]);

$result = $smpl->evaluate('foo ~ " " ~ arr[1] ~ " " ~ hash.a');
// $result will be "bar 2 b"

try

$smpl = new \Le\SMPLang\SMPLang([
    'prepend' => fn(string $a): string => "hello $a",
    'reverse' => strrev(...),
]);

$result = $smpl->evaluate('prepend("simple " ~ reverse("world"))');
// $result will be "hello simple dlrow"

try

$smpl = new \Le\SMPLang\SMPLang([
    'foo' => 'bar',
]);

$result = $smpl->evaluate('foo !== "bar" ? "yes" : "no"');
// $result will be "no"

try

// SMPL can even be used as an (incredibly slow) JSON parser!

$smpl = new \Le\SMPLang\SMPLang();
$json = '{ "foo": null, "bar": 15.23, "baz": true, "arr": [5, 6], "str": "cool" }';
$result = $smpl->evaluate($json);
print_r($result);

try

leongrdic/smplang 适用场景与选型建议

leongrdic/smplang 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 807.39k 次下载、GitHub Stars 达 16, 最近一次更新时间为 2022 年 05 月 10 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 leongrdic/smplang 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 807.39k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 16
  • 点击次数: 18
  • 依赖项目数: 4
  • 推荐数: 0

GitHub 信息

  • Stars: 16
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-10