cacahouwete/lazy-api-collection

Handle rest api endpoint with pagination through an unique iterable

v0.1.0 2022-11-09 22:43 UTC

This package is auto-updated.

Last update: 2024-11-10 02:50:09 UTC


README

PHP tool to have an easy way to iterate an api with multiple pages

Installation with symfony

composer require cacahouwete/lazy-api-collection 

Add bundle in your symfony project

<?php
// config/bundles.php

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

Basic usage with symfony

<?php
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()
        ;
    }
}