Skip to content

Add regex analyser action handler to execute APIv4 actions#527

Draft
dontub wants to merge 1 commit intomasterfrom
api4-regex-analyser-action-handler
Draft

Add regex analyser action handler to execute APIv4 actions#527
dontub wants to merge 1 commit intomasterfrom
api4-regex-analyser-action-handler

Conversation

@dontub
Copy link
Copy Markdown
Collaborator

@dontub dontub commented Apr 10, 2026

This adds an regex analyser action handler for the action "api4" to execute APIv4 actions. It is inspired by #513.

To use values from the pattern matches, the bank transaction (btx), the bank account (ba), or the party bank account (party_ba) the Symfony Expression Language is used. So it is not only possible to access a single value, but to do some mathematics and other things. Strings starting with @= (this is the prefix Symfony uses in config files) are interpreted as expressions. I decided to give expressions also a try in the result_map in addition to what was discussed in #513. In expressions in the result map the Result object is available as variable result. I'm not sure if we need and want to have two approaches. (This is something to be discussed.) With the expression language the expression @=result.first()['some_field'][0] ?? NULL could be used instead of the first filter.

The Symfony Expression Language can be extended with custom functions so we're not limited to the ones available by default. (We could even consider giving third party extensions the possibility to provide custom functions.)

Adapted example from #513:

{
  "comment": "Look for previous contribution with matching bank name and amount",
  "action": "api4",
  "api4": {
    "entity": "Contribution",
    "action": "get",
    "params": {
      "limit": 1,
      "orderBy": {
        "receive_date": "DESC"
      },
      "where": [
        [
          "Donor_Information.Bank_Name",
          "=",
          "@=purpose"
        ],
        [
          "total_amount",
          "=",
          "@=btx.amount"
        ]
      ]
    },
    "result_map": {
      "previous_contribution_id": "id",
      "contact_id": "contact_id"
      "financial_type_id": "@=result.first()['financial_type_id']",
    }
  }
},

Actually @=result.first()['financial_type_id'] is the same as just financial_type_id here with results limited to one in the API call. It's just meant as an example. In \Civi\Banking\Matcher\RegexAnalyser\ActionHandlers\Api4RegexAnalyserActionHandlerTest you can find expressions with operations (addition, string concatenation).

Note: The . is the object access operator in expressions which prevents using dots in pattern matches. In case there are values with . in the parsed data of btx, array access can be used: btx['foo.bar'].

I'd like to hear your opinions @ufundo, @jensschuppe.

Another thing I'm currently unsure about: What is the expected behavior when the APIv4 call returns no result, but a result map is defined. The current implementation doesn't set any value at all, though another option would be to set NULL. In case of expressions they might be evaluated with the empty result object.

systopia-reference: 30273

@dontub dontub requested a review from jensschuppe April 10, 2026 15:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant