Date rules
mitze opened this issue · 6 comments
Hi,
I'm playing around with this fantastic library and wondered what would be the best way to make rules with dates.
If you could include some examples in the documentation it would be great.
Thank you.
@mitze Hi and thanks 😃,
What do you want to do with dates? This is an interesting usecase and I reckon it's not that hard, but there is so much possibilities I would like a clue about your goal.
In my particular case I'm trying to evaluate the following conditions for a given date:
- It's between two dates (eg 2015-11-03 13:35:23 is between 2015-11-03 09:00:00
and 2015-11-04 08:59:59) - It's on a list of days of the week (eg Monday, Wednesday and Friday)
- The number of seconds that have passed since an earlier date
(eg 2015-11-03 10:35:23 - 2015-11-03 10:31:12 = 251 seconds)
On the other hand. Is it possible that you can create the BETWEEN operator?
EDIT: Other use case I will need.
- It's between two hours (eg 2015-11-03 13:35:23 is between 09:00:00
and 18:45:00)
It would be great to create these operators in the standard library (aka the operators provided by Hoa\Ruler
). However, in order to not introduce a regression (let's say, if someone has already declared a similar operator and function, upgrading to a new version of Hoa\Ruler
could break its code… actually it won't break because its operators or functions will override existing ones), we could introduce a “pluggable collection of operators”.
I mean, something like a list of operators that could be added to the asserter easily. That's really easy to do. Just an array of operators/functions. For instance:
$asserter = $ruler->getAsserter();
$asserter->addOperators(new My\Collection\Of\Operators());
The My\Collection\Of\Operators
would implement the Traversable
interface. This way, we would be able to add more operators without introducing regressions. The user knows if a collection is used or not. Since we are likely to add functions more than operators, we can namespace function names by using _
, for instance: date_compare($date1, $date2)
for the date
collection.
Thoughts @hoaproject/hoackers?
I really like the idea.
What I was testing was to create new operators in the way that appears in the documentation.
//Day of the week
$ruler->getDefaultAsserter()->setOperator('week_day', function ($date) {
return date('N', strtotime($date));
});
//Evaluation rule
$rule = 'week_day(date) IN [1,2,3,7]';
However I prefer your suggestion as these pluggins can be more efficient and the code will look neater without aggregates in the middle.
Besides people could create and share their pluggins more easily.
So, the best of this library that is the flexibility would be even better.