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\Response;
12 use Psr\Http\Message\ResponseInterface;
14 trait SapiEmitterTrait
17 * Inject the Content-Length header if is not already present.
19 * @param ResponseInterface $response
20 * @return ResponseInterface
22 private function injectContentLength(ResponseInterface $response)
24 if (! $response->hasHeader('Content-Length')) {
25 // PSR-7 indicates int OR null for the stream size; for null values,
26 // we will not auto-inject the Content-Length.
27 if (null !== $response->getBody()->getSize()) {
28 return $response->withHeader('Content-Length', (string) $response->getBody()->getSize());
36 * Emit the status line.
38 * Emits the status line using the protocol version and status code from
39 * the response; if a reason phrase is available, it, too, is emitted.
41 * @param ResponseInterface $response
43 private function emitStatusLine(ResponseInterface $response)
45 $reasonPhrase = $response->getReasonPhrase();
48 $response->getProtocolVersion(),
49 $response->getStatusCode(),
50 ($reasonPhrase ? ' ' . $reasonPhrase : '')
55 * Emit response headers.
57 * Loops through each header, emitting each; if the header value
58 * is an array with multiple values, ensures that each is sent
59 * in such a way as to create aggregate headers (instead of replace
62 * @param ResponseInterface $response
64 private function emitHeaders(ResponseInterface $response)
66 foreach ($response->getHeaders() as $header => $values) {
67 $name = $this->filterHeader($header);
69 foreach ($values as $value) {
81 * Loops through the output buffer, flushing each, before emitting
84 * @param int|null $maxBufferLevel Flush up to this buffer level.
86 private function flush($maxBufferLevel = null)
88 if (null === $maxBufferLevel) {
89 $maxBufferLevel = ob_get_level();
92 while (ob_get_level() > $maxBufferLevel) {
98 * Filter a header name to wordcase
100 * @param string $header
103 private function filterHeader($header)
105 $filtered = str_replace('-', ' ', $header);
106 $filtered = ucwords($filtered);
107 return str_replace(' ', '-', $filtered);