Giter Site home page Giter Site logo

generic-value-expression's Introduction

概要

渡されたデータを、jsonで定義された式に沿って計算を行います。

計算式の定義

以下の形が基本形。これをexpressionと呼ぶ。expressionは複数のexpressionを子に持つとことができる。

{
  "type": "type_name",
  "value": "value if necessary",
  "expressions": []
}

typeは、

  • 値を示すもの
    • constant
    • variable
  • 条件式
    • equals
    • and
    • or
    • morethan
  • 関数
    • concat
    • if
    • multiply
    • add
  • 集約
    • count
    • countif
    • exists
    • max
    • sum

typeによって必要なexpressionsvalueは異なる。例えば、

  • 条件式equalsは二つの値を比較するため、 2つのexpressionを指定する必要がある。
  • ifは、 条件式trueの場合の返り値falseの場合の返り値 の3つの expressionが必要。
  • constantvariableの値を表す場合は value が必要。

sample

sampleディレクトリも参照。

sample1

計算式定義

出庫依頼に含まれる商品の合計金額に税額を加えた金額を算出して、最後に円をつける

{
  "type": "concat",
  "expressions": [
    {
      "type": "multiply",
      "expressions": [
        {
          "type": "sum",
          "expressions": [
            {
              "type": "variable",
              "value": "items"
            },
            {
              "type": "multiply",
              "expressions": [
                {
                  "type": "variable",
                  "value": "quantity"
                },
                {
                  "type": "variable",
                  "value": "price"
                }
              ]
            }
          ]
        },
        1.08
      ]
    },
    ""
  ]
}
$engine = Parser::parse(json_decode('JSONで書いた計算式', true));
echo $engine->evaluate([
    'items' => [
        ['quantity' => 3, 'price' => 100],
        ['quantity' => 2, 'price' => 500],
    ]
]) . PHP_EOL;

> 1404

sample2

itemsに、OL001-I000001が3レコード以上かつOL001-I000002を含むという条件に当てはまるかをチェック

計算式

{
  "type": "and",
  "expressions": [
    {
      "type": "morethan",
      "expressions": [
        {
          "type": "countif",
          "expressions": [
            {
              "type": "variable",
              "value": "items"
            },
            {
              "type": "equals",
              "expressions": [
                {
                  "type": "variable",
                  "value": "uid"
                }, "OL001-I000001"
              ]
            }
          ]
        },
        2
      ]
    },
    {
      "type": "exists",
      "expressions": [
        {
          "type": "variable",
          "value": "items"
        }, {
          "type": "equals",
          "expressions": [
            {
              "type": "variable",
              "value": "uid"
            }, "OL001-I000002"
          ]
        }
      ]
    }
  ]
}
$engine = Parser::parse(json_decode('JSONで書いた計算式', true));

echo $engine->evaluate([
        'items' => [
            ['uid' => 'OL001-I000001'],
            ['uid' => 'OL001-I000001'],
            ['uid' => 'OL001-I000001'],
            ['uid' => 'OL001-I000002'],
        ]
    ]) . PHP_EOL;
> 1

expressionをオブジェクトではなく文字列・数値で指定した場合は、constantとして扱われる。

sample3

CSVの変換にexpressionを用いた例

./sample/csv_export 参照

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.