Skip to content

Commit 5b39f69

Browse files
committed
Fix fixed width extractor
1 parent f628fe7 commit 5b39f69

File tree

2 files changed

+11
-27
lines changed

2 files changed

+11
-27
lines changed

src/Extractors/FixedWidth.php

Lines changed: 6 additions & 22 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 FixedWidth extends Extractor
68
{
79
/**
@@ -11,13 +13,6 @@ class FixedWidth extends Extractor
1113
*/
1214
protected $columns;
1315

14-
/**
15-
* The source file.
16-
*
17-
* @var string
18-
*/
19-
protected $file;
20-
2116
/**
2217
* Properties that can be set via the options method.
2318
*
@@ -28,27 +23,16 @@ class FixedWidth 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->file = $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-
$handle = fopen($this->file, 'r');
32+
$handle = fopen($this->input, 'r');
4933

5034
while ($row = fgets($handle)) {
51-
yield $this->makeRow($row);
35+
yield new Row($this->makeRow($row));
5236
}
5337

5438
fclose($handle);

tests/Extractors/FixedWidthTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\Extractors;
44

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

89
class FixedWidthTest extends TestCase
@@ -11,16 +12,15 @@ class FixedWidthTest extends TestCase
1112
public function columns_start_and_length()
1213
{
1314
$expected = [
14-
['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@email.com'],
15-
['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@email.com'],
15+
new Row(['id' => 1, 'name' => 'John Doe', 'email' => 'johndoe@email.com']),
16+
new Row(['id' => 2, 'name' => 'Jane Doe', 'email' => 'janedoe@email.com']),
1617
];
1718

1819
$extractor = new FixedWidth;
1920

21+
$extractor->input(__DIR__.'/../data/fixed-width.txt');
2022
$extractor->options(['columns' => ['id' => [0, 1], 'name' => [1, 8], 'email' => [9, 17]]]);
2123

22-
$extractor->extract(__DIR__.'/../data/fixed-width.txt');
23-
24-
$this->assertEquals($expected, iterator_to_array($extractor));
24+
$this->assertEquals($expected, iterator_to_array($extractor->extract()));
2525
}
2626
}

0 commit comments

Comments
 (0)