-
Notifications
You must be signed in to change notification settings - Fork 152
Add text-to-rule parsing functionality #227
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add text-to-rule parsing functionality #227
Conversation
FabianoLothor
commented
Oct 1, 2025
- Create TextParser class with natural language processing
- Add Rule::createFromText() static method
- Support common patterns: frequencies, intervals, counts, weekdays, months
- Compatible with existing Rule infrastructure and TextTransformer
|
@simshaun I don't know how to trigger phpstan check in the PR. |
1a99dd0 to
db0b79f
Compare
…to match Rule class patterns
- Cast numeric values (INTERVAL, COUNT, BYMONTH) to strings - Update type annotations to match Rule constructor expectations - Maintain null safety checks for symbol array access
9f6e6ea to
7b99dc8
Compare
src/Recurr/Rule.php
Outdated
| \DateTime|\DateTimeImmutable|string|null $endDate = null, | ||
| ?string $timezone = null, | ||
| ): self { | ||
| require_once __DIR__ . '/TextParser.php'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't be necessary with Composer's autoloader
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
|
Thank you! At initial glance, looks good. I'll fully review when I get some downtime. Couple things:
|
I won't be able to write unit-tests until the weekend, but this snippet might be helpful if you decide to write them properly: <?php
require_once 'src/Recurr/Exception.php';
require_once 'src/Recurr/Exception/InvalidRRule.php';
require_once 'src/Recurr/Exception/InvalidArgument.php';
require_once 'src/Recurr/Exception/InvalidWeekday.php';
require_once 'src/Recurr/TextParser.php';
require_once 'src/Recurr/Rule.php';
use Recurr\Rule;
use Recurr\TextParser;
echo "Testing Text-to-Rule functionality\n";
echo "==================================\n\n";
// Test cases similar to rrule.js examples
$testCases = [
'every day',
'every 2 days',
'every week',
'every 3 weeks',
'every month',
'every year',
'every day for 3 times',
'every 2 weeks for 5 times',
'every Monday',
'every Friday',
'every weekdays',
'every January',
'every December'
];
foreach ($testCases as $text) {
echo "Testing: '$text'\n";
try {
$rule = Rule::createFromText($text);
$rruleString = $rule->getString();
echo " Result: $rruleString\n";
echo " Frequency: " . $rule->getFreqAsText() . "\n";
if ($rule->getInterval() > 1) {
echo " Interval: " . $rule->getInterval() . "\n";
}
if ($rule->getCount()) {
echo " Count: " . $rule->getCount() . "\n";
}
if ($rule->getByDay()) {
echo " ByDay: " . implode(',', $rule->getByDay()) . "\n";
}
if ($rule->getByMonth()) {
echo " ByMonth: " . implode(',', $rule->getByMonth()) . "\n";
}
} catch (Exception $e) {
echo " Error: " . $e->getMessage() . "\n";
}
echo "\n";
}
echo "Testing completed!\n";
I really like the multi-lingual support suggestion, certainly doable and likely not too complicated to implement. That said, I'm not aware of a similar feature in other libraries that supports internationalization. It would be a really nice and welcome novelty. todoist for example is an app that supports multi-lingual support for recurring tasks. |
4b2eda8 to
2440c2b
Compare
|
Initial unit-tests for the class The phpstan error that is appearing in the PR is also happening on $ git branch --show-current
master
$ git pull
Already up to date.
$ git status
On branch master
Your branch is up to date with 'origin/master'.
nothing to commit, working tree clean
$ ./vendor/bin/phpstan analyse --no-ansi --no-progress --error
-format=table
Note: Using configuration file ./phpstan.neon.
------ ---------------------------------------------
Line src/Recurr/Transformer/ArrayTransformer.php
------ ---------------------------------------------
:258 Possibly invalid array key type float|int.
🪪 offsetAccess.invalidOffset
:277 Invalid array key type float.
🪪 offsetAccess.invalidOffset
------ ---------------------------------------------
[ERROR] Found 2 errors |