You are here

protected static function AbstractSerializer::splitStream in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/AbstractSerializer.php \Zend\Diactoros\AbstractSerializer::splitStream()

Split the stream into headers and body content.

Returns an array containing two elements

  • The first is an array of headers
  • The second is a StreamInterface containing the body content

Parameters

StreamInterface $stream:

Return value

array

Throws

UnexpectedValueException For invalid headers.

2 calls to AbstractSerializer::splitStream()
Serializer::fromStream in vendor/zendframework/zend-diactoros/src/Request/Serializer.php
Deserialize a request stream to a request instance.
Serializer::fromStream in vendor/zendframework/zend-diactoros/src/Response/Serializer.php
Parse a response from a stream.

File

vendor/zendframework/zend-diactoros/src/AbstractSerializer.php, line 89

Class

AbstractSerializer
Provides base functionality for request and response de/serialization strategies, including functionality for retrieving a line at a time from the message, splitting headers from the body, and serializing headers.

Namespace

Zend\Diactoros

Code

protected static function splitStream(StreamInterface $stream) {
  $headers = [];
  $currentHeader = false;
  while ($line = self::getLine($stream)) {
    if (preg_match(';^(?P<name>[!#$%&\'*+.^_`\\|~0-9a-zA-Z-]+):(?P<value>.*)$;', $line, $matches)) {
      $currentHeader = $matches['name'];
      if (!isset($headers[$currentHeader])) {
        $headers[$currentHeader] = [];
      }
      $headers[$currentHeader][] = ltrim($matches['value']);
      continue;
    }
    if (!$currentHeader) {
      throw new UnexpectedValueException('Invalid header detected');
    }
    if (!preg_match('#^[ \\t]#', $line)) {
      throw new UnexpectedValueException('Invalid header continuation');
    }

    // Append continuation to last header value found
    $value = array_pop($headers[$currentHeader]);
    $headers[$currentHeader][] = $value . ltrim($line);
  }

  // use RelativeStream to avoid copying initial stream into memory
  return [
    $headers,
    new RelativeStream($stream, $stream
      ->tell()),
  ];
}