Skip to content

Commit

Permalink
Add Iterators
Browse files Browse the repository at this point in the history
  • Loading branch information
h4kuna committed Sep 13, 2023
1 parent 40f603c commit 7f86a74
Show file tree
Hide file tree
Showing 15 changed files with 722 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@ $ composer require h4kuna/data-type
- [Basic](src/Basic)
- [Collection](src/Collection)
- czech [Date](src/Date)
- [Iterators](src/Iterators)
- [GPS](src/Location)
- [Number](src/Number)
92 changes: 92 additions & 0 deletions src/Iterators/FlattenArrayIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php declare(strict_types=1);

namespace h4kuna\DataType\Iterators;

/**
* @example new \RecursiveIteratorIterator(new FlattenArrayIterator($data));
* @implements \RecursiveIterator<string, mixed>
*/
final class FlattenArrayIterator implements \RecursiveIterator
{

/**
* @var array<int|string>
*/
private array $keys = [];


/**
* @param array<mixed> $data
*/
public function __construct(private array $data, private string $delimiter = '-')
{
}


public function current(): mixed
{
return current($this->data);
}


public function next(): void
{
next($this->data);
}


public function key(): mixed
{
return implode($this->delimiter, $this->keys);
}


public function valid(): bool
{
$key = key($this->data);
if ($key === null) {
return false;
}
$lastKey = (int) array_key_last($this->keys);
$this->keys[$lastKey] = $key;

return isset($this->data[$key]);
}


public function rewind(): void
{
reset($this->data);
$this->keys[] = '';
}


public function hasChildren(): bool
{
return is_array($this->current());
}


/**
* @return \RecursiveIterator<string, mixed>
*/
public function getChildren(): ?\RecursiveIterator
{
$current = $this->current();
assert(is_array($current));
$child = new static($current, $this->delimiter);
$child->addKeys($this->keys);

return $child;
}


/**
* @param array<int|string> $keys
*/
protected function addKeys(array $keys): void
{
$this->keys = $keys;
}

}
19 changes: 19 additions & 0 deletions src/Iterators/FlattenArrayRecursiveIterator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php declare(strict_types=1);

namespace h4kuna\DataType\Iterators;

/**
* @extends \RecursiveIteratorIterator<FlattenArrayIterator>
*/
final class FlattenArrayRecursiveIterator extends \RecursiveIteratorIterator
{

/**
* @param array<mixed> $data
*/
public function __construct(array $data, string $delimiter = '-')
{
parent::__construct(new FlattenArrayIterator($data, $delimiter));
}

}
85 changes: 85 additions & 0 deletions src/Iterators/PeriodDayFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php declare(strict_types=1);

namespace h4kuna\DataType\Iterators;

use DateInterval;
use DatePeriod;
use DateTime;
use DateTimeImmutable;
use h4kuna\DataType\Date\Convert;

final class PeriodDayFactory
{

/**
* interval is (from, to> | exclude from, include to
*/
public static function createExFromInTo(
DateTimeImmutable|DateTime $start,
DateTimeImmutable|DateTime $end,
): DatePeriod
{
return new DatePeriod(
Convert::toImmutableMidnight($start),
self::createDayInterval(),
Convert::toImmutable($end)->modify('+1 day, midnight'),
DatePeriod::EXCLUDE_START_DATE
);
}


/**
* interval is <from, to> | exclude from, include to
*/
public static function createInFromInTo(
DateTimeImmutable|DateTime $start,
DateTimeImmutable|DateTime $end,
): DatePeriod
{
return new DatePeriod(
Convert::toImmutableMidnight($start),
self::createDayInterval(),
Convert::toImmutable($end)->modify('+1 day, midnight')
);
}


/**
* interval is (from, to) | exclude from, include to
*/
public static function createExFromExTo(
DateTimeImmutable|DateTime $start,
DateTimeImmutable|DateTime $end,
): DatePeriod
{
return new DatePeriod(
Convert::toImmutableMidnight($start),
self::createDayInterval(),
Convert::toImmutableMidnight($end),
DatePeriod::EXCLUDE_START_DATE
);
}


/**
* interval is <from, to) | exclude from, include to
*/
public static function createInFromExTo(
DateTimeImmutable|DateTime $start,
DateTimeImmutable|DateTime $end,
): DatePeriod
{
return new DatePeriod(
Convert::toImmutableMidnight($start),
self::createDayInterval(),
Convert::toImmutableMidnight($end)
);
}


private static function createDayInterval(): DateInterval
{
return new DateInterval('P1D');
}

}
81 changes: 81 additions & 0 deletions src/Iterators/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
TextIterator
------------

Read the text line by line.

```php
use h4kuna\DataType\Iterators\TextIterator;
$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.

```php
use h4kuna\DataType\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.

```php
use h4kuna\DataType\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
}

```

0 comments on commit 7f86a74

Please sign in to comment.