Skip to content

Commit

Permalink
Add offset parameter (#99)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpswsg authored Jan 28, 2025
1 parent 405941a commit 7191a4b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 0 deletions.
1 change: 1 addition & 0 deletions DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ Tag pair that returns the next X event dates.
* `event` - optional - if used, it gets occurrences from the given event only, and ignores `collection` parameter
* `limit` - required, number of occurrences to return
* `collapse_multi_days` - optional, only relevant on multi-day, all-day events. When `true`, multi-day events will only show up once in the event list.
* `offset` – optional – if used, it skips a specified number of occurrences.

*Example*:

Expand Down
13 changes: 13 additions & 0 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ class Events

private array $filters = [];

private ?int $offset = null;

private ?int $page = null;

private ?int $perPage = null;
Expand Down Expand Up @@ -92,6 +94,13 @@ public function filter(string $fieldCondition, $value): self
return $this;
}

public function offset(int $offset): self
{
$this->offset = $offset;

return $this;
}

public function pagination(int $page = 1, int $perPage = 10): self
{
$this->page = $page;
Expand Down Expand Up @@ -139,6 +148,10 @@ private function output(callable $type): EntryCollection|LengthAwarePaginator
{
$occurrences = $this->entries()->occurrences(generator: $type);

if ($this->offset) {
$occurrences = $occurrences->slice(offset: $this->offset);
}

return $this->page ? $this->paginate(occurrences: $occurrences) : $occurrences;
}

Expand Down
3 changes: 3 additions & 0 deletions src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ private function generator(): Generator
)->when(
value: $this->parseFilters(),
callback: fn (Generator $generator, array $filters) => $generator->filters(filters: $filters)
)-> when(
value: $this->params->int('offset'),
callback: fn (Generator $generator, int $offset) => $generator->offset(offset: $offset)
)->when(
value: $this->params->int('paginate'),
callback: fn (Generator $generator, int $perPage) => $generator->pagination(
Expand Down
120 changes: 120 additions & 0 deletions tests/Feature/EventsOffsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

namespace TransformStudios\Events\Tests\Feature;

use Illuminate\Support\Carbon;
use Statamic\Facades\Cascade;
use Statamic\Facades\Entry;
use TransformStudios\Events\Tags\Events;
use TransformStudios\Events\Tests\PreventSavingStacheItemsToDisk;
use TransformStudios\Events\Tests\TestCase;

class EventsOffsetTest extends TestCase
{
use PreventSavingStacheItemsToDisk;

private Events $tag;

public function setUp(): void
{
parent::setUp();

Entry::make()
->collection('events')
->slug('recurring-event')
->id('recurring-event')
->data([
'title' => 'Recurring Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '11:00',
'end_time' => '12:00',
'recurrence' => 'weekly',
'categories' => ['one'],
])->save();

$this->tag = app(Events::class);
}

/** @test */
public function canOffsetUpcomingOccurrences()
{
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

$this->tag
->setContext([])
->setParameters([
'collection' => 'events',
'limit' => 5,
'offset' => 2,
]);

$occurrences = $this->tag->upcoming();

$this->assertCount(3, $occurrences);
}

/** @test */
public function canOffsetBetweenOccurrences()
{
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

$this->tag->setContext([])
->setParameters([
'collection' => 'events',
'from' => Carbon::now()->toDateString(),
'to' => Carbon::now()->addWeek(3),
'offset' => 2,
]);

$occurrences = $this->tag->between();

$this->assertCount(2, $occurrences);
}

/** @test */
public function canOffsetTodayOccurrences()
{
Carbon::setTestNow(now()->setTimeFromTimeString('12:01'));

Entry::make()
->collection('events')
->slug('single-event')
->data([
'title' => 'Single Event',
'start_date' => Carbon::now()->toDateString(),
'start_time' => '13:00',
'end_time' => '15:00',
])->save();

$this->tag->setContext([])
->setParameters([
'collection' => 'events',
'offset' => 1,
]);

$this->assertCount(1, $this->tag->today());

$this->tag->setContext([])
->setParameters([
'collection' => 'events',
'ignore_finished' => true,
'offset' => 1,
]);

$this->assertCount(0, $this->tag->today());
}

/** @test */
public function canOffsetSingleDayOccurrences()
{
Carbon::setTestNow(now()->setTimeFromTimeString('10:00'));

$this->tag->setContext([])
->setParameters([
'collection' => 'events',
'offset' => 1,
]);

$this->assertCount(0, $this->tag->today());
}
}

0 comments on commit 7191a4b

Please sign in to comment.