PHP code example of cacahouwete / lazy-api-collection

1. Go to this page and download the library: Download cacahouwete/lazy-api-collection 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/ */

    

cacahouwete / lazy-api-collection example snippets



// config/bundles.php

return [
    ...
    LazyApiCollection\Bridge\Symfony\LazyApiCollectionBundle::class => ['all' => true],
]


namespace App\ApiEntity;

// src/ApiEntity/Dummy.php
final class Dummy {
    public string $field1;
    public string $field2;
    ....
}

// src/ApiRepository/DummyApiRepository.php
namespace App\ApiRepository;

use LazyApiCollection\Bridge\Symfony\Builder\LazyApiCollectionBuilderInterface;
use LazyApiCollection\Model\ApiCollection;
use LazyApiCollection\Model\LazyApiCollection;

final class DummyApiRepository
{
    private const PATH = '/api/dummies';
    
    private LazyApiCollectionBuilderInterface $lazyApiCollectionBuilder;
    private string $targetUrl;

    public function __construct(LazyApiCollectionBuilderInterface $lazyApiCollectionBuilder, string $targetUrl)
    {
        $this->lazyApiCollectionBuilder = $lazyApiCollectionBuilder;
        $this->targetUrl = $targetUrl;
    }

    /**
     * @return iterable<Dummy>
     */
    public function findAllByPageAndNbItem(): iterable
    {
        return $this->lazyApiCollectionBuilder
            ->create($this->targetUrl.self::PATH, Dummy::class)
            ->build()
        ;
    }
}