3 * Zend Framework (http://framework.zend.com/)
5 * @see http://github.com/zendframework/zend-diactoros for the canonical source repository
6 * @copyright Copyright (c) 2015-2016 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md New BSD License
10 namespace Zend\Diactoros;
12 use Psr\Http\Message\StreamInterface;
13 use UnexpectedValueException;
16 * Provides base functionality for request and response de/serialization
17 * strategies, including functionality for retrieving a line at a time from
18 * the message, splitting headers from the body, and serializing headers.
20 abstract class AbstractSerializer
27 * Retrieve a single line from the stream.
29 * Retrieves a line from the stream; a line is defined as a sequence of
30 * characters ending in a CRLF sequence.
32 * @param StreamInterface $stream
34 * @throws UnexpectedValueException if the sequence contains a CR or LF in
35 * isolation, or ends in a CR.
37 protected static function getLine(StreamInterface $stream)
41 while (! $stream->eof()) {
42 $char = $stream->read(1);
44 if ($crFound && $char === self::LF) {
49 // CR NOT followed by LF
50 if ($crFound && $char !== self::LF) {
51 throw new UnexpectedValueException('Unexpected carriage return detected');
55 if (! $crFound && $char === self::LF) {
56 throw new UnexpectedValueException('Unexpected line feed detected');
59 // CR found; do not append
60 if ($char === self::CR) {
65 // Any other character: append
69 // CR found at end of stream
71 throw new UnexpectedValueException("Unexpected end of headers");
78 * Split the stream into headers and body content.
80 * Returns an array containing two elements
82 * - The first is an array of headers
83 * - The second is a StreamInterface containing the body content
85 * @param StreamInterface $stream
87 * @throws UnexpectedValueException For invalid headers.
89 protected static function splitStream(StreamInterface $stream)
92 $currentHeader = false;
94 while ($line = self::getLine($stream)) {
95 if (preg_match(';^(?P<name>[!#$%&\'*+.^_`\|~0-9a-zA-Z-]+):(?P<value>.*)$;', $line, $matches)) {
96 $currentHeader = $matches['name'];
97 if (! isset($headers[$currentHeader])) {
98 $headers[$currentHeader] = [];
100 $headers[$currentHeader][] = ltrim($matches['value']);
104 if (! $currentHeader) {
105 throw new UnexpectedValueException('Invalid header detected');
108 if (! preg_match('#^[ \t]#', $line)) {
109 throw new UnexpectedValueException('Invalid header continuation');
112 // Append continuation to last header value found
113 $value = array_pop($headers[$currentHeader]);
114 $headers[$currentHeader][] = $value . ltrim($line);
117 // use RelativeStream to avoid copying initial stream into memory
118 return [$headers, new RelativeStream($stream, $stream->tell())];
122 * Serialize headers to string values.
124 * @param array $headers
127 protected static function serializeHeaders(array $headers)
130 foreach ($headers as $header => $values) {
131 $normalized = self::filterHeader($header);
132 foreach ($values as $value) {
133 $lines[] = sprintf('%s: %s', $normalized, $value);
137 return implode("\r\n", $lines);
141 * Filter a header name to wordcase
143 * @param string $header
146 protected static function filterHeader($header)
148 $filtered = str_replace('-', ' ', $header);
149 $filtered = ucwords($filtered);
150 return str_replace(' ', '-', $filtered);