Skip to content

Commit

Permalink
Renamed CustomFilter to CallbackFilter to avoid confusion
Browse files Browse the repository at this point in the history
  • Loading branch information
Willem Vervuurt committed Jun 8, 2023
1 parent ebf50de commit 04ec7d1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
14 changes: 7 additions & 7 deletions docs/event-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Event filters do *not* combine; the user either applies 1 event filter, or none.
- [Available filter types](#available-filter-types)
- [`NovaResourceFilter`](#novaresourcefilter)
- [`ExcludeNovaResourceFilter`](#excludenovaresourcefilter)
- [`CustomFilter`](#customfilter)
- [`CallbackFilter`](#callbackfilter)
- A [custom event filter](#custom-event-filters)
- [Customization options](#customization-options)
- [Setting a default event filter](#setting-a-default-event-filter)
Expand All @@ -38,7 +38,7 @@ Then, add one or more instances of the following filters to the array in the ord

* [`NovaResourceFilter`](#novaresourcefilter) to show only Nova resources of one or more specific classes
* [`ExcludeNovaResourceFilter`](#excludenovaresourcefilter) to exclude Nova resources of one or more specific classes
* [`CustomFilter`](#excludenovaresourcefilter) to define your own filtering logic using a callback method
* [`CallbackFilter`](#callbackfilter) to define your own filtering logic using a callback method
* A [custom event filter](#custom-event-filters) subclass that you implement yourself for better code organization and filter reusability across calendar data providers

The first argument to a filter constructor is always the filter label, the rest of the arguments define how the filter works.
Expand Down Expand Up @@ -107,17 +107,17 @@ As with a `NovaResourceFilter`, the callback method receives the calendar event



### `CustomFilter`
### `CallbackFilter`
Define your own custom filtering logic using just a callback method.

```php
use Wdelfuego\NovaCalendar\EventFilter\CustomFilter;
use Wdelfuego\NovaCalendar\EventFilter\CallbackFilter;

public function filters() : array
{
return [
// Only show events that have an underlying Eloquent model that has an even id
new CustomFilter(__('Eloquent models with an even id'), function($event) { return $event->model() && $event->model()->id % 2 == 0; }),
new CallbackFilter(__('Eloquent models with an even id'), function($event) { return $event->model() && $event->model()->id % 2 == 0; }),
];
}
```
Expand All @@ -127,11 +127,11 @@ Since this filter is applied to any event on your calendar, the event is *not* s
In practice, this is only required if your `nonNovaEvents` method returns calendar events, because otherwise all events on your calendar have underlying Nova resources and Eloquent models anyway, but not doing those checks could cause problems in the future if you start using the `nonNovaEvents` method without checking your filter implementations.

### Custom event filters
Writing complex filtering logic into a `CustomFilter` callback method gets ugly quickly, so it's best to define custom filtering logic in your own filter classes. That has the added advantage of being able to reuse the filter across different calendar data providers and it's better for testing.
Writing complex filtering logic into a callback method gets ugly quickly, so it's best to define custom filtering logic in your own filter classes. That has the added advantage of being able to reuse the filter across different calendar data providers and it's better for testing.

Implement a subclass of `Wdelfuego\NovaCalendar\EventFilter\AbstractEventFilter`. The only method that you have to implement is `showEvent(Event $event): bool`. Implement it however you see fit and you can add an instance of the custom filter class to the `filters()` method in your CalendarDataProvider.

The inline `CustomFilter` example above, that only shows Eloquent models with an even `id`, would be implemented as a custom filter class as follows:
The inline `CallbackFilter` example above that only shows Eloquent models with an even `id` would be implemented as a custom filter class as follows:

```php
use Wdelfuego\NovaCalendar\EventFilter\AbstractEventFilter;
Expand Down
12 changes: 6 additions & 6 deletions src/EventFilter/AbstractEventFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ abstract class AbstractEventFilter implements EventFilterInterface
private string $key = '';
private string $label = '';
private bool $isDefault = false;
private $customFilter = null;
private $callbackFilter = null;

abstract public function showEvent(Event $event): bool;

Expand Down Expand Up @@ -65,21 +65,21 @@ public function isDefaultFilter() : bool
return $this->isDefault;
}

public function setCustomFilter($callable)
public function setCallbackFilter($callable)
{
$this->customFilter = $callable;
$this->callbackFilter = $callable;
}

protected function setKey(string $filterKey) : void
{
$this->key = $filterKey;
}

protected function passesCustomFilter(Event $event) : bool
protected function passesCallbackFilter(Event $event) : bool
{
if($this->customFilter)
if($this->callbackFilter)
{
return ($this->customFilter)($event);
return ($this->callbackFilter)($event);
}

return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@

use Wdelfuego\NovaCalendar\Event;

class CustomFilter extends AbstractEventFilter
class CallbackFilter extends AbstractEventFilter
{
public function __construct(string $label, $customFilter)
public function __construct(string $label, $callback)
{
parent::__construct($label);
$this->setCustomFilter($customFilter);
$this->setCallbackFilter($callback);
}

public function showEvent(Event $event): bool
{
return $this->passesCustomFilter($event);
return $this->passesCallbackFilter($event);
}
}
2 changes: 1 addition & 1 deletion src/EventFilter/ExcludeNovaResourceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function showEvent(Event $event): bool
{
if($event->hasNovaResource($novaResourceClass))
{
return !$this->passesCustomFilter($event);;
return !$this->passesCallbackFilter($event);;
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/EventFilter/NovaResourceFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class NovaResourceFilter extends AbstractEventFilter
{
protected $novaResourceClasses = [];

public function __construct(string $label, $novaResourceClasses, $customFilter = null)
public function __construct(string $label, $novaResourceClasses, $callbackFilter = null)
{
if(is_string($novaResourceClasses))
{
Expand All @@ -44,7 +44,7 @@ public function __construct(string $label, $novaResourceClasses, $customFilter =

parent::__construct($label);
$this->novaResourceClasses = $novaResourceClasses;
$this->setCustomFilter($customFilter);
$this->setCallbackFilter($callbackFilter);
}

public function showEvent(Event $event): bool
Expand All @@ -53,7 +53,7 @@ public function showEvent(Event $event): bool
{
if($event->hasNovaResource($novaResourceClass))
{
return $this->passesCustomFilter($event);
return $this->passesCallbackFilter($event);
}
}

Expand Down

0 comments on commit 04ec7d1

Please sign in to comment.