Skip to content

Commit

Permalink
replace excludeRunning() with full featured Period filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Kurowski committed Jan 11, 2022
1 parent 4093d10 commit 29df959
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ $parameters->setTargetGroup(\HnutiBrontosaurus\BisClient\Enums\TargetGroup::FIRS
// only events organized by organizational unit with ID 123
$parameters->setOrganizedBy(123);

// excludes running events
$parameters->excludeRunning();
// only events in given period
$parameters->setPeriod(\HnutiBrontosaurus\BisClient\Request\Event\Period::RUNNING_AND_FUTURE());

$events = $client->getEvents($parameters);
```
Expand Down
48 changes: 38 additions & 10 deletions src/Request/Event/EventParameters.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,40 @@ public function setTargetGroups(array $targetGroups): self
}


// period (defaults to unlimited)

// miscellaneous

private \DateTimeImmutable $dateFromGreaterThan;
private \DateTimeImmutable $dateFromGreaterThanOrEqualTo;
private \DateTimeImmutable $dateFromLessThanOrEqualTo;
private \DateTimeImmutable $dateToGreaterThanOrEqualTo;
private \DateTimeImmutable $dateToLessThanOrEqualTo;

/**
* Excludes events which are running (started, but not yet ended). Defaults to include them.
*/
public function excludeRunning(): self
public function setPeriod(Period $period): self
{
$this->dateFromGreaterThan = new \DateTimeImmutable();
if ($period->equals(Period::RUNNING_ONLY())) {
$this->dateFromLessThanOrEqualTo = new \DateTimeImmutable();
$this->dateToGreaterThanOrEqualTo = new \DateTimeImmutable();

} elseif ($period->equals(Period::FUTURE_ONLY())) {
$this->dateFromGreaterThanOrEqualTo = new \DateTimeImmutable();

} elseif ($period->equals(Period::PAST_ONLY())) {
$this->dateToLessThanOrEqualTo = new \DateTimeImmutable();

} elseif ($period->equals(Period::RUNNING_AND_FUTURE())) {
$this->dateToGreaterThanOrEqualTo = new \DateTimeImmutable();

} elseif ($period->equals(Period::RUNNING_AND_PAST())) {
$this->dateFromLessThanOrEqualTo = new \DateTimeImmutable();

} else {} // Period::UNLIMITED() – default

return $this;
}



// miscellaneous

public function orderByDateFrom(): self
{
$this->ordering = Ordering::DATE_FROM();
Expand Down Expand Up @@ -166,8 +185,17 @@ public function toArray(): array
'administrative_unit' => \implode(',', $this->organizedBy),
];

if (isset($this->dateFromGreaterThan)) {
$array['date_from__gte'] = $this->dateFromGreaterThan->format('Y-m-d');
if (isset($this->dateFromGreaterThanOrEqualTo)) {
$array['date_from__gte'] = $this->dateFromGreaterThanOrEqualTo->format('Y-m-d');
}
if (isset($this->dateFromLessThanOrEqualTo)) {
$array['date_from__lte'] = $this->dateFromLessThanOrEqualTo->format('Y-m-d');
}
if (isset($this->dateToGreaterThanOrEqualTo)) {
$array['date_to__gte'] = $this->dateToGreaterThanOrEqualTo->format('Y-m-d');
}
if (isset($this->dateToLessThanOrEqualTo)) {
$array['date_to__lte'] = $this->dateToLessThanOrEqualTo->format('Y-m-d');
}

return $array;
Expand Down
27 changes: 27 additions & 0 deletions src/Request/Event/Period.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php declare(strict_types = 1);

namespace HnutiBrontosaurus\BisClient\Request\Event;

use Grifart\Enum\AutoInstances;
use Grifart\Enum\Enum;


/**
* @method static Period UNLIMITED()
* @method static Period RUNNING_ONLY()
* @method static Period FUTURE_ONLY()
* @method static Period PAST_ONLY()
* @method static Period RUNNING_AND_FUTURE()
* @method static Period RUNNING_AND_PAST()
*/
final class Period extends Enum
{
use AutoInstances;

protected const UNLIMITED = 'unlimited';
protected const RUNNING_ONLY = 'runningOnly';
protected const FUTURE_ONLY = 'futureOnly';
protected const PAST_ONLY = 'pastOnly';
protected const RUNNING_AND_FUTURE = 'runningAndFuture';
protected const RUNNING_AND_PAST = 'runningAndPast';
}

0 comments on commit 29df959

Please sign in to comment.