You are here

final class Serializer in Zircon Profile 8

Same name in this branch
  1. 8 vendor/symfony/serializer/Serializer.php \Symfony\Component\Serializer\Serializer
  2. 8 vendor/zendframework/zend-diactoros/src/Request/Serializer.php \Zend\Diactoros\Request\Serializer
  3. 8 vendor/zendframework/zend-diactoros/src/Response/Serializer.php \Zend\Diactoros\Response\Serializer
  4. 8 vendor/phpdocumentor/reflection-docblock/src/phpDocumentor/Reflection/DocBlock/Serializer.php \phpDocumentor\Reflection\DocBlock\Serializer
  5. 8 core/modules/rest/src/Plugin/views/style/Serializer.php \Drupal\rest\Plugin\views\style\Serializer
Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-diactoros/src/Response/Serializer.php \Zend\Diactoros\Response\Serializer

Hierarchy

Expanded class hierarchy of Serializer

File

vendor/zendframework/zend-diactoros/src/Response/Serializer.php, line 20

Namespace

Zend\Diactoros\Response
View source
final class Serializer extends AbstractSerializer {

  /**
   * Deserialize a response string to a response instance.
   *
   * @param string $message
   * @return Response
   * @throws UnexpectedValueException when errors occur parsing the message.
   */
  public static function fromString($message) {
    $stream = new Stream('php://temp', 'wb+');
    $stream
      ->write($message);
    return static::fromStream($stream);
  }

  /**
   * Parse a response from a stream.
   *
   * @param StreamInterface $stream
   * @return ResponseInterface
   * @throws InvalidArgumentException when the stream is not readable.
   * @throws UnexpectedValueException when errors occur parsing the message.
   */
  public static function fromStream(StreamInterface $stream) {
    if (!$stream
      ->isReadable() || !$stream
      ->isSeekable()) {
      throw new InvalidArgumentException('Message stream must be both readable and seekable');
    }
    $stream
      ->rewind();
    list($version, $status, $reasonPhrase) = self::getStatusLine($stream);
    list($headers, $body) = self::splitStream($stream);
    return (new Response($body, $status, $headers))
      ->withProtocolVersion($version)
      ->withStatus($status, $reasonPhrase);
  }

  /**
   * Create a string representation of a response.
   *
   * @param ResponseInterface $response
   * @return string
   */
  public static function toString(ResponseInterface $response) {
    $reasonPhrase = $response
      ->getReasonPhrase();
    $headers = self::serializeHeaders($response
      ->getHeaders());
    $body = (string) $response
      ->getBody();
    $format = 'HTTP/%s %d%s%s%s';
    if (!empty($headers)) {
      $headers = "\r\n" . $headers;
    }
    if (!empty($body)) {
      $headers .= "\r\n\r\n";
    }
    return sprintf($format, $response
      ->getProtocolVersion(), $response
      ->getStatusCode(), $reasonPhrase ? ' ' . $reasonPhrase : '', $headers, $body);
  }

  /**
   * Retrieve the status line for the message.
   *
   * @param StreamInterface $stream
   * @return array Array with three elements: 0 => version, 1 => status, 2 => reason
   * @throws UnexpectedValueException if line is malformed
   */
  private static function getStatusLine(StreamInterface $stream) {
    $line = self::getLine($stream);
    if (!preg_match('#^HTTP/(?P<version>[1-9]\\d*\\.\\d) (?P<status>[1-5]\\d{2})(\\s+(?P<reason>.+))?$#', $line, $matches)) {
      throw new UnexpectedValueException('No status line detected');
    }
    return [
      $matches['version'],
      $matches['status'],
      isset($matches['reason']) ? $matches['reason'] : '',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractSerializer::CR constant
AbstractSerializer::EOL constant
AbstractSerializer::filterHeader protected static function Filter a header name to wordcase
AbstractSerializer::getLine protected static function Retrieve a single line from the stream.
AbstractSerializer::LF constant
AbstractSerializer::serializeHeaders protected static function Serialize headers to string values.
AbstractSerializer::splitStream protected static function Split the stream into headers and body content.
Serializer::fromStream public static function Parse a response from a stream.
Serializer::fromString public static function Deserialize a response string to a response instance.
Serializer::getStatusLine private static function Retrieve the status line for the message.
Serializer::toString public static function Create a string representation of a response.