/** * Method to set an response body * * @param mixed $data * @param string $writerType */ protected function setBody($data, $writerType = null) { if ($this->_responseWritten) { // we have already written a response return; } if (is_array($data)) { $this->setResponse(new Record('record', $data), $writerType); } elseif ($data instanceof \stdClass) { $this->setResponse(new Record('record', (array) $data), $writerType); } elseif ($data instanceof RecordInterface) { $this->setResponse($data, $writerType); } elseif ($data instanceof DOMDocument) { if (!$this->response->hasHeader('Content-Type')) { $this->response->setHeader('Content-Type', 'application/xml'); } $this->response->getBody()->write($data->saveXML()); } elseif ($data instanceof SimpleXMLElement) { if (!$this->response->hasHeader('Content-Type')) { $this->response->setHeader('Content-Type', 'application/xml'); } $this->response->getBody()->write($data->asXML()); } elseif (is_string($data)) { $this->response->getBody()->write($data); } elseif ($data instanceof StreamInterface) { $this->response->setBody($data); } else { throw new InvalidArgumentException('Invalid data type'); } $this->_responseWritten = true; }
public function send(ResponseInterface $response) { // remove body on specific status codes if (in_array($response->getStatusCode(), array(100, 101, 204, 304))) { $response->setBody(new StringStream('')); } elseif ($response->hasHeader('Location')) { $response->setBody(new StringStream('')); } if ($this->shouldSendHeader()) { // if we have an file stream body set custom header $this->prepareFileStream($response); // send status line $this->sendStatusLine($response); // send headers $this->sendHeaders($response); } // send body $this->sendBody($response); }