You are here

function stream_for in Lockr 7.3

Create a new stream based on the input type.

Options is an associative array that can contain the following keys:

  • metadata: Array of custom metadata.
  • size: Size of the stream.

Parameters

resource|string|null|int|float|bool|StreamInterface|callable|\Iterator $resource Entity body data:

array $options Additional options:

Return value

StreamInterface

Throws

\InvalidArgumentException if the $resource arg is not valid.

6 calls to stream_for()
LazyOpenStream::createStream in vendor/guzzlehttp/psr7/src/LazyOpenStream.php
Creates the underlying stream lazily when required.
MessageTrait::getBody in vendor/guzzlehttp/psr7/src/MessageTrait.php
MultipartStream::addElement in vendor/guzzlehttp/psr7/src/MultipartStream.php
MultipartStream::createStream in vendor/guzzlehttp/psr7/src/MultipartStream.php
Create the aggregate stream that will be used to upload the POST data
Request::__construct in vendor/guzzlehttp/psr7/src/Request.php

... See full list

File

vendor/guzzlehttp/psr7/src/functions.php, line 78

Namespace

GuzzleHttp\Psr7

Code

function stream_for($resource = '', array $options = []) {
  if (is_scalar($resource)) {
    $stream = fopen('php://temp', 'r+');
    if ($resource !== '') {
      fwrite($stream, $resource);
      fseek($stream, 0);
    }
    return new Stream($stream, $options);
  }
  switch (gettype($resource)) {
    case 'resource':
      return new Stream($resource, $options);
    case 'object':
      if ($resource instanceof StreamInterface) {
        return $resource;
      }
      elseif ($resource instanceof \Iterator) {
        return new PumpStream(function () use ($resource) {
          if (!$resource
            ->valid()) {
            return false;
          }
          $result = $resource
            ->current();
          $resource
            ->next();
          return $result;
        }, $options);
      }
      elseif (method_exists($resource, '__toString')) {
        return stream_for((string) $resource, $options);
      }
      break;
    case 'NULL':
      return new Stream(fopen('php://temp', 'r+'), $options);
  }
  if (is_callable($resource)) {
    return new PumpStream($resource, $options);
  }
  throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
}