PHP code example of lshamanl / symfony-ui-bundle-query

1. Go to this page and download the library: Download lshamanl/symfony-ui-bundle-query 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/ */

    

lshamanl / symfony-ui-bundle-query example snippets


use App\Path\To\Entity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Nelmio\ApiDocBundle\Annotation\Model;
use SymfonyBundle\UIBundle\Foundation\Core\Contract\ApiFormatter;
use SymfonyBundle\UIBundle\Foundation\Core\Dto\OutputFormat;
use SymfonyBundle\UIBundle\Query\Core\CQRS\Query\Aggregate\Processor as AggregateProcessor;
use SymfonyBundle\UIBundle\Query\Core\CQRS\Query\Aggregate\Context as AggregateContext;

class Controller {
    /**
     * @Route("/{id}.{_format}", methods={"GET"}, name=".read", defaults={"_format"="json"})
     * @OA\Response(
     *     response=200,
     *     description="Read Entity",
     *     @OA\JsonContent(
     *         allOf={
     *             @OA\Schema(ref=@Model(type=ApiFormatter::class)),
     *             @OA\Schema(type="object",
     *                 @OA\Property(
     *                     property="data",
     *                     type="object",
     *                     @OA\Property(
     *                         property="entity",
     *                         ref=@Model(type=UseCase\CommonOutputContract::class)
     *                     )
     *                 ),
     *                 @OA\Property(
     *                     property="status",
     *                     example="200"
     *                )
     *             )
     *         }
     *     )
     * )
     */
    public function read(
        string $id,
        AggregateProcessor $processor,
        OutputFormat $outputFormat
    ): Response {
        $context = new AggregateContext(
            outputFormat: $outputFormat->getFormat(),
            entityId: $id,
            targetEntityClass: User::class,
            outputDtoClass: UseCase\CommonOutputContract::class,
        );

        $processor->process($context);
        return $processor->makeResponse();
    }

use App\Path\To\Entity;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Nelmio\ApiDocBundle\Annotation\Model;
use SymfonyBundle\UIBundle\Foundation\Core\Contract\ApiFormatter;
use SymfonyBundle\UIBundle\Foundation\Core\Dto\OutputFormat;
use SymfonyBundle\UIBundle\Query\Core\Contract\Filter\FilterSortPagination;
use SymfonyBundle\UIBundle\Query\Core\CQRS\Query\Search\Processor as SearchProcessor;
use SymfonyBundle\UIBundle\Query\Core\CQRS\Query\Search\Context as SearchContext;
use SymfonyBundle\UIBundle\Query\Core\Service\Filter\SearchQuery;

class Controller {
    /**
     * @Route(".{_format}", methods={"GET"}, name=".search", defaults={"_format"="json"})
     * @OA\Get(
     *     @OA\Parameter(
     *          name="searchParams",
     *          in="query",
     *               *                      example="200"
     *                 )
     *              )
     *          }
     *      )
     * )
     */
    public function search(
        SearchProcessor $processor,
        SearchQuery $searchQuery,
        OutputFormat $outputFormat
    ): Response {
        $context = new SearchContext(
            targetEntityClass: User::class,
            outputFormat: $outputFormat->getFormat(),
            outputDtoClass: UseCase\CommonOutputContract::class,
            filterBlackList: ['id'],
            pagination: $searchQuery->getPagination(),
            filters: $searchQuery->getFilters(),
            sorts: $searchQuery->getSorts()
        );

        $processor->process($context);

        return $processor->makeResponse();
    }
}