tanthammar/recurring

Laravel Eloquent RRULE tools

1.1.0 2024-03-14 11:02 UTC

This package is auto-updated.

Last update: 2024-11-14 12:51:05 UTC


README

This package adds Eloquent helpers for Simshauns php Recurr package

Latest Version on Packagist Total Downloads

Installation

You can install the package via composer:

composer require tanthammar/recurring

Important

  • start_at and end_at are expected to be stored in its timezone, not converted to utc in db. The Builder will convert them using the timezone attribute.
  • end_at represents DURATION not last recurrence, src: simshaun/recurr#44

Usage

Let's say you have a DatePattern model in your codebase.

The Builder parses them in combination with the timezone attribute.

Your model must have the following attributes:

protected $casts = [
    'start_at'  => 'immutable_date', //required
    'end_at'    => 'immutable_date', //nullable, represents duration, not last recurrence
    'timezone' => 'string', //required
    'str_rule'  => 'string', //required
    'except_on' => 'array', //array with excluded dates
];

Apply the IsRecurring trait to the model

class DatePattern extends Model
{
    use IsRecurring;
    //...
}

After saving a string rrule to the str_rule field, you'll have access to the following methods. For further information about additional methods see https://github.com/simshaun/recurr

All RecurrenceCollection methods returns max 732 recurrences. (Same as parent simshaun/recurr package). Use the $count parameter to set how many recurrences you want returned.

$dp = App\Models\DatePattern::first();

$dp->recurr()->first(): bool|Recurrence
$dp->recurr()->firstStart(): bool|Carbon
$dp->recurr()->firstEnd(): bool|Carbon

$dp->recurr()->last(): bool|Recurrence
$dp->recurr()->lastStart(): bool|Carbon
$dp->recurr()->lastEnd(): bool|Carbon

$dp->recurr()->next(): bool|Recurrence
$dp->recurr()->nextStart(): bool|Carbon
$dp->recurr()->nextEnd(): bool|Carbon

$dp->recurr()->current(): bool|Recurrence
$dp->recurr()->currentStart(): bool|Carbon
$dp->recurr()->currentEnd(): bool|Carbon

$dp->recurr()->rule(): Rule

$dp->recurr()->all(): RecurrenceCollection, //limited to 732 recurrences

$dp->recurr()->schedule(?int $count): RecurrenceCollection

$dp->recurr()->scheduleBetween(string|DateTime $startDate, string|DateTime $endDate, ?int $count): RecurrenceCollection

$dp->recurr()->scheduleBefore(string|DateTime $beforeDate, ?int $count): RecurrenceCollection

$dp->recurr()->scheduleAfter(string|DateTime $afterDate, ?int $count): RecurrenceCollection

Contributing

Happy for every contribution, make a PR :)

License

The MIT License (MIT). Please see License File for more information.