PHP code example of tobento / service-iterable

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

    

tobento / service-iterable example snippets


use Tobento\Service\Iterable\Iter;

$iterable = ['key' => 'value'];

$array = Iter::toArray(iterable: $iterable);

use Tobento\Service\Iterable\Iter;
use Iterator;

$iterable = ['key' => 'value'];

$iterator = Iter::toIterator(iterable: $iterable);

var_dump($iterator instanceof Iterator);
// bool(true)

use Tobento\Service\Iterable\ChunkIterator;

$iterator = new ChunkIterator(
    iterable: range(1, 10),
    chunkLength: 2,
);

foreach($iterator as $chunk) {} // [1, 2], [3, 4], ...

use Tobento\Service\Iterable\ModifyIterator;

$iterator = new ModifyIterator(
    iterable: range(1, 5),
    modifier: function(int $number): int {
        return $number * 10;
    }
);

foreach($iterator as $number) {} // 10, 20, ...

use Tobento\Service\Iterable\ItemFactoryIterator;
use Tobento\Service\Seeder\Str;
use Tobento\Service\Seeder\Arr;

$callable = function(): array {
    return [
        'name' => Str::string(10),
        'color' => Arr::item(['green', 'red', 'blue']),
    ];
};

$iterator = new ItemFactoryIterator(
    factory: $callable,
    create: 10
);

use Tobento\Service\Iterable\JsonFileIterator;

$iterator = new JsonFileIterator(
    file: 'private/src/file.json',
);

foreach($iterator as $key => $item) {}