PHP code example of alexandrebulete / ddd-apiplatform-bridge

1. Go to this page and download the library: Download alexandrebulete/ddd-apiplatform-bridge library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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