Skip to content

Commit

Permalink
Better handling of legacy multi-day events (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell authored Feb 21, 2024
1 parent a4a7773 commit 38dfe6a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 10 deletions.
18 changes: 12 additions & 6 deletions src/EventFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ class EventFactory
{
public static function createFromEntry(Entry $event, bool $collapseMultiDays = false): Event
{
if ($event->multi_day || $event->recurrence->value() === 'multi_day') {
return new MultiDayEvent($event, $collapseMultiDays);
$eventType = static::getTypeClass($event);

return new $eventType($event, $collapseMultiDays);
}

public static function getTypeClass(Entry $event): string
{
if (in_array($event->get('recurrence'), ['daily', 'weekly', 'monthly', 'every'])) {
return RecurringEvent::class;
}

// this has to be `->value` because `recurrence` returns a `LabeledValue`.
if (in_array($event->recurrence->value(), ['daily', 'weekly', 'monthly', 'every'])) {
return new RecurringEvent($event);
if (($event->multi_day || $event->get('recurrence') === 'multi_day') && !empty($event->get('days'))) {
return MultiDayEvent::class;
}

return new SingleDayEvent($event);
return SingleDayEvent::class;
}
}
8 changes: 7 additions & 1 deletion src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Statamic\Fields\Values;
use Statamic\Support\Arr;
use Statamic\Tags\Concerns\QueriesConditions;
use TransformStudios\Events\Types\MultiDayEvent;

class Events
{
Expand Down Expand Up @@ -163,6 +164,11 @@ private function entries(): self
return $this;
}

private function isMultiDay(Entry $occurrence): bool
{
return EventFactory::getTypeClass(event: $occurrence) === MultiDayEvent::class;
}

private function occurrences(callable $generator): EntryCollection
{
return $this->entries
Expand All @@ -178,7 +184,7 @@ private function occurrences(callable $generator): EntryCollection

private function hasStartDate(Entry $occurrence): bool
{
if ($occurrence->multi_day || $occurrence->get('recurrence') === 'multi_day') {
if ($this->isMultiDay($occurrence)) {
try {
$days = collect($occurrence->days);

Expand Down
2 changes: 1 addition & 1 deletion src/Types/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public function isAllDay(): bool

public function isMultiDay(): bool
{
return boolval($this->multi_day) || $this->recurrence?->value() === 'multi_day';
return boolval(($this->multi_day || $this->recurrence?->value() === 'multi_day') && ! empty($this->days));
}

public function isRecurring(): bool
Expand Down
97 changes: 97 additions & 0 deletions tests/Unit/EventFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

namespace TransformStudios\Events\Tests\Unit;

use Illuminate\Support\Carbon;
use Statamic\Facades\Entry;
use TransformStudios\Events\EventFactory;
use TransformStudios\Events\Tests\TestCase;
use TransformStudios\Events\Types\MultiDayEvent;
use TransformStudios\Events\Types\RecurringEvent;
use TransformStudios\Events\Types\SingleDayEvent;

class EventFactoryTest extends TestCase
{
/**
* @test
*
* @dataProvider provideEventData
*/
public function canGetEventTypeClass(string $class, array $data)
{
$entry = Entry::make()
->collection('events')
->data($data);

$this->assertEquals($class, EventFactory::getTypeClass($entry));
}

/**
* @test
*
* @dataProvider provideEventData
*/
public function canCreateCorrectEventType(string $class, array $data)
{
$entry = Entry::make()
->collection('events')
->data($data);

$this->assertInstanceOf($class, EventFactory::createFromEntry($entry));
}

public static function provideEventData()
{
return [
[
SingleDayEvent::class,
[
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'timezone' => 'America/Vancouver',
],
],
[
SingleDayEvent::class,
[
'multi_day' => true,
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'timezone' => 'America/Vancouver',
],
],
[
RecurringEvent::class,
[
'start_date' => Carbon::now()->toDateString(),
'end_date' => Carbon::now()->addWeek()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'recurrence' => 'daily',
'timezone' => 'America/Vancouver',
],
],
[
MultiDayEvent::class,
[
'recurrence' => 'multi_day',
'days' => [
[
'date' => '2019-11-23',
'start_time' => '19:00',
'end_time' => '21:00',
],
[
'date' => '2019-11-24',
'start_time' => '11:00',
'end_time' => '15:00',
],
],
'timezone' => 'America/Vancouver',
],
],
];
}
}
5 changes: 3 additions & 2 deletions tests/Unit/RecurringEventsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use TransformStudios\Events\Tests\TestCase;
use TransformStudios\Events\Types\MultiDayEvent;
use TransformStudios\Events\Types\RecurringEvent;
use TransformStudios\Events\Types\SingleDayEvent;

class RecurringEventsTest extends TestCase
{
Expand Down Expand Up @@ -43,9 +44,9 @@ public function wontCreateRecurringEventWhenMultiDay()

$event = EventFactory::createFromEntry($recurringEntry);

$this->assertTrue($event instanceof MultiDayEvent);
$this->assertTrue($event instanceof SingleDayEvent);
$this->assertFalse($event->isRecurring());
$this->assertTrue($event->isMultiDay());
$this->assertFalse($event->isMultiDay());
}

/** @test */
Expand Down

0 comments on commit 38dfe6a

Please sign in to comment.