class ReadLimitEntityBody

EntityBody decorator used to return only a subset of an entity body

Hierarchy

Expanded class hierarchy of ReadLimitEntityBody

File

drupal/core/vendor/guzzle/http/Guzzle/Http/ReadLimitEntityBody.php, line 8

Namespace

Guzzle\Http
View source
class ReadLimitEntityBody extends AbstractEntityBodyDecorator {

  /**
   * @var int Limit the number of bytes that can be read
   */
  protected $limit;

  /**
   * @var int Offset to start reading from
   */
  protected $offset;

  /**
   * @param EntityBodyInterface $body   Body to wrap
   * @param int                 $limit  Total number of bytes to allow to be read from the stream
   * @param int                 $offset Position to seek to before reading (only works on seekable streams)
   */
  public function __construct(EntityBodyInterface $body, $limit, $offset = 0) {
    parent::__construct($body);
    $this
      ->setLimit($limit)
      ->setOffset($offset);
    $this->body
      ->seek($offset);
  }

  /**
   * Returns only a subset of the decorated entity body when cast as a string
   * {@inheritdoc}
   */
  public function __toString() {
    return substr((string) $this->body, $this->offset, $this->limit);
  }

  /**
   * {@inheritdoc}
   */
  public function isConsumed() {
    return $this->offset + $this->limit - $this->body
      ->ftell() <= 0;
  }

  /**
   * Returns the Content-Length of the limited subset of data
   * {@inheritdoc}
   */
  public function getContentLength() {
    $length = $this->body
      ->getContentLength();
    return $length === false ? $this->limit : min($this->limit, min($length, $this->offset + $this->limit) - $this->offset);
  }

  /**
   * Allow for a bounded seek on the read limited entity body
   * {@inheritdoc}
   */
  public function seek($offset, $whence = SEEK_SET) {
    return $whence === SEEK_SET ? $this->body
      ->seek(max($this->offset, min($this->offset + $this->limit, $offset))) : false;
  }

  /**
   * Set the offset to start limiting from
   *
   * @param int $offset Offset to seek to and begin byte limiting from
   *
   * @return self
   */
  public function setOffset($offset) {
    $this->body
      ->seek($offset);
    $this->offset = $offset;
    return $this;
  }

  /**
   * Set the limit of bytes that the decorator allows to be read from the stream
   *
   * @param int $limit Total number of bytes to allow to be read from the stream
   *
   * @return self
   */
  public function setLimit($limit) {
    $this->limit = $limit;
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function read($length) {

    // Check if the current position is less than the total allowed bytes + original offset
    $remaining = $this->offset + $this->limit - $this->body
      ->ftell();
    if ($remaining > 0) {

      // Only return the amount of requested data, ensuring that the byte limit is not exceeded
      return $this->body
        ->read(min($remaining, $length));
    }
    else {
      return false;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractEntityBodyDecorator::$body protected property
AbstractEntityBodyDecorator::compress public function @codeCoverageIgnore Overrides EntityBodyInterface::compress
AbstractEntityBodyDecorator::ftell public function @codeCoverageIgnore Overrides StreamInterface::ftell
AbstractEntityBodyDecorator::getContentEncoding public function Get the Content-Encoding of the EntityBody Overrides EntityBodyInterface::getContentEncoding
AbstractEntityBodyDecorator::getContentMd5 public function Get an MD5 checksum of the stream's contents Overrides EntityBodyInterface::getContentMd5
AbstractEntityBodyDecorator::getContentType public function Guess the Content-Type or return the default application/octet-stream Overrides EntityBodyInterface::getContentType
AbstractEntityBodyDecorator::getMetaData public function @codeCoverageIgnore Overrides StreamInterface::getMetaData
AbstractEntityBodyDecorator::getSize public function Get the size of the stream if able Overrides StreamInterface::getSize
AbstractEntityBodyDecorator::getStream public function Get the stream resource Overrides StreamInterface::getStream
AbstractEntityBodyDecorator::getStreamType public function Get a label describing the underlying implementation of the stream Overrides StreamInterface::getStreamType
AbstractEntityBodyDecorator::getUri public function Get the URI/filename associated with this stream Overrides StreamInterface::getUri
AbstractEntityBodyDecorator::getWrapper public function Get the stream wrapper type Overrides StreamInterface::getWrapper
AbstractEntityBodyDecorator::getWrapperData public function Wrapper specific data attached to this stream. Overrides StreamInterface::getWrapperData
AbstractEntityBodyDecorator::isLocal public function Check if the stream is a local stream vs a remote stream Overrides StreamInterface::isLocal
AbstractEntityBodyDecorator::isReadable public function Check if the stream is readable Overrides StreamInterface::isReadable
AbstractEntityBodyDecorator::isSeekable public function Check if the string is repeatable Overrides StreamInterface::isSeekable
AbstractEntityBodyDecorator::isWritable public function Check if the stream is writable Overrides StreamInterface::isWritable
AbstractEntityBodyDecorator::rewind public function @codeCoverageIgnore Overrides StreamInterface::rewind
AbstractEntityBodyDecorator::setRewindFunction public function @codeCoverageIgnore Overrides EntityBodyInterface::setRewindFunction
AbstractEntityBodyDecorator::setSize public function @codeCoverageIgnore Overrides StreamInterface::setSize
AbstractEntityBodyDecorator::setStream public function @codeCoverageIgnore Overrides StreamInterface::setStream
AbstractEntityBodyDecorator::uncompress public function @codeCoverageIgnore Overrides EntityBodyInterface::uncompress
AbstractEntityBodyDecorator::write public function @codeCoverageIgnore Overrides StreamInterface::write
AbstractEntityBodyDecorator::__call public function Allow decorators to implement custom methods
ReadLimitEntityBody::$limit protected property
ReadLimitEntityBody::$offset protected property
ReadLimitEntityBody::getContentLength public function Returns the Content-Length of the limited subset of data Overrides AbstractEntityBodyDecorator::getContentLength
ReadLimitEntityBody::isConsumed public function Check if the stream has been consumed Overrides AbstractEntityBodyDecorator::isConsumed
ReadLimitEntityBody::read public function @codeCoverageIgnore Overrides AbstractEntityBodyDecorator::read
ReadLimitEntityBody::seek public function Allow for a bounded seek on the read limited entity body Overrides AbstractEntityBodyDecorator::seek
ReadLimitEntityBody::setLimit public function Set the limit of bytes that the decorator allows to be read from the stream
ReadLimitEntityBody::setOffset public function Set the offset to start limiting from
ReadLimitEntityBody::__construct public function Overrides AbstractEntityBodyDecorator::__construct
ReadLimitEntityBody::__toString public function Returns only a subset of the decorated entity body when cast as a string Overrides AbstractEntityBodyDecorator::__toString