diff --git a/README.md b/README.md index 1fcfb80..a20905e 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ Read the CSV file ------------------------------------------------------ ```hack +use hhpack\file\FileReader; use hhpack\file\SeparatedFileReader; use hhpack\file\ColumnSpecification; @@ -41,14 +42,15 @@ $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 @@ -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; @@ -70,9 +73,10 @@ final class CustomRecordSpecification implements ParseSpecificationrecords($spec) as $values) { +foreach ($csvReader->records() as $values) { echo $values[0], "\n"; echo $values[1], "\n"; } diff --git a/spec/SeparatedFileReaderSpec.hh b/spec/SeparatedFileReaderSpec.hh index 40566d7..e6e9051 100644 --- a/spec/SeparatedFileReaderSpec.hh +++ b/spec/SeparatedFileReaderSpec.hh @@ -1,6 +1,7 @@ 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() { @@ -23,7 +24,7 @@ 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'); @@ -31,7 +32,7 @@ describe(SeparatedFileReader::class, function() { } $this->concatValue = $concatValue; }); - it('return Generator', function() { + it('return Iterator', function() { expect($this->concatValue)->toBe("foo1bar1foo2bar2"); }); }); diff --git a/src/ParsedFileReader.hh b/src/ParsedFileReader.hh index 8a86d62..da1095a 100644 --- a/src/ParsedFileReader.hh +++ b/src/ParsedFileReader.hh @@ -16,23 +16,17 @@ use \Generator; final class ParsedFileReader { - private FileReader $reader; - - public function __construct(FileReader $reader) - { - $this->reader = $reader; - } - - public static function fromString(string $filePath) : ParsedFileReader + public function __construct( + private FileReader $reader, + private ParseSpecification $spec + ) { - $reader = FileReader::fromString($filePath); - return new self($reader); } - public function records(ParseSpecification $spec) : Generator + public function records() : Iterator { foreach ($this->reader->lines() as $line) { - yield $spec->parse($line); + yield $this->spec->parse($line); } } diff --git a/src/SeparatedFileReader.hh b/src/SeparatedFileReader.hh index c8092d7..060644e 100644 --- a/src/SeparatedFileReader.hh +++ b/src/SeparatedFileReader.hh @@ -19,20 +19,17 @@ final class SeparatedFileReader private ParsedFileReader $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 { - $reader = FileReader::fromString($filePath); - return new self($reader); - } - - public function records(ColumnSpecification $spec) : Generator - { - return $this->reader->records($spec); + return $this->reader->records(); } public function close() : void