You are here

class MultipartStream in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/guzzlehttp/psr7/src/MultipartStream.php \GuzzleHttp\Psr7\MultipartStream

Stream that when read returns bytes for a streaming multipart or multipart/form-data stream.

Hierarchy

Expanded class hierarchy of MultipartStream

1 file declares its use of MultipartStream
MultipartStreamTest.php in vendor/guzzlehttp/psr7/tests/MultipartStreamTest.php

File

vendor/guzzlehttp/psr7/src/MultipartStream.php, line 10

Namespace

GuzzleHttp\Psr7
View source
class MultipartStream implements StreamInterface {
  use StreamDecoratorTrait;
  private $boundary;

  /**
   * @param array  $elements Array of associative arrays, each containing a
   *                         required "name" key mapping to the form field,
   *                         name, a required "contents" key mapping to a
   *                         StreamInterface/resource/string, an optional
   *                         "headers" associative array of custom headers,
   *                         and an optional "filename" key mapping to a
   *                         string to send as the filename in the part.
   * @param string $boundary You can optionally provide a specific boundary
   *
   * @throws \InvalidArgumentException
   */
  public function __construct(array $elements = [], $boundary = null) {
    $this->boundary = $boundary ?: uniqid();
    $this->stream = $this
      ->createStream($elements);
  }

  /**
   * Get the boundary
   *
   * @return string
   */
  public function getBoundary() {
    return $this->boundary;
  }
  public function isWritable() {
    return false;
  }

  /**
   * Get the headers needed before transferring the content of a POST file
   */
  private function getHeaders(array $headers) {
    $str = '';
    foreach ($headers as $key => $value) {
      $str .= "{$key}: {$value}\r\n";
    }
    return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
  }

  /**
   * Create the aggregate stream that will be used to upload the POST data
   */
  protected function createStream(array $elements) {
    $stream = new AppendStream();
    foreach ($elements as $element) {
      $this
        ->addElement($stream, $element);
    }

    // Add the trailing boundary with CRLF
    $stream
      ->addStream(stream_for("--{$this->boundary}--\r\n"));
    return $stream;
  }
  private function addElement(AppendStream $stream, array $element) {
    foreach ([
      'contents',
      'name',
    ] as $key) {
      if (!array_key_exists($key, $element)) {
        throw new \InvalidArgumentException("A '{$key}' key is required");
      }
    }
    $element['contents'] = stream_for($element['contents']);
    if (empty($element['filename'])) {
      $uri = $element['contents']
        ->getMetadata('uri');
      if (substr($uri, 0, 6) !== 'php://') {
        $element['filename'] = $uri;
      }
    }
    list($body, $headers) = $this
      ->createElement($element['name'], $element['contents'], isset($element['filename']) ? $element['filename'] : null, isset($element['headers']) ? $element['headers'] : []);
    $stream
      ->addStream(stream_for($this
      ->getHeaders($headers)));
    $stream
      ->addStream($body);
    $stream
      ->addStream(stream_for("\r\n"));
  }

  /**
   * @return array
   */
  private function createElement($name, $stream, $filename, array $headers) {

    // Set a default content-disposition header if one was no provided
    $disposition = $this
      ->getHeader($headers, 'content-disposition');
    if (!$disposition) {
      $headers['Content-Disposition'] = $filename ? sprintf('form-data; name="%s"; filename="%s"', $name, basename($filename)) : "form-data; name=\"{$name}\"";
    }

    // Set a default content-length header if one was no provided
    $length = $this
      ->getHeader($headers, 'content-length');
    if (!$length) {
      if ($length = $stream
        ->getSize()) {
        $headers['Content-Length'] = (string) $length;
      }
    }

    // Set a default Content-Type if one was not supplied
    $type = $this
      ->getHeader($headers, 'content-type');
    if (!$type && $filename) {
      if ($type = mimetype_from_filename($filename)) {
        $headers['Content-Type'] = $type;
      }
    }
    return [
      $stream,
      $headers,
    ];
  }
  private function getHeader(array $headers, $key) {
    $lowercaseHeader = strtolower($key);
    foreach ($headers as $k => $v) {
      if (strtolower($k) === $lowercaseHeader) {
        return $v;
      }
    }
    return null;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MultipartStream::$boundary private property
MultipartStream::addElement private function
MultipartStream::createElement private function
MultipartStream::createStream protected function Create the aggregate stream that will be used to upload the POST data Overrides StreamDecoratorTrait::createStream
MultipartStream::getBoundary public function Get the boundary
MultipartStream::getHeader private function
MultipartStream::getHeaders private function Get the headers needed before transferring the content of a POST file
MultipartStream::isWritable public function Returns whether or not the stream is writable. Overrides StreamDecoratorTrait::isWritable
MultipartStream::__construct public function Overrides StreamDecoratorTrait::__construct
StreamDecoratorTrait::close public function 1
StreamDecoratorTrait::detach public function
StreamDecoratorTrait::eof public function 2
StreamDecoratorTrait::getContents public function
StreamDecoratorTrait::getMetadata public function
StreamDecoratorTrait::getSize public function 2
StreamDecoratorTrait::isReadable public function
StreamDecoratorTrait::isSeekable public function 1
StreamDecoratorTrait::read public function 2
StreamDecoratorTrait::rewind public function 1
StreamDecoratorTrait::seek public function 3
StreamDecoratorTrait::tell public function 1
StreamDecoratorTrait::write public function 2
StreamDecoratorTrait::__call public function Allow decorators to implement custom methods
StreamDecoratorTrait::__get public function Magic method used to create a new stream if streams are not added in the constructor of a decorator (e.g., LazyOpenStream).
StreamDecoratorTrait::__toString public function