You are here

function stream_for in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/psr7/src/functions.php \GuzzleHttp\Psr7\stream_for()

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|StreamInterface $resource Entity body data:

array $options Additional options:

Return value

Stream

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 77

Namespace

GuzzleHttp\Psr7

Code

function stream_for($resource = '', array $options = []) {
  switch (gettype($resource)) {
    case 'string':
      $stream = fopen('php://temp', 'r+');
      if ($resource !== '') {
        fwrite($stream, $resource);
        fseek($stream, 0);
      }
      return new Stream($stream, $options);
    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));
}