@@ -4,6 +4,46 @@ Streaming UTF-8 parser for React PHP
44
55> Note: This project is in early alpha stage! Feel free to report any issues you encounter.
66
7+ ## Usage
8+
9+ ### Sequencer
10+
11+ The ` Sequencer ` class can be used to make sure you only get back complete, valid
12+ UTF-8 byte sequences when reading from a stream.
13+ It wraps a given ` ReadableStreamInterface ` and exposes its data through the same
14+ interface.
15+
16+ ``` php
17+ $stdin = new Stream(STDIN, $loop);
18+
19+ $stream = new Sequencer($stdin);
20+
21+ $stream->on('data', function ($chunk) {
22+ var_dump($chunk);
23+ });
24+ ```
25+
26+ React's streams emit chunks of data strings and make no assumption about its encoding.
27+ These chunks do not necessarily represent complete UTF-8 byte sequences, as a
28+ sequence may be broken up into multiple chunks.
29+ This class reassembles these sequences by buffering incomplete ones.
30+
31+ Also, if you're merely consuming a stream and you're not in control of producing and
32+ ensuring valid UTF-8 data, it may as well include invalid UTF-8 byte sequences.
33+ This class replaces any invalid bytes in the sequence with a ` ? ` .
34+ This replacement character can be given as a second paramter to the constructor:
35+
36+ ``` php
37+ $stream = new Sequencer($stdin, 'X');
38+ ```
39+
40+ As such, you can be sure you never get an invalid UTF-8 byte sequence out of
41+ the resulting stream.
42+
43+ Note that the stream may still contain ASCII/ANSI control characeters, as they're
44+ valid UTF-8.
45+ This binary data will be left as-is, unless you filter this at a later stage.
46+
747## Install
848
949The recommended way to install this library is [ through Composer] ( https://getcomposer.org ) .
0 commit comments