Skip to content

Add headers event to AssocDecoder class #29

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ $stdin = new ReadableResourceStream(STDIN);
$stream = new AssocDecoder($stdin);

$stream->on('data', function ($data) {
// data is a parsed element from the CSV stream
// $data is a parsed element from the CSV stream
// line 1: $data = array('name' => 'test', 'id' => '1');
// line 2: $data = array('name' => 'hello world', 'id' => '2');
var_dump($data);
Expand All @@ -281,7 +281,15 @@ explicitly use this class instead of the underlying [`Decoder`](#decoder).
In fact, it uses the [`Decoder`](#decoder) class internally. The only difference
is that this class requires the first line to include the name of headers and
will use this as keys for all following row data which will be emitted as
assoc arrays.
assoc arrays. After receiving the name of headers, this class will always emit
a `headers` event with a list of header names.

```php
$stream->on('headers', function (array $headers) {
// header line: $headers = array('name', 'id');
var_dump($headers);
});
```

This implies that the input stream MUST start with one row of header names and
MUST use the same number of columns for all records. If the input stream does
Expand Down
1 change: 1 addition & 0 deletions src/AssocDecoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function handleData($data)
if ($this->expected === null) {
$this->headers = $data;
$this->expected = \count($data);
$this->emit('headers', array($data));
} else {
if (\count($data) !== $this->expected) {
$this->handleError(new \UnexpectedValueException(
Expand Down
17 changes: 11 additions & 6 deletions tests/AssocDecoderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,52 @@ public function setUpDecoder()

public function testEmitDataWithoutNewlineWillNotForward()
{
$this->decoder->on('headers', $this->expectCallableNever());
$this->decoder->on('data', $this->expectCallableNever());

$this->input->emit('data', array("hello"));
}

public function testEmitDataOneLineWillBeSavedAsHeaderAndWillNotForward()
public function testEmitDataOneLineWillBeSavedAsHeaderAndWillOnlyForwardHeader()
{
$this->decoder->on('headers', $this->expectCallableOnceWith(array('hello', 'world')));
$this->decoder->on('data', $this->expectCallableNever());

$this->input->emit('data', array("hello,world\n"));
}

public function testEmitDataTwoLinesWillForwardOneRecord()
public function testEmitDataTwoLinesWillForwardHeaderAndOneRecord()
{
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
$this->decoder->on('data', $this->expectCallableOnceWith(array('name' => 'alice', 'partner' => 'bob')));

$this->input->emit('data', array("name,partner\nalice,bob\n"));
}

public function testEmitDataTwoLinesWithoutTrailingNewlineWillNotForwardRecord()
public function testEmitDataTwoLinesWithoutTrailingNewlineWillOnlyForwardHeaderAndNotRecord()
{
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
$this->decoder->on('data', $this->expectCallableNever());

$this->input->emit('data', array("name,partner\nalice,bob"));
}

public function testEmitDataTwoLinesWithCustomSemicolonWillForwardOneRecord()
public function testEmitDataTwoLinesWithCustomSemicolonWillForwardHeaderAndOneRecord()
{
$this->decoder = new AssocDecoder($this->input, ';');
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
$this->decoder->on('data', $this->expectCallableOnceWith(array('name' => 'alice', 'partner' => 'bob')));

$this->input->emit('data', array("name;partner\nalice;bob\n"));
}

public function testEmitDataTwoLinesButWrongColumnCoundWillEmitErrorAndClose()
public function testEmitDataTwoLinesButWrongColumnCountWillForwardHeaderAndWillEmitAndErrorAndClose()
{
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
$this->decoder->on('data', $this->expectCallableNever());
$this->decoder->on('error', $this->expectCallableOnceWith($this->isInstanceOf('UnexpectedValueException')));
$this->decoder->on('close', $this->expectCallableOnce());


$this->input->emit('data', array("name,partner\nalice\n"));
}

Expand Down