annotate vendor/guzzlehttp/psr7/src/DroppingStream.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2 namespace GuzzleHttp\Psr7;
Chris@0 3
Chris@0 4 use Psr\Http\Message\StreamInterface;
Chris@0 5
Chris@0 6 /**
Chris@0 7 * Stream decorator that begins dropping data once the size of the underlying
Chris@0 8 * stream becomes too full.
Chris@0 9 */
Chris@0 10 class DroppingStream implements StreamInterface
Chris@0 11 {
Chris@0 12 use StreamDecoratorTrait;
Chris@0 13
Chris@0 14 private $maxLength;
Chris@0 15
Chris@0 16 /**
Chris@0 17 * @param StreamInterface $stream Underlying stream to decorate.
Chris@0 18 * @param int $maxLength Maximum size before dropping data.
Chris@0 19 */
Chris@0 20 public function __construct(StreamInterface $stream, $maxLength)
Chris@0 21 {
Chris@0 22 $this->stream = $stream;
Chris@0 23 $this->maxLength = $maxLength;
Chris@0 24 }
Chris@0 25
Chris@0 26 public function write($string)
Chris@0 27 {
Chris@0 28 $diff = $this->maxLength - $this->stream->getSize();
Chris@0 29
Chris@0 30 // Begin returning 0 when the underlying stream is too large.
Chris@0 31 if ($diff <= 0) {
Chris@0 32 return 0;
Chris@0 33 }
Chris@0 34
Chris@0 35 // Write the stream or a subset of the stream if needed.
Chris@0 36 if (strlen($string) < $diff) {
Chris@0 37 return $this->stream->write($string);
Chris@0 38 }
Chris@0 39
Chris@0 40 return $this->stream->write(substr($string, 0, $diff));
Chris@0 41 }
Chris@0 42 }