Entity body used with an HTTP request or response
Expanded class hierarchy of EntityBody
class EntityBody extends Stream implements EntityBodyInterface {
/**
* @var bool Content-Encoding of the entity body if known
*/
protected $contentEncoding = false;
/**
* @var callable Method to invoke for rewinding a stream
*/
protected $rewindFunction;
/**
* Create a new EntityBody based on the input type
*
* @param resource|string|EntityBody $resource Entity body data
* @param int $size Size of the data contained in the resource
*
* @return EntityBody
* @throws InvalidArgumentException if the $resource arg is not a resource or string
*/
public static function factory($resource = '', $size = null) {
if ($resource instanceof EntityBodyInterface) {
return $resource;
}
switch (gettype($resource)) {
case 'string':
return self::fromString($resource);
case 'resource':
return new static($resource, $size);
case 'object':
if (method_exists($resource, '__toString')) {
return self::fromString((string) $resource);
}
break;
case 'array':
return self::fromString(http_build_query($resource));
}
throw new InvalidArgumentException('Invalid resource type');
}
/**
* {@inheritdoc}
*/
public function setRewindFunction($callable) {
if (!is_callable($callable)) {
throw new InvalidArgumentException('Must specify a callable');
}
$this->rewindFunction = $callable;
return $this;
}
/**
* {@inheritdoc}
*/
public function rewind() {
return $this->rewindFunction ? call_user_func($this->rewindFunction, $this) : parent::rewind();
}
/**
* Create a new EntityBody from a string
*
* @param string $string String of data
*
* @return EntityBody
*/
public static function fromString($string) {
$stream = fopen('php://temp', 'r+');
fwrite($stream, $string);
rewind($stream);
return new static($stream);
}
/**
* {@inheritdoc}
*/
public function compress($filter = 'zlib.deflate') {
$result = $this
->handleCompression($filter);
$this->contentEncoding = $result ? $filter : false;
return $result;
}
/**
* {@inheritdoc}
*/
public function uncompress($filter = 'zlib.inflate') {
$offsetStart = 0;
// When inflating gzipped data, the first 10 bytes must be stripped
// if a gzip header is present
if ($filter == 'zlib.inflate') {
// @codeCoverageIgnoreStart
if (!$this
->isReadable() || $this
->isConsumed() && !$this
->isSeekable()) {
return false;
}
// @codeCoverageIgnoreEnd
if (stream_get_contents($this->stream, 3, 0) === "") {
$offsetStart = 10;
}
}
$this->contentEncoding = false;
return $this
->handleCompression($filter, $offsetStart);
}
/**
* {@inheritdoc}
*/
public function getContentLength() {
return $this
->getSize();
}
/**
* {@inheritdoc}
*/
public function getContentType() {
if (!($this
->isLocal() && $this
->getWrapper() == 'plainfile' && file_exists($this
->getUri()))) {
return 'application/octet-stream';
}
return Mimetypes::getInstance()
->fromFilename($this
->getUri()) ?: 'application/octet-stream';
}
/**
* {@inheritdoc}
*/
public function getContentMd5($rawOutput = false, $base64Encode = false) {
$hash = self::getHash($this, 'md5', $rawOutput);
return $hash && $base64Encode ? base64_encode($hash) : $hash;
}
/**
* Calculate the MD5 hash of an entity body
*
* @param EntityBodyInterface $body Entity body to calculate the hash for
* @param bool $rawOutput Whether or not to use raw output
* @param bool $base64Encode Whether or not to base64 encode raw output (only if raw output is true)
*
* @return bool|string Returns an MD5 string on success or FALSE on failure
* @deprecated This will be deprecated soon
* @codeCoverageIgnore
*/
public static function calculateMd5(EntityBodyInterface $body, $rawOutput = false, $base64Encode = false) {
return $body
->getContentMd5($rawOutput, $base64Encode);
}
/**
* {@inheritdoc}
*/
public function setStreamFilterContentEncoding($streamFilterContentEncoding) {
$this->contentEncoding = $streamFilterContentEncoding;
return $this;
}
/**
* {@inheritdoc}
*/
public function getContentEncoding() {
return strtr($this->contentEncoding, array(
'zlib.deflate' => 'gzip',
'bzip2.compress' => 'compress',
)) ?: false;
}
/**
* {@inheritdoc}
*/
protected function handleCompression($filter, $offsetStart = 0) {
// @codeCoverageIgnoreStart
if (!$this
->isReadable() || $this
->isConsumed() && !$this
->isSeekable()) {
return false;
}
// @codeCoverageIgnoreEnd
$handle = fopen('php://temp', 'r+');
$filter = @stream_filter_append($handle, $filter, STREAM_FILTER_WRITE);
if (!$filter) {
return false;
}
// Seek to the offset start if possible
$this
->seek($offsetStart);
while ($data = fread($this->stream, 8096)) {
fwrite($handle, $data);
}
fclose($this->stream);
$this->stream = $handle;
stream_filter_remove($filter);
$stat = fstat($this->stream);
$this->size = $stat['size'];
$this
->rebuildCache();
$this
->seek(0);
// Remove any existing rewind function as the underlying stream has been replaced
$this->rewindFunction = null;
return true;
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
EntityBody:: |
public static | function | Create a new EntityBody based on the input type | |
EntityBody:: |
public | function |
Specify a custom callback used to rewind a non-seekable stream. This can be useful entity enclosing requests
that are redirected. Overrides EntityBodyInterface:: |
|
EntityBody:: |
public | function |
Rewind to the beginning of the stream Overrides Stream:: |
|
EntityBody:: |
public static | function | Create a new EntityBody from a string | |
EntityBody:: |
public | function |
If the stream is readable, compress the data in the stream using deflate compression. The uncompressed stream is
then closed, and the compressed stream then becomes the wrapped stream. Overrides EntityBodyInterface:: |
|
EntityBody:: |
public | function |
Decompress a deflated string. Once uncompressed, the uncompressed string is then used as the wrapped stream. Overrides EntityBodyInterface:: |
|
EntityBody:: |
public | function |
Get the Content-Length of the entity body if possible (alias of getSize) Overrides EntityBodyInterface:: |
|
EntityBody:: |
public | function |
Guess the Content-Type or return the default application/octet-stream Overrides EntityBodyInterface:: |
|
EntityBody:: |
public | function |
Get an MD5 checksum of the stream's contents Overrides EntityBodyInterface:: |
|
EntityBody:: |
public static | function | Calculate the MD5 hash of an entity body | |
EntityBody:: |
public | function | ||
EntityBody:: |
public | function |
Get the Content-Encoding of the EntityBody Overrides EntityBodyInterface:: |
|
EntityBody:: |
protected | function | ||
Stream:: |
public | function | Construct a new Stream | |
Stream:: |
public | function | Closes the stream when the helper is destructed | |
Stream:: |
public | function |
Convert the stream to a string if the stream is readable and the stream is seekable. Overrides StreamInterface:: |
|
Stream:: |
public static | function | Calculate a hash of a Stream | |
Stream:: |
public | function |
Get stream metadata Overrides StreamInterface:: |
|
Stream:: |
public | function |
Get the stream resource Overrides StreamInterface:: |
|
Stream:: |
public | function |
Set the stream that is wrapped by the object Overrides StreamInterface:: |
|
Stream:: |
public | function |
Get the stream wrapper type Overrides StreamInterface:: |
|
Stream:: |
public | function |
Wrapper specific data attached to this stream. Overrides StreamInterface:: |
|
Stream:: |
public | function |
Get a label describing the underlying implementation of the stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Get the URI/filename associated with this stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Get the size of the stream if able Overrides StreamInterface:: |
|
Stream:: |
public | function |
Check if the stream is readable Overrides StreamInterface:: |
|
Stream:: |
public | function |
Check if the stream is writable Overrides StreamInterface:: |
|
Stream:: |
public | function |
Check if the stream has been consumed Overrides StreamInterface:: |
|
Stream:: |
public | function |
Check if the stream is a local stream vs a remote stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Check if the string is repeatable Overrides StreamInterface:: |
|
Stream:: |
public | function |
Specify the size of the stream in bytes Overrides StreamInterface:: |
|
Stream:: |
public | function |
Seek to a position in the stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Read data from the stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Write data to the stream Overrides StreamInterface:: |
|
Stream:: |
public | function |
Returns the current position of the file read/write pointer Overrides StreamInterface:: |
|
Stream:: |
protected | function | Reprocess stream metadata | |
EntityBody:: |
protected | property | ||
EntityBody:: |
protected | property | ||
Stream:: |
protected | property | ||
Stream:: |
protected | property | ||
Stream:: |
protected | property | ||
Stream:: |
protected static | property | ||
Stream:: |
constant | |||
Stream:: |
constant | |||
Stream:: |
constant | |||
Stream:: |
constant | |||
Stream:: |
constant | |||
Stream:: |
constant |