1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- <?php
- namespace App\DataProvider;
- use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
- use ApiPlatform\Core\DataProvider\ContextAwareCollectionDataProviderInterface;
- use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
- use App\Entity\WebflowCollection;
- use App\Entity\WebflowItem;
- use App\Http\WebflowApi\WebflowApiCollection;
- use App\Http\WebflowApi\WebflowSite;
- use App\Http\WebflowApiClient;
- use Psr\Log\LoggerInterface;
- final class WebflowDataProvider implements ContextAwareCollectionDataProviderInterface, RestrictedDataProviderInterface {
- private $site;
- private $apiClient;
- private $logger;
- public function __construct(WebflowApiClient $webflowApiClient, LoggerInterface $l) {
- $this->site = WebflowSite::byId($webflowApiClient, '5ebabfe546c816388d66c03a');
- $this->apiClient = $webflowApiClient;
- $this->logger= $l;
- }
- public function supports(string $resourceClass, ?string $operationName = null, array $context = []): bool
- {
- return in_array($resourceClass, [
- WebflowCollection::class,
- WebflowItem::class,
- ]);
- }
- public function getCollection(string $resourceClass, ?string $operationName = null, array $context = []): \Generator
- {
- switch ($resourceClass) {
- case WebflowCollection::class:
- foreach($this->site->getCollections() as $col)
- yield new WebflowCollection($col);
- break;
- case WebflowItem::class:
- if (isset($context['filters']) && isset($context['filters']['cid'])) {
- $col = WebflowApiCollection::byId($this->apiClient, $context['filters']['cid']);
- foreach ($col->getItems() as $item)
- yield new WebflowItem($item);
- break;
- }
- foreach ($this->site->getCollections() as $col)
- foreach ($col->getItems() as $item)
- yield new WebflowItem($item);
- break;
- }
- return null;
- }
- }
|