PHPackages                             prohalexey/the-choice - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. prohalexey/the-choice

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

prohalexey/the-choice
=====================

3.0.1(1mo ago)253.3k↓22.2%5[3 issues](https://github.com/prohalexey/TheChoice/issues)MITPHPPHP &gt;=8.4

Since Nov 25Pushed 9mo ago2 watchersCompare

[ Source](https://github.com/prohalexey/TheChoice)[ Packagist](https://packagist.org/packages/prohalexey/the-choice)[ RSS](/packages/prohalexey-the-choice/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (4)Dependencies (30)Versions (10)Used By (0)

TheChoice - Business Rule Engine
================================

[](#thechoice---business-rule-engine)

[![Build Status](https://camo.githubusercontent.com/ef2f8bf7ec63d4ddb5db7058792bb531d4aa8b79ebacfd067bf1812477e1c2f8/68747470733a2f2f7472617669732d63692e6f72672f70726f68616c657865792f54686543686f6963652e706e67)](https://travis-ci.org/prohalexey/TheChoice)[![GitHub license](https://camo.githubusercontent.com/7013272bd27ece47364536a221edb554cd69683b68a46fc0ee96881174c4214c/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)](https://raw.githubusercontent.com/prohalexey/TheChoice/master/LICENSE)

A powerful and flexible Business Rule Engine for PHP that allows you to separate business logic from your application code.

Features
--------

[](#features)

This library helps you simplify the implementation of complex business rules such as:

- Complex discount calculations
- Customer bonus systems
- User permission resolution
- Dynamic pricing strategies

**Why use TheChoice?** If you find yourself constantly modifying business conditions in your code, this library allows you to move those conditions to external configuration sources. You can even create a web interface to edit configurations dynamically.

### Key Benefits

[](#key-benefits)

- ✅ Rules written in JSON or YAML format
- ✅ Store rules in files or databases
- ✅ Serializable and cacheable configurations
- ✅ PSR-11 compatible container support
- ✅ Extensible with custom operators and contexts

Installation
------------

[](#installation)

```
composer require prohalexey/the-choice
```

Quick Start
-----------

[](#quick-start)

### JSON Configuration Example

[](#json-configuration-example)

```
{
  "node": "condition",
  "if": {
    "node": "collection",
    "type": "and",
    "elements": [
      {
        "node": "context",
        "context": "withdrawalCount",
        "operator": "equal",
        "value": 0
      },
      {
        "node": "context",
        "context": "inGroup",
        "operator": "arrayContain",
        "value": [
          "testgroup",
          "testgroup2"
        ]
      }
    ]
  },
  "then": {
    "node": "context",
    "description": "Giving 10% of deposit sum as discount for the next order",
    "context": "getDepositSum",
    "modifiers": [
      "$context * 0.1"
    ],
    "params": {
      "discountType": "VIP client"
    }
  },
  "else": {
    "node": "value",
    "description": "Giving 5% discount for the next order",
    "value": "5"
  }
}
```

### YAML Configuration Example

[](#yaml-configuration-example)

```
node: condition
if:
  node: collection
  type: and
  elements:
  - node: context
    context: withdrawalCount
    operator: equal
    value: 0
  - node: context
    context: inGroup
    operator: arrayContain
    value:
      - testgroup
      - testgroup2
then:
  node: context
  context: getDepositSum
  description: "Giving 10% of deposit sum as discount for the next order"
  modifiers:
    - "$context * 0.1"
  params:
    discountType: "VIP client"
else:
  node: value
  description: "Giving 5% discount for the next order"
  value: 5
```

### PHP Usage

[](#php-usage)

```
