vmassalov/config

Configuration service

v1.0.0-alpha 2024-02-22 21:40 UTC

This package is auto-updated.

Last update: 2024-04-23 15:52:18 UTC


README

Library for matching rulesets based on file configs

The main goal of this package is to move out endless sequences of business logic 'elseif' outside of the code. It can be mapping exception classes to error codes, discount calculation rules, or any other strategy conditions. All conditions that do not change often enough and are not worth putting in the database can be conveniently transferred to the configs.

Install

Via composer

composer require vmassalov/config

Quick usage

Create yaml config file, e.g.:

- conditions:
    dayOfWeek:
      - monday
      - tuesday
      - wednesday
      - thursday
      - friday
    projectPriority:
      - normal
      - minor
  result:
    salaryRate: 1
    needOvertimeApprove: true

- conditions:
    dayOfWeek:
      - monday
      - tuesday
      - wednesday
      - thursday
      - friday
    projectPriority: critical
  result:
    salaryRate: 1
    needOvertimeApprove: false

- conditions:
    projectPriority: critical
  result:
    salaryRate: 2
    needOvertimeApprove: false

All result blocks should contain same keys. All condition block can contain a different keys with single or multiple options.

$configClient = \VMassalov\Config\ClientFactory::build('filesystem://./path/to/configs');
$configClient->find(
    'config.yaml',
    [
        'dayOfWeek' => 'sunday',
        'projectPriority' => 'critical',
    ],
);

Client will return a result of first full match item based on passed criteria

Configuration

DSN

filesystem://

Config syntax

See examples in tests/functional/stubs/yaml/baseConfig.yaml

Test

Run unit and functional test in docker

make build && make test

Run unit and functional test locally

php ./vendor/bin/phpunit

Roadmap

  • Add JSON support
  • Add XML support