createBody($content), $status, $this->injectFilename($filename, $this->injectContentType("application/octet-stream", $headers)) ); } /** * Inject the provided Content-Type, if none is already present. * * @return array Headers with injected Content-Type */ private function injectFilename(string $filename, array $headers) : array { $hasContentType = array_reduce(array_keys($headers), function ($carry, $item) { return $carry ?: (strtolower($item) === 'content-disposition'); }, false); if (! $hasContentType) { $headers['content-disposition'] = ['Content-Disposition:attachment; filename="' . $filename . '"']; } return $headers; } /** * Create the message body. * * @param string|StreamInterface $content * @throws Exception\InvalidArgumentException if $content is neither a string or stream. */ private function createBody(string $content) : StreamInterface { if ($content instanceof StreamInterface) { return $content; } $body = new Stream('php://temp', 'wb+'); $body->write($content); $body->rewind(); return $body; } }