<?php
namespace App\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\DataTransformer\AnalysisServiceWeightTransformer;
use App\Entity\AnalysisServiceWeight;
use App\Provider\AnalysisServiceWeightProvider;
use App\Request\AnalysisServiceWeightRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class AnalysisServiceWeightDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface, ItemDataProviderInterface
{
/**
* @var AnalysisServiceWeightTransformer
*/
private $transformer;
/**
* @var AnalysisServiceWeightProvider
*/
private $provider;
/**
* AnalysisServiceWeightDataProvider constructor.
*
* @param AnalysisServiceWeightTransformer $transformer
* @param AnalysisServiceWeightProvider $provider
*/
public function __construct(AnalysisServiceWeightTransformer $transformer, AnalysisServiceWeightProvider $provider)
{
$this->transformer = $transformer;
$this->provider = $provider;
}
public function getCollection(string $resourceClass, string $operationName = null)
{
$weights = $this->provider->getAnalysisServiceWeights();
$data = $weights->map(
function (AnalysisServiceWeight $weight) {
return $this->transformer->transform($weight);
}
);
return $data;
}
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = [])
{
$weight = $this->provider->getAnalysisServiceWeightById($id);
if ($weight === null) {
throw new NotFoundHttpException();
}
return $this->transformer->transform($weight);
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return AnalysisServiceWeightRequest::class === $resourceClass;
}
}