/DailyStatsProvider.php Secret
Created
May 28, 2021 10:19
API Platform - Custom resource - DailyStatsProvider file
This file contains hidden or 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