Files
wp-multisite-waas/dependencies/amphp/byte-stream/lib/Base64/Base64EncodingInputStream.php
2024-11-30 18:24:12 -07:00

38 lines
1.1 KiB
PHP

<?php
namespace WP_Ultimo\Dependencies\Amp\ByteStream\Base64;
use WP_Ultimo\Dependencies\Amp\ByteStream\InputStream;
use WP_Ultimo\Dependencies\Amp\Promise;
use function WP_Ultimo\Dependencies\Amp\call;
final class Base64EncodingInputStream implements InputStream
{
/** @var InputStream */
private $source;
/** @var string|null */
private $buffer = '';
public function __construct(InputStream $source)
{
$this->source = $source;
}
public function read() : Promise
{
return call(function () {
$chunk = (yield $this->source->read());
if ($chunk === null) {
if ($this->buffer === null) {
return null;
}
$chunk = \base64_encode($this->buffer);
$this->buffer = null;
return $chunk;
}
$this->buffer .= $chunk;
$length = \strlen($this->buffer);
$chunk = \base64_encode(\substr($this->buffer, 0, $length - $length % 3));
$this->buffer = \substr($this->buffer, $length - $length % 3);
return $chunk;
});
}
}