Skip to content
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/Recurr/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,39 @@ public static function createFromArray(
}

/**
* Create a Rule object based on natural language text.
*
* @param string $text Natural language description like "every day for 3 times"
* @param \DateTime|\DateTimeImmutable|string|null $startDate Start date for the recurrence
* @param \DateTime|\DateTimeImmutable|string|null $endDate End date for the recurrence
* @param string|null $timezone Timezone identifier
*
* @return self
*
* @throws InvalidRRule If the text cannot be parsed
*/
public static function createFromText(
string $text,
\DateTime|\DateTimeImmutable|string|null $startDate = null,
\DateTime|\DateTimeImmutable|string|null $endDate = null,
?string $timezone = null,
): self {
require_once __DIR__ . '/TextParser.php';
Copy link
Owner

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed


$parser = new TextParser();
$options = $parser->parseText($text);

if ($options === null) {
throw new InvalidRRule('Unable to parse text: ' . $text);
}

return new self($options, $startDate, $endDate, $timezone);
}

/**
* Populate the object based on a RRULE string.
*
* @param string $rrule RRULE string
* Populate the model from a RRULE string.
*
* @throws InvalidRRule
Expand Down
Loading