Prepare response body for sending

@return string
325816
protected function prepareBody()
    {
        $body = $this->body;

        $result = $body;

        if ($this->isJson() && (is_array($body) || is_object($body))) {
            $result = json_encode(\pwf\helpers\ArrayHelper::toArray($body));
        }

        return $result;
    }
3433
public function bodyToArray()
    {
        // Getting response content
        $xmlContent = $this->response->getContent();

        // Wrapping it into a Xml reader object and convert it to array
        $xmlReader = new Xml();
        return $xmlReader->fromString($xmlContent);
    }
774367
protected function getResponseBody() {
        $body = json_decode((string) $this->response->getBody());

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new InvalidArgumentException('The response body does not contain valid JSON data.');
        } else if (!is_array($body) && !($body instanceof stdClass)) {
            throw new InvalidArgumentException('The response body does not contain a valid JSON array / object.');
        }

        return $body;
    }
1335263
private function sendHead(): Response
    {
        $this->header($this->status->line($this->version, $this->sapi));
        foreach ($this->headers as $name => $value) {
            $this->header($name . ': ' . $value);
        }

        foreach ($this->cookies as $cookie) {
            $cookie->send();
        }

        $this->header('Content-type: ' . $this->mimeType);
        if (!$this->headers->contain('X-Request-ID')) {
            $this->header('X-Request-ID: ' . $this->request->id());
        }

        return $this;
    }
5815
public function send()
    {
        $responses = new \StdClass();

        foreach ($this->resources as $key => $promise) {
            $response = $promise->wait();
            $responses->{$key} = $this->parse($response->getBody());
        }

        return $responses;
    }
750692
public function toResponse()
    {
        $response = Http::getInstance()->response();
        $response->reset();
        $response->status($this->code);
        if (is_string($this->details)) {
            $response->body($this->details);
        } elseif (is_string($this->message)) {
            $response->body($this->message);
        }

        return $response;
    }
619177
public function getBody()
    {
        // first we should render the body if it was not rendered before
        if (is_null($this->body)) {
            $this->set('body', '');
            // this is unnormal situation, notifying developer
            $this->app->logger->logLine('Email body is null: '.$this->get('from').' >> '.
                    date($this->app->getConfig('locale/timestamp', 'Y-m-d H:i:s')."\n"), null, 'error');
        }
        //if(!isset($this->mime['text'])&&!isset($this->mime['html'])){
        $this->setBody(is_object($this->body) ? $this->body->render() : $this->body);
        //}
        // sign should be added to all parts
        if (isset($this->mime['text'])) {
            $this->mime['text']['content'] .= $this->getSign();
        }
        if (isset($this->mime['html'])) {
            $this->mime['html']['content'] .= $this->getSign();
            // HTML should be converted to base 64
            //$this->mime['html']['content']=base64_encode($this->mime['html']['content']);
            // no, it should be splitted
            //$this->mime['html']['content']=wordwrap($this->mime['html']['content'],76,"\r\n",false);
        }
        // now as we have all the needed parts set
        $result = '';
        // first we have to add a simple text for non-Mime readers
        if (count($this->mime) == 0) {
            $result .= $this->plain_text;
        }
        // adding mail parts
        foreach ($this->mime as $name => $att) {
            list($type) = explode(';', $att['type']);
            // $name is a file name/part name, $att is a hash with type and content
            // depending on the type adding a header
            switch ($type) {
                case 'text/plain':
                    if (count($this->mime) > 0) {
                        $result .= "\n\n--".$this->getBoundary()."\n".
                        'Content-Type: '.$att['type'].'; ';
                    }
                    //$result.="charset=UTF-8";
                    break;

                case 'text/html':
                    if (count($this->mime) > 0) {
                        $result .= "\n\n--".$this->getBoundary()."\n".
                        'Content-Type: '.$att['type'].'; ';
                    }
                    $result .=
                        'Content-Transfer-Encoding: 8bit';
                    $att['content'] = rtrim(wordwrap($att['content']));
                    break;
            }
            if (isset($att['attachment'])) {
                $att['content'] = rtrim(chunk_split($att['content']));
                $result .= "name=$name\n".
                    "Content-transfer-encoding: base64\nContent-Disposition: attachment;";
            }
            $result .= "\n\n";
            $result .= $att['content'];
        }
        // if there were any attachments, trailing boundary should be added
        if (count($this->mime) > 0) {
            $result .= "\n--".$this->getBoundary()."\n";
        }

        return ltrim($result);
    }
424768
public function send()
    {
        // Send headers
        if (headers_sent() === false) {
            if (strpos(PHP_SAPI, 'cgi') === 0) {
                header(sprintf('Status: %s', $this->getReasonPhrase()));
            } else {
                header(sprintf('%s %s', $this->getProtocolVersion(), $this->getReasonPhrase()));
            }

            foreach ($this->headers as $name => $value) {
                foreach ($value as $hVal) {
                    header("$name: $hVal", false);
                }
            }
        }

        // Send body
        $this->body->seek(0);
        while ($this->body->feof() === false) {
            echo $this->body->read(1024);
        }

        return $this;
    }
156591
public function generateBody()
    {
        $contents = stream_get_contents( $this->stream );
        return chunk_split( base64_encode( $contents ), 76, ezcMailTools::lineBreak() );
    }
862603
private function prepareResponse() {
    if (is_null($this->response)) {
      return false;
    }

    $response = "";

    $this->response_header_block = $this->extractHeadersFromResponse();
    if ($this->tryGetOption(CURLOPT_HEADER, $value) && $value) {
      $response .= $this->response_header_block;
    }
    $response .= $this->response->getContent();

    return $response;
  }
607101
public function body()
	{
		$msg = $this->getMessage();
		
		if (422 == $this->getCode() && !empty($msg)) {
			$this->extra['errors'] = json_decode($msg, true);
			$msg = null;
		}
		
		if (empty($msg)) {
			switch ($this->getCode()) {
				case 404:
					$msg = 'Not Found';
					break;
				case 405:
					$msg = 'Method Not Allowed';
					break;
				case 400:
					$msg = 'Bad Request';
					break;
				case 401:
					$msg = 'Unauthorized';
					break;
				case 402:
					$msg = 'Payment Required';
					break;
				case 403:
					$msg = 'Forbidden';
					break;
				case 422:
					$msg = 'Unprocessable Entity';
					break;
				case 410:
					$msg = 'Resource Deleted';
					break;
				case 500:
					$msg = 'Server Error';
					break;
				default:
					$msg = 'Error';
			}
		}
		
		$output = array(
			'status'  => $this->getCode(),
			'message' => $msg,
		);
		
		if (!empty($this->getExtra())) {
			$output = array_merge($output, $this->getExtra());
		}
		
		return $output;
	}
209203
protected function prepareBody(InternalRequestInterface $internalRequest)
    {
        $body = (string) $internalRequest->getBody();

        if (!empty($body)) {
            return $body;
        }

        $files = $internalRequest->getFiles();

        if (empty($files)) {
            return http_build_query($internalRequest->getDatas(), null, '&');
        }

        $body = '';

        foreach ($internalRequest->getDatas() as $name => $value) {
            $body .= $this->prepareRawBody($name, $value);
        }

        foreach ($internalRequest->getFiles() as $name => $file) {
            $body .= $this->prepareRawBody($name, $file, true);
        }

        $body .= '--'.$this->configuration->getBoundary().'--'."\r\n";

        return $body;
    }
62648
public function send()
  {
    # Set HTTP Response Code
    http_response_code($this->statusCode);

    # Update Cache-Control Header
    if ( empty($this->getHeader('Cache-Control')) ) {
      $this->setHeader('Cache-Control', $this->cacheControl);
    }

    # Set All HTTP headers
    foreach ($this->headers as $header => $value) {
      header($header.': '.$value);
    }

    # Set all cookies
    foreach ($this->cookies as $idx => $cookie) {
      setCookie( $cookie['name'], $cookie['value'], $cookie['expire'], $cookie['path'], $cookie['domain'], $cookie['secure'], $cookie['httponly'] );
    }

    # Send Response Body -- if we have something in the $body param
    if ( !empty($this->getBody()) ) {
      echo $this->getBody();
    } else
    if ( !empty($this->getFileBody()) ) {
      readfile($this->getFileBody());
    }

    # Results
    return $this;
  }
1099123
public function sendContent()
    {
        if (! empty($this->_padding)) {
            $response = $this->getResponse();
            $response->setContent(sprintf('%s(%s);', $this->_padding, $response->getContent()));
            print $response->getContent();
            die;
        }

        return parent::sendContent();
    }
265753
protected function processResponse()
    {
        $body = $this->response->getBody();

        // Look for UTF-8 BOM and remove.
        if (0 === strpos(bin2hex(substr($body, 0, 4)), 'efbbbf')) {
            $body = substr($body, 3);
        }

        $parsedResponse = \json_decode($body);

        // Test if return a valid JSON.
        if (JSON_ERROR_NONE !== json_last_error()) {
            $message = function_exists('json_last_error_msg') ? json_last_error_msg() : 'Invalid JSON returned';
            throw new HttpClientException(
                sprintf('JSON ERROR: %s', $message),
                $this->response->getCode(),
                $this->request,
                $this->response
            );
        }

        $this->lookForErrors($parsedResponse);

        return $parsedResponse;
    }
966521
public function sendBody(Response $response)
    {
        $body = $response->getBody();
        $body->rewind();
        echo $body->getContents();
        return $response;
    }
61911
public function toSymfonyResponse() {
    $headers = array_merge(
      array('Content-Type' => Constants::MIME_TYPE),
      $this->getHeaders()
    );
    return new \Symfony\Component\HttpFoundation\Response(
      $this->encode(),
      $this->code,
      $headers
    );
  }