酒店预订平台
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

46 рядки
1.1 KiB

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