[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/ -> RelativeStream.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros;
  12  
  13  use Psr\Http\Message\StreamInterface;
  14  
  15  use const SEEK_SET;
  16  
  17  /**
  18   * Class RelativeStream
  19   *
  20   * Wrapper for default Stream class, representing subpart (starting from given offset) of initial stream.
  21   * It can be used to avoid copying full stream, conserving memory.
  22   * @example see Laminas\Diactoros\AbstractSerializer::splitStream()
  23   */
  24  final class RelativeStream implements StreamInterface
  25  {
  26      /**
  27       * @var StreamInterface
  28       */
  29      private $decoratedStream;
  30  
  31      /**
  32       * @var int
  33       */
  34      private $offset;
  35  
  36      /**
  37       * Class constructor
  38       *
  39       * @param StreamInterface $decoratedStream
  40       * @param int $offset
  41       */
  42      public function __construct(StreamInterface $decoratedStream, ?int $offset)
  43      {
  44          $this->decoratedStream = $decoratedStream;
  45          $this->offset = (int) $offset;
  46      }
  47  
  48      /**
  49       * {@inheritdoc}
  50       */
  51      public function __toString() : string
  52      {
  53          if ($this->isSeekable()) {
  54              $this->seek(0);
  55          }
  56          return $this->getContents();
  57      }
  58  
  59      /**
  60       * {@inheritdoc}
  61       */
  62      public function close() : void
  63      {
  64          $this->decoratedStream->close();
  65      }
  66  
  67      /**
  68       * {@inheritdoc}
  69       */
  70      public function detach()
  71      {
  72          return $this->decoratedStream->detach();
  73      }
  74  
  75      /**
  76       * {@inheritdoc}
  77       */
  78      public function getSize() : int
  79      {
  80          return $this->decoratedStream->getSize() - $this->offset;
  81      }
  82  
  83      /**
  84       * {@inheritdoc}
  85       */
  86      public function tell() : int
  87      {
  88          return $this->decoratedStream->tell() - $this->offset;
  89      }
  90  
  91      /**
  92       * {@inheritdoc}
  93       */
  94      public function eof() : bool
  95      {
  96          return $this->decoratedStream->eof();
  97      }
  98  
  99      /**
 100       * {@inheritdoc}
 101       */
 102      public function isSeekable() : bool
 103      {
 104          return $this->decoratedStream->isSeekable();
 105      }
 106  
 107      /**
 108       * {@inheritdoc}
 109       */
 110      public function seek($offset, $whence = SEEK_SET) : void
 111      {
 112          if ($whence == SEEK_SET) {
 113              $this->decoratedStream->seek($offset + $this->offset, $whence);
 114              return;
 115          }
 116          $this->decoratedStream->seek($offset, $whence);
 117      }
 118  
 119      /**
 120       * {@inheritdoc}
 121       */
 122      public function rewind() : void
 123      {
 124          $this->seek(0);
 125      }
 126  
 127      /**
 128       * {@inheritdoc}
 129       */
 130      public function isWritable() : bool
 131      {
 132          return $this->decoratedStream->isWritable();
 133      }
 134  
 135      /**
 136       * {@inheritdoc}
 137       */
 138      public function write($string) : int
 139      {
 140          if ($this->tell() < 0) {
 141              throw new Exception\InvalidStreamPointerPositionException();
 142          }
 143          return $this->decoratedStream->write($string);
 144      }
 145  
 146      /**
 147       * {@inheritdoc}
 148       */
 149      public function isReadable() : bool
 150      {
 151          return $this->decoratedStream->isReadable();
 152      }
 153  
 154      /**
 155       * {@inheritdoc}
 156       */
 157      public function read($length) : string
 158      {
 159          if ($this->tell() < 0) {
 160              throw new Exception\InvalidStreamPointerPositionException();
 161          }
 162          return $this->decoratedStream->read($length);
 163      }
 164  
 165      /**
 166       * {@inheritdoc}
 167       */
 168      public function getContents() : string
 169      {
 170          if ($this->tell() < 0) {
 171              throw new Exception\InvalidStreamPointerPositionException();
 172          }
 173          return $this->decoratedStream->getContents();
 174      }
 175  
 176      /**
 177       * {@inheritdoc}
 178       */
 179      public function getMetadata($key = null)
 180      {
 181          return $this->decoratedStream->getMetadata($key);
 182      }
 183  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer