Skip to content

Commit 8c6165d

Browse files
committed
Fix collection extractor
1 parent 988ac40 commit 8c6165d

File tree

2 files changed

+18
-35
lines changed

2 files changed

+18
-35
lines changed

src/Extractors/Collection.php

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Marquine\Etl\Extractors;
44

5+
use Marquine\Etl\Row;
6+
57
class Collection extends Extractor
68
{
79
/**
@@ -11,13 +13,6 @@ class Collection extends Extractor
1113
*/
1214
protected $columns;
1315

14-
/**
15-
* The extractor data collection.
16-
*
17-
* @var mixed
18-
*/
19-
protected $data;
20-
2116
/**
2217
* Properties that can be set via the options method.
2318
*
@@ -28,29 +23,18 @@ class Collection extends Extractor
2823
];
2924

3025
/**
31-
* Set up the extraction from the given source.
32-
*
33-
* @param mixed $source
34-
* @return void
35-
*/
36-
public function extract($source)
37-
{
38-
$this->data = $source;
39-
}
40-
41-
/**
42-
* Get the extractor iterator.
26+
* Extract data from the input.
4327
*
4428
* @return \Generator
4529
*/
46-
public function getIterator()
30+
public function extract()
4731
{
48-
foreach ($this->data as $row) {
32+
foreach ($this->input as $row) {
4933
if ($this->columns) {
50-
yield array_intersect_key($row, array_flip($this->columns));
51-
} else {
52-
yield $row;
34+
$row = array_intersect_key($row, array_flip($this->columns));
5335
}
36+
37+
yield new Row($row);
5438
}
5539
}
5640
}

tests/Extractors/CollectionTest.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Tests\Extractors;
44

55
use Tests\TestCase;
6+
use Marquine\Etl\Row;
67
use Marquine\Etl\Extractors\Collection;
78

89
class CollectionTest extends TestCase
910
{
10-
protected $source = [
11+
protected $input = [
1112
['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@email.com'],
1213
['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@email.com'],
1314
];
@@ -16,31 +17,29 @@ class CollectionTest extends TestCase
1617
public function default_options()
1718
{
1819
$expected = [
19-
['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@email.com'],
20-
['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@email.com'],
20+
new Row(['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@email.com']),
21+
new Row(['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@email.com']),
2122
];
2223

2324
$extractor = new Collection;
2425

25-
$extractor->extract($this->source);
26+
$extractor->input($this->input);
2627

27-
$this->assertEquals($expected, iterator_to_array($extractor));
28+
$this->assertEquals($expected, iterator_to_array($extractor->extract()));
2829
}
2930

3031
/** @test */
3132
public function custom_columns()
3233
{
3334
$expected = [
34-
['id' => 1, 'name' => 'John Doe'],
35-
['id' => 2, 'name' => 'Jane Doe'],
35+
new Row(['id' => 1, 'name' => 'John Doe']),
36+
new Row(['id' => 2, 'name' => 'Jane Doe']),
3637
];
3738

3839
$extractor = new Collection;
3940

40-
$extractor->options(['columns' => ['id', 'name']]);
41+
$extractor->input($this->input)->options(['columns' => ['id', 'name']]);
4142

42-
$extractor->extract($this->source);
43-
44-
$this->assertEquals($expected, iterator_to_array($extractor));
43+
$this->assertEquals($expected, iterator_to_array($extractor->extract()));
4544
}
4645
}

0 commit comments

Comments
 (0)