Skip to content

Commit 5402245

Browse files
authored
Added Rows flatMap (#46)
1 parent 9faddfa commit 5402245

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

‎src/Flow/ETL/Rows.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,16 @@ public function map(callable $callable) : self
113113
return new self(...\array_map($callable, $this->rows));
114114
}
115115

116+
/**
117+
* @psalm-param pure-callable(Row) : Row[] $callable
118+
*
119+
* @param callable(Row) : Row[] $callable
120+
*/
121+
public function flatMap(callable $callable) : self
122+
{
123+
return new self(...\array_merge(...\array_map($callable, $this->rows)));
124+
}
125+
116126
/**
117127
* @psalm-param pure-callable(Row) : void $callable
118128
*

‎tests/Flow/ETL/Tests/Unit/RowsTest.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,4 +342,33 @@ public function test_sorts_entries_in_all_rows() : void
342342
$sorted
343343
);
344344
}
345+
346+
public function test_flat_map() : void
347+
{
348+
$rows = new Rows(
349+
Row::create(
350+
new IntegerEntry('id', 1234),
351+
),
352+
Row::create(
353+
new IntegerEntry('id', 4567),
354+
)
355+
);
356+
357+
$rows = $rows->flatMap(function (Row $row) : array {
358+
return [
359+
$row->add(new StringEntry('name', $row->valueOf('id') . '-name-01')),
360+
$row->add(new StringEntry('name', $row->valueOf('id') . '-name-02')),
361+
];
362+
});
363+
364+
$this->assertSame(
365+
[
366+
['id' => 1234, 'name' => '1234-name-01'],
367+
['id' => 1234, 'name' => '1234-name-02'],
368+
['id' => 4567, 'name' => '4567-name-01'],
369+
['id' => 4567, 'name' => '4567-name-02'],
370+
],
371+
$rows->toArray()
372+
);
373+
}
345374
}

0 commit comments

Comments
 (0)