1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace WyriHaximus\React\Stream\Base64; |
4
|
|
|
|
5
|
|
|
use Evenement\EventEmitter; |
6
|
|
|
use React\Stream\ReadableStreamInterface; |
7
|
|
|
use React\Stream\Util; |
8
|
|
|
use React\Stream\WritableStreamInterface; |
9
|
|
|
|
10
|
|
View Code Duplication |
final class ReadableStreamBase64Decode extends EventEmitter implements ReadableStreamInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var WritableStreamInterface |
14
|
|
|
*/ |
15
|
|
|
private $stream; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
private $buffer = ''; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ReadableStreamInterface $stream |
24
|
|
|
*/ |
25
|
1221 |
|
public function __construct(ReadableStreamInterface $stream) |
26
|
|
|
{ |
27
|
1221 |
|
$this->stream = $stream; |
|
|
|
|
28
|
|
|
$this->stream->on('data', function ($data): void { |
29
|
1221 |
|
$this->buffer .= $data; |
30
|
1221 |
|
$this->emit('data', [$this->processBuffer()]); |
31
|
1221 |
|
}); |
32
|
|
|
$this->stream->once('end', function (): void { |
33
|
1221 |
|
$this->emit('data', [\base64_decode($this->buffer, true)]); |
34
|
1221 |
|
$this->emit('end'); |
35
|
1221 |
|
}); |
36
|
1221 |
|
Util::forwardEvents($stream, $this, ['error', 'close']); |
37
|
1221 |
|
} |
38
|
|
|
|
39
|
1221 |
|
public function isReadable() |
40
|
|
|
{ |
41
|
1221 |
|
return $this->stream->isReadable(); |
42
|
|
|
} |
43
|
|
|
|
44
|
1221 |
|
public function pause() |
45
|
|
|
{ |
46
|
1221 |
|
return $this->stream->pause(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function resume() |
50
|
|
|
{ |
51
|
|
|
return $this->stream->resume(); |
52
|
|
|
} |
53
|
|
|
|
54
|
1221 |
|
public function pipe(WritableStreamInterface $dest, array $options = []) |
55
|
|
|
{ |
56
|
1221 |
|
return Util::pipe($this, $dest, $options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function close(): void |
60
|
|
|
{ |
61
|
|
|
$this->stream->close(); |
62
|
|
|
} |
63
|
|
|
|
64
|
1221 |
|
private function processBuffer(): string |
65
|
|
|
{ |
66
|
1221 |
|
$length = \strlen($this->buffer); |
67
|
1221 |
|
$buffer = \base64_decode(\substr($this->buffer, 0, $length - $length % 4), true); |
68
|
1221 |
|
$this->buffer = \substr($this->buffer, $length - $length % 4); |
69
|
|
|
|
70
|
1221 |
|
return $buffer; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..