h4kuna/iterators

This package is abandoned and no longer maintained. The author suggests using the h4kuna/data-type package instead.

tools ala iterators

v2.0.2 2023-03-18 08:32 UTC

This package is auto-updated.

Last update: 2023-09-27 10:49:38 UTC


README

h4kuna\iterators

Downloads this Month Latest Stable Version Total Downloads License

Best way to install h4kuna\iterators is using composer.

$ composer require h4kuna/iterators

TextIterator

Read the text line by line.

$incomingString = "  foo

bar
joe";

$textIterator = new TextIterator($incomingString);
$textIterator->setFlags($textIterator::SKIP_EMPTY_LINE | $textIterator::TRIM_LINE);
foreach($textIterator as $line) {
    echo $line;
}
/*
 * output will be trimed
foo
bar
joe
*/

FlattenArrayIterator

Make one level array from multidimensional with to use delimiter for join keys.

use h4kuna\Iterators\FlattenArrayRecursiveIterator;

$input = [
    'address' => [
        'street' => 'foo',
        'zip' => 29404,
        'c' => [
            'p' => '5',
            'e' => 10.6,
        ],
    ],
    'main' => ['a', 'b', 'c'],
    'email' => 'exampl@foo.com',
];

$iterator = new FlattenArrayRecursiveIterator($input, '%');
$output = [];
foreach ($iterator as $key => $item) {
    $output[$key] = $item;
}

// output is
// [
//    'address%street' => 'foo',
//    'address%zip' => 29404,
//    'address%c%p' => '5',
//    'address%c%e' => 10.6,
//    'main%0' => 'a',
//    'main%1' => 'b',
//    'main%2' => 'c',
//    'email' => 'exampl@foo.com',
// ]

PeriodDayFactory

Iterate between dates by days. A time is reset to midnight.

use h4kuna\Iterators\PeriodDayFactory;
$endDate = new \DateTime('1996-04-09 08:00:00');
$period = PeriodDayFactory::createExFromInTo(new \DateTime('1989-02-01 07:00:00'), $endDate);

foreach ($period as $date) {
    // first date is 1989-02-02
    // last date is 1996-04-09
}