简体   繁体   English

PHP 中的条件间隔期

[英]Conditional interval periods in PHP

I'm creating a PHP app targetting companies (in Congo), ultimately helping them manage the employees and their statuses.我正在创建一个针对公司(在刚果)的 PHP 应用程序,最终帮助他们管理员工及其状态。

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.从0到6个月,试用期为15天。
  • Above, the trial period is 1 month.以上,试用期为1个月。

While the company's internal rules state:虽然公司的内部规则规定:

  • For categories 1 to 6, the trial period is 5 days.对于类别 1 至 6,试用期为 5 天。
  • For categories 7 to 8, the trial period is 10 days.第七类至第八类,试用期为10天。
  • For categories 9 to 12, the trial period is 15 days.对于第 9 至第 12 类,试用期为 15 天。

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).我很难编写一个可以存储条件类型(持续时间或类别)、间隔(从x天/月/年到y天/月/年,或从类别xy )的类,结果(以日/月/年为单位的持续时间)。

In my v1, I had this:在我的 v1 中,我有这个:

'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.使用 strtotime 转换日期字符串,结果很难使用。

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.我正在考虑类似DatePeriod PHP 类的东西,而不是日期,我会使用DateInteval作为间隔(从xy ),但不确定这是否是最好的方法。 Are there better solutions in the wild?在野外有更好的解决方案吗?

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.根据我的经验, Period是一个优秀的实现部分库。 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 .在我的头顶上,我想起了Symfony ExpressionLanguageHoa Ruler

In the end, I wrote a composer package: max13/conditional-period最后写了一个composer包: max13/conditional-period

It was simpler than I thought.这比我想象的要简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM