|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Csv; |
| 4 | + |
| 5 | +use Evenement\EventEmitter; |
| 6 | +use React\Stream\ReadableStreamInterface; |
| 7 | +use React\Stream\WritableStreamInterface; |
| 8 | +use React\Stream\Util; |
| 9 | + |
| 10 | +/** |
| 11 | + * The AssocDecoder / Parser reads from a plain stream and emits assoc data arrays for each CSV record |
| 12 | + */ |
| 13 | +class AssocDecoder extends EventEmitter implements ReadableStreamInterface |
| 14 | +{ |
| 15 | + private $input; |
| 16 | + private $expected; |
| 17 | + private $headers = array(); |
| 18 | + private $closed = false; |
| 19 | + |
| 20 | + /** |
| 21 | + * @param ReadableStreamInterface $input |
| 22 | + * @param string $delimiter |
| 23 | + * @param string $enclosure |
| 24 | + * @param string $escapeChar |
| 25 | + * @param int $maxlength |
| 26 | + */ |
| 27 | + public function __construct(ReadableStreamInterface $input, $delimiter = ',', $enclosure = '"', $escapeChar = '\\', $maxlength = 65536) |
| 28 | + { |
| 29 | + $this->input = new Decoder($input, $delimiter, $enclosure, $escapeChar, $maxlength); |
| 30 | + |
| 31 | + if (!$input->isReadable()) { |
| 32 | + $this->close(); |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + $this->input->on('data', array($this, 'handleData')); |
| 37 | + $this->input->on('end', array($this, 'handleEnd')); |
| 38 | + $this->input->on('error', array($this, 'handleError')); |
| 39 | + $this->input->on('close', array($this, 'close')); |
| 40 | + } |
| 41 | + |
| 42 | + public function isReadable() |
| 43 | + { |
| 44 | + return !$this->closed; |
| 45 | + } |
| 46 | + |
| 47 | + public function close() |
| 48 | + { |
| 49 | + if ($this->closed) { |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + $this->closed = true; |
| 54 | + $this->input->close(); |
| 55 | + |
| 56 | + $this->emit('close'); |
| 57 | + $this->removeAllListeners(); |
| 58 | + } |
| 59 | + |
| 60 | + public function pause() |
| 61 | + { |
| 62 | + $this->input->pause(); |
| 63 | + } |
| 64 | + |
| 65 | + public function resume() |
| 66 | + { |
| 67 | + $this->input->resume(); |
| 68 | + } |
| 69 | + |
| 70 | + public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 71 | + { |
| 72 | + Util::pipe($this, $dest, $options); |
| 73 | + |
| 74 | + return $dest; |
| 75 | + } |
| 76 | + |
| 77 | + /** @internal */ |
| 78 | + public function handleData($data) |
| 79 | + { |
| 80 | + if ($this->expected === null) { |
| 81 | + $this->headers = $data; |
| 82 | + $this->expected = \count($data); |
| 83 | + } else { |
| 84 | + if (\count($data) !== $this->expected) { |
| 85 | + $this->handleError(new \UnexpectedValueException( |
| 86 | + 'Expected record with ' . $this->expected . ' columns, but got ' . \count($data) . ' instead') |
| 87 | + ); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + $this->emit('data', array( |
| 92 | + \array_combine($this->headers, $data) |
| 93 | + )); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** @internal */ |
| 98 | + public function handleEnd() |
| 99 | + { |
| 100 | + if ($this->headers === array()) { |
| 101 | + $this->handleError(new \UnderflowException('Stream ended without headers')); |
| 102 | + } |
| 103 | + |
| 104 | + if (!$this->closed) { |
| 105 | + $this->emit('end'); |
| 106 | + $this->close(); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + /** @internal */ |
| 111 | + public function handleError(\Exception $error) |
| 112 | + { |
| 113 | + $this->emit('error', array($error)); |
| 114 | + $this->close(); |
| 115 | + } |
| 116 | +} |
0 commit comments