-
-
Save bastien70/ba30f1aec444dcbfad6e7e84206d647f to your computer and use it in GitHub Desktop.
API Platform - Custom resource - DailyStatsProvider file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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