-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
722 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
``` |
Oops, something went wrong.