Skip to content

Commit

Permalink
Update instantiation vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
cerbero90 committed Sep 6, 2024
1 parent d40a669 commit de070a9
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 15 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
[![Total Downloads][ico-downloads]][link-downloads]

```php
$lazyCollection = LazyJsonPages::from($source)
use Illuminate\Support\LazyCollection;

LazyCollection::fromJsonPages($source)
->totalPages('pagination.total_pages')
->async(requests: 5)
->throttle(requests: 60, perMinute: 1)
->collect('data.*');
```

Expand Down Expand Up @@ -51,21 +54,25 @@ composer require cerbero/lazy-json-pages

### 👣 Basics

Depending on our coding style, we can initialize Lazy JSON Pages in 3 different ways:
Depending on our coding style, we can initialize Lazy JSON Pages in 4 different ways:

```php
use Cerbero\LazyJsonPages\LazyJsonPages;
use Illuminate\Support\LazyCollection;

use function Cerbero\LazyJsonPages\lazyJsonPages;

// lazy collection macro
LazyCollection::fromJsonPages($source);

// classic instantiation
$lazyJsonPages = new LazyJsonPages($source);
new LazyJsonPages($source);

// static method (easier methods chaining)
$lazyJsonPages = LazyJsonPages::from($source);
// static method
LazyJsonPages::from($source);

// namespaced helper
$lazyJsonPages = lazyJsonPages($source);
lazyJsonPages($source);
```

The variable `$source` in our examples represents any [source](#-sources) that points to a paginated JSON API. Once we define the source, we can then chain methods to define how the API is paginated:
Expand Down
8 changes: 8 additions & 0 deletions bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

use Cerbero\LazyJsonPages\LazyJsonPages;
use Illuminate\Support\LazyCollection;

(static function() {
LazyCollection::macro('fromJsonPages', [LazyJsonPages::class, 'from']);
})();
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"Cerbero\\LazyJsonPages\\": "src"
},
"files": [
"bootstrap.php",
"helpers.php"
]
},
Expand Down
11 changes: 2 additions & 9 deletions helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@

namespace Cerbero\LazyJsonPages;

use Closure;
use Illuminate\Support\LazyCollection;

/**
* Load items from any paginated JSON API into a lazy collection.
*
* @param mixed $source
* @param Closure $config
* @return LazyCollection
*/
function lazyJsonPages(mixed $source, Closure $config): LazyCollection
function lazyJsonPages(mixed $source): LazyJsonPages
{
return LazyCollection::fromJsonPages($source, $config);
return LazyJsonPages::from($source);
}

0 comments on commit de070a9

Please sign in to comment.