Download the PHP package talesoft/tale-iterator without Composer
On this page you can find all versions of the php package talesoft/tale-iterator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download talesoft/tale-iterator
More information about talesoft/tale-iterator
Files in talesoft/tale-iterator
Package tale-iterator
Short Description A basic, PSR-7 compatible stream utility library
License MIT
Homepage http://docs.talesoft.codes/php/tale/iterator
Informations about the package tale-iterator
Tale Iterator
What is Tale Iterator?
Tale Iterator extends the SPL iterators by some more, useful iterators for common use-cases.
Installation
Usage
MapIterator
Maps values by a map()
-method. You mostly want to extend it
and override the map()
-method.
Stack with FlipIterator
to map keys
(see "How do I XYZ" at the bottom of this README)
CallbackMapIterator
Maps values by specifying a simple callback.
Stack with FlipIterator
to map keys
(see "How do I XYZ" at the bottom of this README)
FilterIterator
Filters values by an accept()
-method. You mostly want to extend
it and override the accept()
-method. It will preserve keys!.
If you want to reset the keys, chain a ValueIterator
as shown below.
Stack with FlipIterator
to filter keys
(see "How do I XYZ" at the bottom of this README)
CallbackFilterIterator
Filters values by specifying a simple callback.
Stack with FlipIterator
to filter keys
(see "How do I XYZ" at the bottom of this README)
Note:
PHP already has a FilterIterator and a CallbackFilterIterator, but it only accepts instances of \Iterator, which doesn't include \IteratorAggregate instances. This one accepts instances of \Traversable, which includes all iterables except for objects and native arrays (which are covered, too, keep reading). It uses the same API as the PHP implementation, though!
IterableIterator
This is a small utility iterator that turns any iterable
into a valid iterator.
It's equivalent to an \IteratorIterator,
that normalizes the passed iterable to
$iterable instanceof \Traversable ? $iterable : new \ArrayIterator($iterable)
.
With this iterator, you can pass any kind of iterable, arrays, objects, generators etc. to an iterator that only accepts \Iterator instances easily.
This is useful for PHPs SPL iterators or other iterator implementations that don't leverage
iterable
or \Traversable
and rely on \Iterator
only and/or do this for a very good reason
ValueIterator
This is basically array_values()
for iterators. This is useful
to e.g. reset the keys for FilterIterator
outputs.
KeyIterator
This is basically array_keys()
for iterators. This is useful
if you want to get a clean list of the inner
iterators keys.
FlipIterator
This iterator will flip keys and values. This is often useful if you want outer iterators act on keys rather than on values.
Through the way iterators work, as long as you don't flatten the iterator to an array, duplicate values won't result on dropped keys! Notice the second example to understand what I mean.
With array_flip
, duplicate values will lead to dropped keys, as array
keys have to be unique. With iterators, this isn't the case as long as
you don't actually flatten it!
FormatIterator
This is basically sprintf($format, $current)
on each value in the
iterator.
Stack with FlipIterator
to format keys
(see "How do I XYZ" at the bottom of this README)
PrefixIterator
This is $prefix.$current
for each value in the iterator.
Stack with FlipIterator
to prefix keys
(see "How do I XYZ" at the bottom of this README)
SuffixIterator
This is $current.$suffix
for each value in the iterator.
Stack with FlipIterator
to suffix keys
(see "How do I XYZ" at the bottom of this README)
IndexIterator
This iterator counts an independent index during iteration and makes
it available. This is useful to count the amount of iterations, mostly.
ValueIterator
and KeyIterator
use this to reset the keys.
How to do XYZ?
How to map keys instead of values?
Easy, through chaining a MapIterator and FlipIterators! Notice this doesn't create any additional overhead except for function calls. The internal array is as no point copied or even modified.
How to filter keys instead of values?
Here, again, the FlipIterator does everything you need!