Skip to content

Commit

Permalink
Merge pull request #110 from michaelKaefer/76-rrule-string
Browse files Browse the repository at this point in the history
Allow adding rrules as string
  • Loading branch information
rubenvanassche authored Mar 10, 2023
2 parents c5f248a + b082f42 commit a432116
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 4 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,24 @@ Event::create('Laracon Online')
->doNotRepeatOn([new DateTime('05/16/2020 12:00:00'), new DateTime('08/13/2020 15:00:00')]);
```

Alternatively you can add RRules as a string:

```php
Event::create('SymfonyCon')
->rruleAsString('FREQ=DAILY;INTERVAL=1');
```

If you add RRules as a string the timezones included in DTSTART and UNTIL are unknown to the package as the string is never parsed and evaluated. If they are known you can add DTSTART and UNTIL separately to help the package discover the timezones:

```php
Event::create('SymfonyCon')
->rruleAsString(
'DTSTART=20231207T090000Z;FREQ=DAILY;INTERVAL=1;UNTIL=20231208T090000Z',
new DateTime('7 december 2023 09:00:00', new DateTimeZone('UTC')),
new DateTime('8 december 2023 09:00:00', new DateTimeZone('UTC'))
);
```


### Use with Laravel

Expand Down
25 changes: 22 additions & 3 deletions src/Components/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,12 @@ class Event extends Component implements HasTimezones

private ?EventStatus $status = null;

private ?RRule $rrule = null;
/** @var RRule|string|null */
private $rrule = null;

private ?DateTime $rruleStarting = null;

private ?DateTime $rruleUntil = null;

/** @var \Spatie\IcalendarGenerator\ValueObjects\DateTimeValue[] */
private array $recurrence_dates = [];
Expand Down Expand Up @@ -295,6 +300,15 @@ public function rrule(RRule $rrule): Event
return $this;
}

public function rruleAsString(string $rrule, ?DateTimeInterface $starting = null, DateTimeInterface $until = null): Event
{
$this->rrule = $rrule;
$this->rruleStarting = $starting;
$this->rruleUntil = $until;

return $this;
}

/**
* @param DateTimeInterface[]|DateTimeInterface $dates
* @param bool $withTime
Expand Down Expand Up @@ -379,7 +393,10 @@ public function getTimezoneRangeCollection(): TimezoneRangeCollection
->add($this->starts)
->add($this->ends)
->add($this->created)
->add($this->rrule)
->add(is_string($this->rrule)
? [$this->rruleStarting, $this->rruleUntil]
: $this->rrule
)
->add($this->recurrence_dates)
->add($this->excluded_recurrence_dates);
}
Expand Down Expand Up @@ -441,7 +458,9 @@ private function resolveProperties(ComponentPayload $payload): self
)
->optional(
$this->rrule,
fn () => RRuleProperty::create('RRULE', $this->rrule)
fn () => is_string($this->rrule)
? TextProperty::create('RRULE', $this->rrule)
: RRuleProperty::create('RRULE', $this->rrule)
)
->multiple(
$this->attendees,
Expand Down
2 changes: 1 addition & 1 deletion src/Properties/RRuleProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ public function getValue(): string

public function getOriginalValue(): RRule
{
return$this->recurrenceRule;
return $this->recurrenceRule;
}
}
9 changes: 9 additions & 0 deletions tests/Components/EventTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,15 @@
->expectValue($rrule);
});

test('it an set a recurrence rule as a string', function () {
$payload = Event::create('An introduction into event sourcing')
->rruleAsString($rrule = 'FREQ=DAILY;INTERVAL=2;UNTIL=20240301T230000Z')
->resolvePayload();

PropertyExpectation::create($payload, 'RRULE')
->expectValue($rrule);
});

test('it can create an event without timezones', function () {
$dateAlert = new DateTime('17 may 2019 11:00:00');
$dateStarts = new DateTime('17 may 2019 12:00:00');
Expand Down

0 comments on commit a432116

Please sign in to comment.