| @@ 8-65 (lines=58) @@ | ||
| 5 | use Evenement\EventEmitterTrait; |
|
| 6 | use React\Stream\WritableStreamInterface; |
|
| 7 | ||
| 8 | final class WritableStreamBase64Decode implements WritableStreamInterface |
|
| 9 | { |
|
| 10 | use EventEmitterTrait; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @var WritableStreamInterface |
|
| 14 | */ |
|
| 15 | private $stream; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | private $buffer = ''; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * WritableStreamHash constructor. |
|
| 24 | * @param WritableStreamInterface $stream |
|
| 25 | */ |
|
| 26 | public function __construct(WritableStreamInterface $stream) |
|
| 27 | { |
|
| 28 | $this->stream = $stream; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function isWritable() |
|
| 32 | { |
|
| 33 | return $this->stream->isWritable(); |
|
| 34 | } |
|
| 35 | ||
| 36 | public function write($data) |
|
| 37 | { |
|
| 38 | $this->buffer .= $data; |
|
| 39 | ||
| 40 | return $this->stream->write($this->processBuffer()); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function end($data = null) |
|
| 44 | { |
|
| 45 | $this->buffer .= $data; |
|
| 46 | $this->stream->end( |
|
| 47 | $this->processBuffer() . base64_decode($this->buffer) |
|
| 48 | ); |
|
| 49 | $this->buffer = ''; |
|
| 50 | } |
|
| 51 | ||
| 52 | public function close() |
|
| 53 | { |
|
| 54 | $this->stream->close(); |
|
| 55 | } |
|
| 56 | ||
| 57 | private function processBuffer(): string |
|
| 58 | { |
|
| 59 | $length = strlen($this->buffer); |
|
| 60 | $buffer = base64_decode(substr($this->buffer, 0, $length - $length % 4)); |
|
| 61 | $this->buffer = substr($this->buffer, $length - $length % 4); |
|
| 62 | ||
| 63 | return $buffer; |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||
| @@ 8-65 (lines=58) @@ | ||
| 5 | use Evenement\EventEmitterTrait; |
|
| 6 | use React\Stream\WritableStreamInterface; |
|
| 7 | ||
| 8 | final class WritableStreamBase64Encode implements WritableStreamInterface |
|
| 9 | { |
|
| 10 | use EventEmitterTrait; |
|
| 11 | ||
| 12 | /** |
|
| 13 | * @var WritableStreamInterface |
|
| 14 | */ |
|
| 15 | private $stream; |
|
| 16 | ||
| 17 | /** |
|
| 18 | * @var string |
|
| 19 | */ |
|
| 20 | private $buffer = ''; |
|
| 21 | ||
| 22 | /** |
|
| 23 | * WritableStreamHash constructor. |
|
| 24 | * @param WritableStreamInterface $stream |
|
| 25 | */ |
|
| 26 | public function __construct(WritableStreamInterface $stream) |
|
| 27 | { |
|
| 28 | $this->stream = $stream; |
|
| 29 | } |
|
| 30 | ||
| 31 | public function isWritable() |
|
| 32 | { |
|
| 33 | return $this->stream->isWritable(); |
|
| 34 | } |
|
| 35 | ||
| 36 | public function write($data) |
|
| 37 | { |
|
| 38 | $this->buffer .= $data; |
|
| 39 | ||
| 40 | return $this->stream->write($this->processBuffer()); |
|
| 41 | } |
|
| 42 | ||
| 43 | public function end($data = null) |
|
| 44 | { |
|
| 45 | $this->buffer .= $data; |
|
| 46 | $this->stream->end( |
|
| 47 | $this->processBuffer() . base64_encode($this->buffer) |
|
| 48 | ); |
|
| 49 | $this->buffer = ''; |
|
| 50 | } |
|
| 51 | ||
| 52 | public function close() |
|
| 53 | { |
|
| 54 | $this->stream->close(); |
|
| 55 | } |
|
| 56 | ||
| 57 | private function processBuffer(): string |
|
| 58 | { |
|
| 59 | $length = strlen($this->buffer); |
|
| 60 | $buffer = base64_encode(substr($this->buffer, 0, $length - $length % 3)); |
|
| 61 | $this->buffer = substr($this->buffer, $length - $length % 3); |
|
| 62 | ||
| 63 | return $buffer; |
|
| 64 | } |
|
| 65 | } |
|
| 66 | ||