You are here

protected static function AbstractSerializer::getLine in Zircon Profile 8

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

Retrieve a single line from the stream.

Retrieves a line from the stream; a line is defined as a sequence of characters ending in a CRLF sequence.

Parameters

StreamInterface $stream:

Return value

string

Throws

UnexpectedValueException if the sequence contains a CR or LF in isolation, or ends in a CR.

3 calls to AbstractSerializer::getLine()
AbstractSerializer::splitStream in vendor/zendframework/zend-diactoros/src/AbstractSerializer.php
Split the stream into headers and body content.
Serializer::getRequestLine in vendor/zendframework/zend-diactoros/src/Request/Serializer.php
Retrieve the components of the request line.
Serializer::getStatusLine in vendor/zendframework/zend-diactoros/src/Response/Serializer.php
Retrieve the status line for the message.

File

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

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 getLine(StreamInterface $stream) {
  $line = '';
  $crFound = false;
  while (!$stream
    ->eof()) {
    $char = $stream
      ->read(1);
    if ($crFound && $char === self::LF) {
      $crFound = false;
      break;
    }

    // CR NOT followed by LF
    if ($crFound && $char !== self::LF) {
      throw new UnexpectedValueException('Unexpected carriage return detected');
    }

    // LF in isolation
    if (!$crFound && $char === self::LF) {
      throw new UnexpectedValueException('Unexpected line feed detected');
    }

    // CR found; do not append
    if ($char === self::CR) {
      $crFound = true;
      continue;
    }

    // Any other character: append
    $line .= $char;
  }

  // CR found at end of stream
  if ($crFound) {
    throw new UnexpectedValueException("Unexpected end of headers");
  }
  return $line;
}