Skip to content

Commit e9e4657

Browse files
committed
Add headers event to AssocDecoder class
1 parent 4679900 commit e9e4657

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ $stdin = new ReadableResourceStream(STDIN);
266266
$stream = new AssocDecoder($stdin);
267267

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

286294
This implies that the input stream MUST start with one row of header names and
287295
MUST use the same number of columns for all records. If the input stream does

src/AssocDecoder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function handleData($data)
8080
if ($this->expected === null) {
8181
$this->headers = $data;
8282
$this->expected = \count($data);
83+
$this->emit('headers', array($data));
8384
} else {
8485
if (\count($data) !== $this->expected) {
8586
$this->handleError(new \UnexpectedValueException(

tests/AssocDecoderTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,47 +24,52 @@ public function setUpDecoder()
2424

2525
public function testEmitDataWithoutNewlineWillNotForward()
2626
{
27+
$this->decoder->on('headers', $this->expectCallableNever());
2728
$this->decoder->on('data', $this->expectCallableNever());
2829

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

32-
public function testEmitDataOneLineWillBeSavedAsHeaderAndWillNotForward()
33+
public function testEmitDataOneLineWillBeSavedAsHeaderAndWillOnlyForwardHeader()
3334
{
35+
$this->decoder->on('headers', $this->expectCallableOnceWith(array('hello', 'world')));
3436
$this->decoder->on('data', $this->expectCallableNever());
3537

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

39-
public function testEmitDataTwoLinesWillForwardOneRecord()
41+
public function testEmitDataTwoLinesWillForwardHeaderAndOneRecord()
4042
{
43+
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
4144
$this->decoder->on('data', $this->expectCallableOnceWith(array('name' => 'alice', 'partner' => 'bob')));
4245

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

46-
public function testEmitDataTwoLinesWithoutTrailingNewlineWillNotForwardRecord()
49+
public function testEmitDataTwoLinesWithoutTrailingNewlineWillOnlyForwardHeaderAndNotRecord()
4750
{
51+
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
4852
$this->decoder->on('data', $this->expectCallableNever());
4953

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

53-
public function testEmitDataTwoLinesWithCustomSemicolonWillForwardOneRecord()
57+
public function testEmitDataTwoLinesWithCustomSemicolonWillForwardHeaderAndOneRecord()
5458
{
5559
$this->decoder = new AssocDecoder($this->input, ';');
60+
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
5661
$this->decoder->on('data', $this->expectCallableOnceWith(array('name' => 'alice', 'partner' => 'bob')));
5762

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

61-
public function testEmitDataTwoLinesButWrongColumnCoundWillEmitErrorAndClose()
66+
public function testEmitDataTwoLinesButWrongColumnCountWillForwardHeaderAndWillEmitAndErrorAndClose()
6267
{
68+
$this->decoder->on('headers', $this->expectCallableOnceWith(array('name', 'partner')));
6369
$this->decoder->on('data', $this->expectCallableNever());
6470
$this->decoder->on('error', $this->expectCallableOnceWith($this->isInstanceOf('UnexpectedValueException')));
6571
$this->decoder->on('close', $this->expectCallableOnce());
6672

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

0 commit comments

Comments
 (0)