| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace YoastSEO_Vendor\GuzzleHttp\Psr7; |
| 5 |
|
| 6 |
use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; |
| 7 |
/** |
| 8 |
* Stream decorator that begins dropping data once the size of the underlying |
| 9 |
* stream becomes too full. |
| 10 |
*/ |
| 11 |
final class DroppingStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface |
| 12 |
{ |
| 13 |
use StreamDecoratorTrait; |
| 14 |
/** @var int */ |
| 15 |
private $maxLength; |
| 16 |
/** @var StreamInterface */ |
| 17 |
private $stream; |
| 18 |
/** |
| 19 |
* @param StreamInterface $stream Underlying stream to decorate. |
| 20 |
* @param int $maxLength Maximum size before dropping data. |
| 21 |
*/ |
| 22 |
public function __construct(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream, int $maxLength) |
| 23 |
{ |
| 24 |
$this->stream = $stream; |
| 25 |
$this->maxLength = $maxLength; |
| 26 |
} |
| 27 |
public function write($string) : int |
| 28 |
{ |
| 29 |
if (!\is_string($string)) { |
| 30 |
\YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.', \get_debug_type($string)); |
| 31 |
} |
| 32 |
$diff = $this->maxLength - $this->stream->getSize(); |
| 33 |
// Begin returning 0 when the underlying stream is too large. |
| 34 |
if ($diff <= 0) { |
| 35 |
return 0; |
| 36 |
} |
| 37 |
// Write the stream or a subset of the stream if needed. |
| 38 |
if (\strlen($string) < $diff) { |
| 39 |
return $this->stream->write($string); |
| 40 |
} |
| 41 |
return $this->stream->write(\substr($string, 0, $diff)); |
| 42 |
} |
| 43 |
} |
| 44 |
|