Skip to content

Commit

Permalink
Ability to sort occurrences (#77)
Browse files Browse the repository at this point in the history
  • Loading branch information
edalzell authored Dec 22, 2023
1 parent d6acea9 commit 3007b3e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 3 deletions.
4 changes: 3 additions & 1 deletion DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,9 @@ Currently only simple taxonomy filtering is supported, using the same syntax as
{{ /events:between }}
```

Further filtering is coming.
**Sorting**

By default the occurrenes are sorted in ascending order, to get them in descending order, use `sort="desc"` on any of the tags.

### Between Tag

Expand Down
3 changes: 2 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php" colors="true" processIsolation="false" stopOnFailure="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Unit">
Expand All @@ -21,4 +21,5 @@
<env name="MAIL_DRIVER" value="array"/>
<env name="MAILCHIMP_DRIVER" value="log"/>
</php>
<source/>
</phpunit>
11 changes: 10 additions & 1 deletion src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Events

private ?string $site = null;

private string $sort = 'asc';

private array $terms = [];

public static function fromCollection(string $handle): self
Expand Down Expand Up @@ -102,6 +104,13 @@ public function site(string $handle): self
return $this;
}

public function sort(string $direction): self
{
$this->sort = $direction;

return $this;
}

public function terms(string|array $terms): self
{
$this->terms = Arr::wrap($terms);
Expand Down Expand Up @@ -157,7 +166,7 @@ private function occurrences(callable $generator): EntryCollection
return $this->entries
// take each event and generate the occurences
->flatMap(callback: $generator)
->sortBy(fn (Entry $occurrence) => $occurrence->start)
->sortBy(callback: fn (Entry $occurrence) => $occurrence->start, descending: $this->sort === 'desc')
->values();
}

Expand Down
1 change: 1 addition & 0 deletions src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ private function generator(): Generator
Generator::fromCollection($this->params->get('collection', 'events'));

return $generator
->sort($this->params->get('sort', 'asc'))
->when(
value: $this->parseTerms(),
callback: fn (Generator $generator, array $terms) => $generator->terms(terms: $terms)
Expand Down
18 changes: 18 additions & 0 deletions tests/Feature/TagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -341,4 +341,22 @@ public function canGenerateEventDownloadLink()

$this->assertEquals('http://localhost/!/events/ics?collection=events&event=recurring-event', $url);
}

/** @test */
public function canSortOccurrencesDesc()
{

$this->tag
->setContext([])
->setParameters([
'collection' => 'events',
'limit' => 3,
'sort' => 'desc',
]);

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

$this->assertTrue($occurrences[0]->start->isAfter($occurrences[1]->start));
$this->assertTrue($occurrences[1]->start->isAfter($occurrences[2]->start));
}
}

0 comments on commit 3007b3e

Please sign in to comment.