PHP code example of leads / core

1. Go to this page and download the library: Download leads/core 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/ */

    

leads / core example snippets


return [
    // ...
    Leads\Core\LeadsCoreBundle::class => ['all' => true],
];

use Leads\Core\Payload\Attribute\Clamp;
use Leads\Core\Payload\Attribute\FromHeader;
use Leads\Core\Payload\Attribute\FromPath;
use Leads\Core\Payload\Attribute\FromQuery;
use Leads\Core\Payload\Attribute\Payload;

final readonly class ListOrdersRequest
{
    public function __construct(
        #[FromQuery]
        #[Clamp(1, 100)]
        public int $limit = 20,
        #[FromQuery]
        public ?\DateTimeImmutable $fromDate = null,
        #[FromPath]
        public string $projectId,
        #[FromHeader('X-Request-Source')]
        public ?string $requestSource = null,
    ) {
    }
}

final class ListOrdersAction
{
    public function __invoke(#[Payload] ListOrdersRequest $request): JsonResponse
    {
        // $request is fully cast and validated here
    }
}

use Doctrine\DBAL\Connection;
use Leads\Core\Pagination\DBALPagination;
use Leads\Core\Pagination\DTO\ListResponseDTO;
use Leads\Core\Pagination\Pagination;

final readonly class OrderRepository
{
    public function __construct(
        private Connection $connection,
    ) {
    }

    public function findPaginated(int $page, int $perPage): ListResponseDTO
    {
        $qb = $this->connection->createQueryBuilder()
            ->select('id', 'customer_id', 'amount')
            ->from('orders')
            ->orderBy('id', 'DESC');

        return (new Pagination(
            page: $page,
            perPage: $perPage,
            pagination: new DBALPagination(
                connection: $this->connection,
                qb: $qb,
            ),
        ))->paginate();
    }
}

use ClickHouseDB\Client;
use Leads\Core\Pagination\ClickHousePagination;
use Leads\Core\Pagination\Pagination;

return (new Pagination(
    page: $page,
    perPage: $perPage,
    pagination: new ClickHousePagination(
        client: $client, // ClickHouseDB\Client
        qb: $qb,
    ),
))->paginate();

use Leads\Core\Action\BaseAction;
use Symfony\Component\HttpFoundation\JsonResponse;

final class CreateOrderAction extends BaseAction
{
    public function __invoke(CreateOrderRequest $request): JsonResponse
    {
        $this->validate($request); // throws ApiValidationException on violations

        $id = /* ... */;

        return $this->create201Response($id); // {"id": "..."}
    }
}

use Leads\Core\Exception\EntityNotFoundException;

$order = $repository->find($id)
    ?? throw new EntityNotFoundException(sprintf('Order "%s" not found.', $id));