Skip to content
Snippets Groups Projects
Commit 8740c887 authored by Reiter, Christoph's avatar Reiter, Christoph :snake:
Browse files

Add a simple entity & data provider

parent ed779f3b
No related branches found
No related tags found
No related merge requests found
Pipeline #105578 passed
<?php
declare(strict_types=1);
namespace DBP\API\StarterBundle\DataProvider;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use DBP\API\CoreBundle\Helpers\ArrayFullPaginator;
use DBP\API\StarterBundle\Entity\Place;
use DBP\API\StarterBundle\Service\PlaceProviderInterface;
final class PlaceCollectionDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
private $api;
public function __construct(PlaceProviderInterface $api)
{
$this->api = $api;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Place::class === $resourceClass;
}
public function getCollection(string $resourceClass, string $operationName = null, array $context = []): ArrayFullPaginator
{
$perPage = 30;
$page = 1;
$filters = $context['filters'] ?? [];
if (isset($filters['page'])) {
$page = (int) $filters['page'];
}
if (isset($filters['perPage'])) {
$perPage = (int) $filters['perPage'];
}
return new ArrayFullPaginator($this->api->getPlaces(), $page, $perPage);
}
}
<?php
declare(strict_types=1);
namespace DBP\API\StarterBundle\DataProvider;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use DBP\API\StarterBundle\Entity\Place;
use DBP\API\StarterBundle\Service\PlaceProviderInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
final class PlaceItemDataProvider implements ItemDataProviderInterface, RestrictedDataProviderInterface
{
private $api;
public function __construct(PlaceProviderInterface $api)
{
$this->api = $api;
}
public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
{
return Place::class === $resourceClass;
}
/**
* @param array|int|string $id
*/
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): Place
{
$place = $this->api->getPlaceById($id);
if ($place === null) {
throw new NotFoundHttpException();
}
return $place;
}
}
...@@ -13,10 +13,23 @@ class DbpStarterExtension extends Extension ...@@ -13,10 +13,23 @@ class DbpStarterExtension extends Extension
{ {
public function load(array $configs, ContainerBuilder $container) public function load(array $configs, ContainerBuilder $container)
{ {
$this->extendArrayParameter(
$container, 'api_platform.resource_class_directories', [__DIR__.'/../Entity']);
$loader = new YamlFileLoader( $loader = new YamlFileLoader(
$container, $container,
new FileLocator(__DIR__.'/../Resources/config') new FileLocator(__DIR__.'/../Resources/config')
); );
$loader->load('services.yaml'); $loader->load('services.yaml');
} }
private function extendArrayParameter(ContainerBuilder $container, string $parameter, array $values)
{
if (!$container->hasParameter($parameter)) {
$container->setParameter($parameter, []);
}
$oldValues = $container->getParameter($parameter);
assert(is_array($oldValues));
$container->setParameter($parameter, array_merge($oldValues, $values));
}
} }
<?php
declare(strict_types=1);
namespace DBP\API\StarterBundle\Entity;
use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* collectionOperations={"get"},
* itemOperations={"get"},
* iri="https://schema.org/Place",
* normalizationContext={"groups"={"Place:output"}, "jsonld_embed_context"=true}
* )
*/
class Place
{
/**
* @ApiProperty(identifier=true)
*/
private $identifier;
/**
* @ApiProperty(iri="https://schema.org/name")
* @Groups({"Place:output"})
*
* @var string
*/
private $name;
public function getName(): string
{
return $this->name;
}
public function setName(string $name): void
{
$this->name = $name;
}
public function getIdentifier(): string
{
return $this->identifier;
}
public function setIdentifier(string $identifier): void
{
$this->identifier = $identifier;
}
}
services: services:
DBP\API\StarterBundle\Command\TestCommand: DBP\API\StarterBundle\Command\TestCommand:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
\ No newline at end of file
DBP\API\StarterBundle\DataProvider\PlaceCollectionDataProvider:
tags: [{ name: 'api_platform.collection_data_provider'}]
autowire: true
DBP\API\StarterBundle\DataProvider\PlaceItemDataProvider:
tags: ['api_platform.item_data_provider']
autowire: true
DBP\API\StarterBundle\Service\ExternalApi:
autowire: true
autoconfigure: true
DBP\API\StarterBundle\Service\PlaceProviderInterface:
'@DBP\API\StarterBundle\Service\ExternalApi'
\ No newline at end of file
...@@ -4,9 +4,40 @@ declare(strict_types=1); ...@@ -4,9 +4,40 @@ declare(strict_types=1);
namespace DBP\API\StarterBundle\Service; namespace DBP\API\StarterBundle\Service;
class ExternalApi use DBP\API\StarterBundle\Entity\Place;
class ExternalApi implements PlaceProviderInterface
{ {
private $places;
public function __construct() public function __construct()
{ {
$this->places = [];
$place1 = new Place();
$place1->setIdentifier('graz');
$place1->setName('Graz');
$place2 = new Place();
$place2->setIdentifier('vienna');
$place2->setName('Vienna');
$this->places[] = $place1;
$this->places[] = $place2;
}
public function getPlaceById(string $identifier): ?Place
{
foreach ($this->places as $place) {
if ($place->getIdentifier() === $identifier) {
return $place;
}
}
return null;
}
public function getPlaces(): array
{
return $this->places;
} }
} }
<?php
declare(strict_types=1);
namespace DBP\API\StarterBundle\Service;
use DBP\API\StarterBundle\Entity\Place;
interface PlaceProviderInterface
{
public function getPlaceById(string $identifier): ?Place;
public function getPlaces(): array;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment