<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alexandrebulete / ddd-apiplatform-bridge example snippets
use AlexandreBulete\DddApiPlatformBridge\State\Paginator;
use AlexandreBulete\DddFoundation\Application\Query\QueryBusInterface;
class GetPostsProvider implements ProviderInterface
{
public function __construct(
private QueryBusInterface $queryBus,
) {}
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
{
$repository = $this->queryBus->ask(new GetPostsQuery(
page: $context['filters']['page'] ?? 1,
itemsPerPage: $context['filters']['itemsPerPage'] ?? 30,
));
$paginator = $repository->paginator();
if (null === $paginator) {
return iterator_to_array($repository);
}
return new Paginator(
items: $paginator,
currentPage: $paginator->getCurrentPage(),
itemsPerPage: $paginator->getItemsPerPage(),
lastPage: $paginator->getLastPage(),
totalItems: $paginator->getTotalItems(),
);
}
}
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\GetCollection;
#[ApiResource(
operations: [
new GetCollection(
provider: GetPostsProvider::class,
),
],
)]
class PostResource
{
public function __construct(
public string $id,
public string $title,
public string $content,
) {}
}
src/
└── State/
└── Paginator.php
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.