1

I'm creating a PHP app targetting companies (in Congo), ultimately helping them manage the employees and their statuses.

The legal part is constructed in 3 parts, in order of priority: The labor code, The collective agreement, the rules of procedures.

The hardest part for me, yet, is to write the "conditional periods" of the law, the agreements and the in the rules of procedures. For instance, concerning the trial periods of a fixed term contract:

The law and collective agreement state:

  • From 0 to 6 months, the trial period is 15 days.
  • Above, the trial period is 1 month.

While the company's internal rules state:

  • For categories 1 to 6, the trial period is 5 days.
  • For categories 7 to 8, the trial period is 10 days.
  • For categories 9 to 12, the trial period is 15 days.

In this typical example, the internal rules state something based on different conditions than the law. I'm having a hard time writing a class in which I can store a type of condition (duration or category), interval (from x day/month/year to y day/month/year, or from category x to y), the result (a duration in day/month/year).

In my v1, I had this:

'ftc_trial_periods'  => [
    [
        'left' => 'duration',
        'intervals' => [0, '6 months'],
        'result' => '15 days',
    ],
    '1 months',
];

Using strtotime to convert the date strings, which turned out to be hard to use.

I'm thinking about something like the DatePeriod PHP class, instead of dates, I would use DateInteval as intervals (from x to y) but not sure if it would be the best approach. Are there better solutions in the wild?

Max13
  • 919
  • 2
  • 9
  • 27

2 Answers2

1

I think you should treat this as two separate problems:

  1. How do you conveniently represent business rules.
  2. How do you implement underlying date calculations.

From my experience Period is an excellent library for implementation part. However I have a hunch that it might not be the best approach to formulate and keep track of your original rules.

There are solutions out there specifically for writing down business logic like this. Unfortunately I am not experienced with those to make a specific recommendation for your case. On top of my head I remembered Symfony ExpressionLanguage and Hoa Ruler.

Rarst
  • 2,335
  • 16
  • 26
  • Thank you for your suggestion. I think starting from Period or DatePeriod is a better idea (in my case) than using business logic libraries. The other values are simple, only "a given period depending on category or duration interval" are a pain for now. I'm waiting for eventual other suggestions, else your answer would have been the best – Max13 Feb 25 '19 at 11:06
0

In the end, I wrote a composer package: max13/conditional-period

It was simpler than I thought.

Max13
  • 919
  • 2
  • 9
  • 27