| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Utf8; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use React\Stream\ReadableStreamInterface; |
| 7 |
use React\Stream\Util; |
| 8 |
use React\Stream\WritableStreamInterface; |
| 9 |
|
| 10 |
/** |
| 11 |
* forwards only complete UTF-8 sequences |
| 12 |
*/ |
| 13 |
class Sequencer extends EventEmitter implements ReadableStreamInterface |
| 14 |
{ |
| 15 |
private $input; |
| 16 |
private $invalid; |
| 17 |
|
| 18 |
private $buffer = ''; |
| 19 |
private $closed = false; |
| 20 |
|
| 21 |
public function __construct(ReadableStreamInterface $input, $replacementCharacter = '?') |
| 22 |
{ |
| 23 |
$this->input = $input; |
| 24 |
$this->invalid = $replacementCharacter; |
| 25 |
|
| 26 |
if (!$input->isReadable()) { |
| 27 |
return $this->close(); |
| 28 |
} |
| 29 |
|
| 30 |
$this->input->on('data', array($this, 'handleData')); |
| 31 |
$this->input->on('end', array($this, 'handleEnd')); |
| 32 |
$this->input->on('error', array($this, 'handleError')); |
| 33 |
$this->input->on('close', array($this, 'close')); |
| 34 |
} |
| 35 |
|
| 36 |
/** @internal */ |
| 37 |
public function handleData($data) |
| 38 |
{ |
| 39 |
$this->buffer .= $data; |
| 40 |
$len = strlen($this->buffer); |
| 41 |
|
| 42 |
$sequence = ''; |
| 43 |
$expect = 0; |
| 44 |
$out = ''; |
| 45 |
|
| 46 |
for ($i = 0; $i < $len; ++$i) { |
| 47 |
$char = $this->buffer[$i]; |
| 48 |
$code = ord($char); |
| 49 |
|
| 50 |
if ($code & 128) { |
| 51 |
// multi-byte sequence |
| 52 |
if ($code & 64) { |
| 53 |
// this is the start of a sequence |
| 54 |
|
| 55 |
// unexpected start of sequence because already within sequence |
| 56 |
if ($expect !== 0) { |
| 57 |
$out .= str_repeat($this->invalid, strlen($sequence)); |
| 58 |
$sequence = ''; |
| 59 |
} |
| 60 |
|
| 61 |
$sequence = $char; |
| 62 |
$expect = 2; |
| 63 |
|
| 64 |
if ($code & 32) { |
| 65 |
++$expect; |
| 66 |
if ($code & 16) { |
| 67 |
++$expect; |
| 68 |
|
| 69 |
if ($code & 8) { |
| 70 |
// invalid sequence start length |
| 71 |
$out .= $this->invalid; |
| 72 |
$sequence = ''; |
| 73 |
$expect = 0; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
} else { |
| 78 |
// this is a follow-up byte in a sequence |
| 79 |
if ($expect === 0) { |
| 80 |
// we're not within a sequence in first place |
| 81 |
$out .= $this->invalid; |
| 82 |
} else { |
| 83 |
// valid following byte in sequence |
| 84 |
$sequence .= $char; |
| 85 |
|
| 86 |
// sequence reached expected length => add to output |
| 87 |
if (strlen($sequence) === $expect) { |
| 88 |
$out .= $sequence; |
| 89 |
$sequence = ''; |
| 90 |
$expect = 0; |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} else { |
| 95 |
// simple ASCII character found |
| 96 |
|
| 97 |
// unexpected because already within sequence |
| 98 |
if ($expect !== 0) { |
| 99 |
$out .= str_repeat($this->invalid, strlen($sequence)); |
| 100 |
$sequence = ''; |
| 101 |
$expect = 0; |
| 102 |
} |
| 103 |
|
| 104 |
$out .= $char; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
if ($out !== '') { |
| 109 |
$this->buffer = substr($this->buffer, strlen($out)); |
| 110 |
|
| 111 |
$this->emit('data', array($out)); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
/** @internal */ |
| 116 |
public function handleEnd() |
| 117 |
{ |
| 118 |
if ($this->buffer !== '' && $this->invalid !== '') { |
| 119 |
$data = str_repeat($this->invalid, strlen($this->buffer)); |
| 120 |
$this->buffer = ''; |
| 121 |
|
| 122 |
$this->emit('data', array($data)); |
| 123 |
} |
| 124 |
|
| 125 |
if (!$this->closed) { |
| 126 |
$this->emit('end'); |
| 127 |
$this->close(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** @internal */ |
| 132 |
public function handleError(\Exception $error) |
| 133 |
{ |
| 134 |
$this->emit('error', array($error)); |
| 135 |
$this->close(); |
| 136 |
} |
| 137 |
|
| 138 |
public function isReadable() |
| 139 |
{ |
| 140 |
return !$this->closed && $this->input->isReadable(); |
| 141 |
} |
| 142 |
|
| 143 |
public function close() |
| 144 |
{ |
| 145 |
if ($this->closed) { |
| 146 |
return; |
| 147 |
} |
| 148 |
|
| 149 |
$this->closed = true; |
| 150 |
$this->buffer = ''; |
| 151 |
|
| 152 |
$this->input->close(); |
| 153 |
|
| 154 |
$this->emit('close'); |
| 155 |
$this->removeAllListeners(); |
| 156 |
} |
| 157 |
|
| 158 |
public function pause() |
| 159 |
{ |
| 160 |
$this->input->pause(); |
| 161 |
} |
| 162 |
|
| 163 |
public function resume() |
| 164 |
{ |
| 165 |
$this->input->resume(); |
| 166 |
} |
| 167 |
|
| 168 |
public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 169 |
{ |
| 170 |
Util::pipe($this, $dest, $options); |
| 171 |
|
| 172 |
return $dest; |
| 173 |
} |
| 174 |
} |