Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/parse_spec'
Browse files Browse the repository at this point in the history
  • Loading branch information
holyshared committed Jun 18, 2016
2 parents 7121b01 + 11d2a22 commit eb9f4ac
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 31 deletions.
14 changes: 9 additions & 5 deletions README.md
Expand Up @@ -34,21 +34,23 @@ Read the CSV file
------------------------------------------------------

```hack
use hhpack\file\FileReader;
use hhpack\file\SeparatedFileReader;
use hhpack\file\ColumnSpecification;
$spec = new ColumnSpecification(',', '"');
$spec->addColumn(0, 'name');
$spec->addColumn(1, 'description');
$reader = SeparatedFileReader::fromString(__DIR__ . '/example.csv');
$reader = FileReader::fromString(__DIR__ . '/example.csv');
$csvReader = new SeparatedFileReader($reader, $spec);
foreach ($reader->records($spec) as $record) {
foreach ($csvReader->records() as $record) {
echo $record->get('name'), "\n";
echo $record->get('description'), "\n";
}
$reader->close();
$csvReader->close();
```

Customizing the reading of the record
Expand All @@ -58,6 +60,7 @@ Will create a parser that implements the **ParseSpecification**.
Then use the **ParsedFileReader**, and then apply the parser.

```hack
use hhpack\file\FileReader;
use hhpack\file\ParsedFileReader;
use hhpack\file\ParseSpecification;
Expand All @@ -70,9 +73,10 @@ final class CustomRecordSpecification implements ParseSpecification<array<string
}
$spec = new CustomRecordSpecification();
$reader = ParsedFileReader::fromString(__DIR__ . '/example.csv');
$reader = FileReader::fromString(__DIR__ . '/example.csv');
$csvReader = new ParsedFileReader($reader, $spec);
foreach ($reader->records($spec) as $values) {
foreach ($csvReader->records() as $values) {
echo $values[0], "\n";
echo $values[1], "\n";
}
Expand Down
9 changes: 5 additions & 4 deletions spec/SeparatedFileReaderSpec.hh
@@ -1,6 +1,7 @@
<?hh //partial

use hhpack\file\File;
use hhpack\file\FileReader;
use hhpack\file\ColumnSpecification;
use hhpack\file\SeparatedFileReader;

Expand All @@ -12,8 +13,8 @@ describe(SeparatedFileReader::class, function() {
$spec->addColumn(1, 'title');

$this->spec = $spec;
$this->path = realpath(__DIR__ . '/fixtures/text.csv');
$this->reader = SeparatedFileReader::fromString($this->path);
$this->fileReader = FileReader::fromString(realpath(__DIR__ . '/fixtures/text.csv'));
$this->reader = new SeparatedFileReader($this->fileReader, $this->spec);
});
describe('#fromString()', function() {
it('return file reader', function() {
Expand All @@ -23,15 +24,15 @@ describe(SeparatedFileReader::class, function() {
describe('#records()', function() {
beforeEach(function() {
$concatValue = '';
$records = $this->reader->records($this->spec);
$records = $this->reader->records();

foreach ($records as $record) {
$concatValue .= (string) $record->get('name');
$concatValue .= (string) $record->get('title');
}
$this->concatValue = $concatValue;
});
it('return Generator<int, SeparatedRecord, void>', function() {
it('return Iterator<SeparatedRecord>', function() {
expect($this->concatValue)->toBe("foo1bar1foo2bar2");
});
});
Expand Down
18 changes: 6 additions & 12 deletions src/ParsedFileReader.hh
Expand Up @@ -16,23 +16,17 @@ use \Generator;
final class ParsedFileReader<T>
{

private FileReader $reader;

public function __construct(FileReader $reader)
{
$this->reader = $reader;
}

public static function fromString(string $filePath) : ParsedFileReader<T>
public function __construct(
private FileReader $reader,
private ParseSpecification<T> $spec
)
{
$reader = FileReader::fromString($filePath);
return new self($reader);
}

public function records(ParseSpecification<T> $spec) : Generator<int, T, void>
public function records() : Iterator<T>
{
foreach ($this->reader->lines() as $line) {
yield $spec->parse($line);
yield $this->spec->parse($line);
}
}

Expand Down
17 changes: 7 additions & 10 deletions src/SeparatedFileReader.hh
Expand Up @@ -19,20 +19,17 @@ final class SeparatedFileReader

private ParsedFileReader<SeparatedRecord> $reader;

public function __construct(FileReader $reader)
public function __construct(
FileReader $reader,
ColumnSpecification $spec
)
{
$this->reader = new ParsedFileReader($reader);
$this->reader = new ParsedFileReader($reader, $spec);
}

public static function fromString(string $filePath) : SeparatedFileReader
public function records() : Iterator<SeparatedRecord>
{
$reader = FileReader::fromString($filePath);
return new self($reader);
}

public function records(ColumnSpecification $spec) : Generator<int, SeparatedRecord, void>
{
return $this->reader->records($spec);
return $this->reader->records();
}

public function close() : void
Expand Down

0 comments on commit eb9f4ac

Please sign in to comment.