Custom Filter for Custom Resources

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

Let's create one more custom filter: this time for DailyStats. Here's our mission: to be able to add a ?from= date on the URL. For example, if we go to /api/daily-stats.jsonld, we see all the stats. But if we add ?from=2020-09-01, we want to only show stats on that day or later.

Let's do this! Start basically the same way as before: in the src/ApiPlatform/ directory, create a new PHP class called DailyStatsDateFilter:

... lines 1 - 2
namespace App\ApiPlatform;
... lines 4 - 7
class DailyStatsDateFilter implements FilterInterface
{
... lines 10 - 16
}

Creating the Filter Class

For the search filter, we extended AbstractFilter:

... lines 1 - 4
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\AbstractFilter;
... lines 6 - 8
class CheeseSearchFilter extends AbstractFilter
{
... lines 11 - 36
}

but that class only works for entity resources. This time, implement FilterInterface. Now, be careful. There are multiple FilterInterface in API platform. One is for the ORM, and even inside the main part of API Platform, there are 2: one in Serializer and one in Api. Get the one in Serializer:

... lines 1 - 4
use ApiPlatform\Core\Serializer\Filter\FilterInterface;
... lines 6 - 7
class DailyStatsDateFilter implements FilterInterface
{
... lines 10 - 16
}

This FilterInterface actually extends the other one from Api\ but it has extra integration that will make our life much easier.

Back in the new class, go to "Code"->"Generate" - or Command + N on a Mac - and hit "Implement Methods" to add the two we need... which are quite similar to the two methods we had before:

... lines 1 - 7
class DailyStatsDateFilter implements FilterInterface
{
public function apply(Request $request, bool $normalization, array $attributes, array &$context)
{
}
public function getDescription(string $resourceClass): array
{
}
}

The only difference is that the apply() method has different arguments than filterProperty()... which makes sense because that method was all about querying via Doctrine.

Before we start filling this in, go to src/Entity/DailyStats.php so we can use the filter. So @ApiFilter(), hit tab to auto-complete that - and DailyStatsDateFilter::class... but this time we need to manually add the use statement: use DailyStatsDateFilter:

... lines 1 - 8
use App\ApiPlatform\DailyStatsDateFilter;
... lines 10 - 11
/**
... lines 13 - 22
* @ApiFilter(DailyStatsDateFilter::class)
*/
class DailyStats
{
... lines 27 - 58
}

Now that we've activated the filter for this resource, let's jump straight into getDescription(). This will be exactly like what we did in CheeseSearchFilter. In fact, let's go steal our code! Copy the return statement from the other filter... and paste it here. This time the query parameter will be called from, keep 'property' => null, type string is good, required false is good, but tweak the description:

From date e.g. 2020-09-01

... lines 1 - 7
class DailyStatsDateFilter implements FilterInterface
{
... lines 10 - 13
public function getDescription(string $resourceClass): array
{
return [
'from' => [
'property' => null,
'type' => 'string',
'required' => false,
'openapi' => [
'description' => 'From date e.g. 2020-09-01',
],
]
];
}
}

The apply() method is still empty, but this should be enough to see the filter in the docs. Refresh that page, open the DailyStats collection operation and... there it is!

Adding the Filter Logic

But... how can we make this filter actually work? Well, forget about the filter system for a minute. No matter what fanciness we do, it's 100% up to our data provider to return the collection of data that will be shown.

What I mean is, we're not going to return this DailyStatsPaginator object and then expect some other system to somehow magically filter those results:

... lines 1 - 13
class DailyStatsProvider implements CollectionDataProviderInterface, ItemDataProviderInterface, RestrictedDataProviderInterface
{
... lines 16 - 24
public function getCollection(string $resourceClass, string $operationName = null)
{
list($page, $offset, $limit) = $this->pagination->getPagination($resourceClass, $operationName);
return new DailyStatsPaginator(
$this->statsHelper,
$page,
$limit
);
}
... lines 35 - 44
}

Nope, if we want DailyStats to have some filters, we need to handle that logic inside getCollection() so that the items inside the paginator represent the filtered items.

So before we really think about reading query parameters or integrating with our filter class, let's first focus on making our DailyStatsProvider able to filter the results by date.

And actually, most of the work of figuring out which results to return is done by the paginator. So let's jump into DailyStatsPaginator.

Down here, getIterator() is where we use StatsHelper to fetch all of the objects:

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 46
public function getIterator()
{
if ($this->dailyStatsIterator === null) {
$offset = (($this->getCurrentPage() - 1) * $this->getItemsPerPage());
$this->dailyStatsIterator = new \ArrayIterator(
$this->statsHelper->fetchMany(
$this->getItemsPerPage(),
$offset
)
);
}
return $this->dailyStatsIterator;
}
}

And, it already has a type of filtering: it returns a subset of the items based on what page we're on.

And fortunately, if we jump into src/Service/StatsHelper.php, the fetchMany() method already has a third argument called $criteria, which supports from and to keys, which are both DateTime objects:

... lines 1 - 7
class StatsHelper
{
... lines 10 - 16
/**
* @param array An array of criteria to limit the results
* Supported keys are:
* * from DateTimeInterface
* * to DateTimeInterface
* @return array|DailyStats[]
*/
public function fetchMany(int $limit = null, int $offset = null, array $criteria = [])
{
... lines 26 - 56
}
... lines 58 - 111
}

Basically, we can already pass a from key on the 3rd argument to filter exactly like we want. The boring work was done for us. Yay!

Ok, let's think: in order for DailyStatsPaginator to be able to return the correct results, it needs to know what the from date is. To store that info, add a property: a new private $fromDate. Above this, I'll document that this will be a \DateTimeInterface or null if it's not set:

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 13
/**
* @var \DateTimeInterface|null
*/
private $fromDate;
... lines 18 - 75
}

Then, at the bottom, go to "Code"->"Generate" - or Command+N on a Mac - select setters, and generate the setFromDate() method. Oh, but I'll remove the nullable type:

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 71
public function setFromDate(\DateTimeInterface $fromDate)
{
$this->fromDate = $fromDate;
}
}

If you call this method... I'll assume that you do have a DateTime that you want to use. We'll see who sets this in a minute.

But assuming that this is set, we can use it inside of getIterator(). Add $criteria equals an empty array. Then, if we have $this->fromDate, say $criteria['from'] = $this->fromDate:

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 50
public function getIterator()
{
if ($this->dailyStatsIterator === null) {
$offset = (($this->getCurrentPage() - 1) * $this->getItemsPerPage());
$criteria = [];
if ($this->fromDate) {
$criteria['from'] = $this->fromDate;
}
... lines 60 - 66
}
... lines 68 - 69
}
... lines 71 - 75
}

Finally, pass $criteria as that third argument to fetchMany():

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 50
public function getIterator()
{
if ($this->dailyStatsIterator === null) {
... lines 54 - 59
$this->dailyStatsIterator = new \ArrayIterator(
$this->statsHelper->fetchMany(
$this->getItemsPerPage(),
$offset,
$criteria
)
);
}
... lines 68 - 69
}
... lines 71 - 75
}

This class is ready! Let's make sure it's working by going to DailyStatsProvider and hard-coding a date temporarily. Let's see, remove the return statement, add $paginator = and, at the bottom return $paginator. In between add: $paginator->setFromDate(), with a new \DateTime() and 2020-08-30:

... lines 1 - 13
class DailyStatsProvider implements CollectionDataProviderInterface, ItemDataProviderInterface, RestrictedDataProviderInterface
{
... lines 16 - 24
public function getCollection(string $resourceClass, string $operationName = null)
{
list($page, $offset, $limit) = $this->pagination->getPagination($resourceClass, $operationName);
$paginator = new DailyStatsPaginator(
$this->statsHelper,
$page,
$limit
);
$paginator->setFromDate(new \DateTime('2020-08-30'));
return $paginator;
}
... lines 38 - 47
}

Let's see if it works! Back at the browser, I'll go over to my other tab. I do have a ?from=, but that query parameter is not being used yet: the filtering should use the hardcoded date.

When we refresh... let's see. Yes! It returned only 5 results starting with the hard-coded 08-30! Awesome!

Oh, but I do see one, teenie-tiny bug: it says hydra:totalItems 30. That should only say 5.

This number comes from our paginator object: the getTotalItems() method, which calls $this->statsHelper->count():

... lines 1 - 7
class DailyStatsPaginator implements PaginatorInterface, \IteratorAggregate
{
... lines 10 - 30
public function getTotalItems(): float
{
return $this->statsHelper->count();
}
... lines 35 - 75
}

Really, the count() method should also allow an array of $criteria to be passed so that it can do the same filtering that happens in fetchMany(). But... I'll leave that for you as extra credit.

Next: this is good! We have the filtering logic working. But how can we fetch the real data from the query parameter? Well... I sort of just answered my own question! We could grab the Request object and read the query parameter directly. But there's actually a cooler, more built-in way that involves our DailyStatsDateFilter class.

Leave a comment!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": "^7.2.5",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/core": "^2.1", // v2.5.7
        "doctrine/annotations": "^1.0", // 1.10.4
        "doctrine/doctrine-bundle": "^2.0", // 2.1.2
        "doctrine/doctrine-migrations-bundle": "^3.0", // 3.0.1
        "doctrine/orm": "^2.4.5", // v2.7.3
        "nelmio/cors-bundle": "^2.1", // 2.1.0
        "nesbot/carbon": "^2.17", // 2.39.1
        "phpdocumentor/reflection-docblock": "^3.0 || ^4.0", // 4.3.4
        "ramsey/uuid-doctrine": "^1.6", // 1.6.0
        "symfony/asset": "5.1.*", // v5.1.5
        "symfony/console": "5.1.*", // v5.1.5
        "symfony/debug-bundle": "5.1.*", // v5.1.5
        "symfony/dotenv": "5.1.*", // v5.1.5
        "symfony/expression-language": "5.1.*", // v5.1.5
        "symfony/flex": "^1.1", // v1.9.6
        "symfony/framework-bundle": "5.1.*", // v5.1.5
        "symfony/http-client": "5.1.*", // v5.1.5
        "symfony/monolog-bundle": "^3.4", // v3.5.0
        "symfony/security-bundle": "5.1.*", // v5.1.5
        "symfony/twig-bundle": "5.1.*", // v5.1.5
        "symfony/validator": "5.1.*", // v5.1.5
        "symfony/webpack-encore-bundle": "^1.6", // v1.7.3
        "symfony/yaml": "5.1.*" // v5.1.5
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3", // 3.3.2
        "symfony/browser-kit": "5.1.*", // v5.1.5
        "symfony/css-selector": "5.1.*", // v5.1.5
        "symfony/maker-bundle": "^1.11", // v1.21.1
        "symfony/phpunit-bridge": "5.1.*", // v5.1.5
        "symfony/stopwatch": "5.1.*", // v5.1.5
        "symfony/twig-bundle": "5.1.*", // v5.1.5
        "symfony/web-profiler-bundle": "5.1.*", // v5.1.5
        "zenstruck/foundry": "^1.1" // v1.1.2
    }
}