gacek85/collection-iterator

There is no license information available for the latest version (1.0.1) of this package.

Helps iterating big sets of data

1.0.1 2016-09-29 07:31 UTC

This package is not auto-updated.

Last update: 2025-01-22 22:00:48 UTC


README

Build Status

#Collection Iterator

An implementation of SPL's Iterator for traversing large collections of data minimising the risk of memory limit exhaustion. Consists of an iterator service class Gacek85\Collection\CollectionIterator and an interface Gacek85\Collection\IterableDataProvider for a provider that will be providing chunks of data to the iterator service.

##How it works

The iterator loads the data using the provider chunk by chunk, previously loaded data are wiped out of the memory by the garbage collector.

##Public API

###CollectionIterator

setPerPage(int $perPage): CollectionIterator // Sets the number of items loaded at once
count(): int	// Returns the total count of the elements that will be traversed

Other public methods are Iterator specific API.

###IterableDataProvider

getTotal(): int // Returns the total count of available elements
getChunk(int $offset, int $limit): array|Iterator //Returns the chunk of data from given offset limited with given limit

##Usage

<?php

$provider = new \My\Provider(); // An implementation of Gacek85\Collection\IterableDataProvider
$iterator = new \Gacek85\Collection\CollectionIterator($provider);

foreach ($iterator as $k => $item) {
	// Do your stuff here with the item
}