Skip to content

Instantly share code, notes, and snippets.

@bastien70
Created May 28, 2021 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastien70/ba30f1aec444dcbfad6e7e84206d647f to your computer and use it in GitHub Desktop.
Save bastien70/ba30f1aec444dcbfad6e7e84206d647f to your computer and use it in GitHub Desktop.
API Platform - Custom resource - DailyStatsProvider file
<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\Pagination;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\DailyStats;
use App\Service\StatsHelper;
class DailyStatsProvider implements CollectionDataProviderInterface, ItemDataProviderInterface, RestrictedDataProviderInterface
{
public function __construct(private StatsHelper $statsHelper, private Pagination $pagination){}
public function getCollection(string $resourceClass, string $operationName = null)
{
list($page, $offset, $limit) = $this->pagination->getPagination($resourceClass, $operationName);
return new DataPaginator(
$page,
$limit,
$this->statsHelper->fetchMany($limit, $offset), //results for current page, or all results
$this->statsHelper->count() //global count, or null if the parameter just above contains all the results and not just those of the current page
);
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return $resourceClass === DailyStats::class;
}
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
return $this->statsHelper->fetchOne($id);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment