Encode the body based on the content type

@see lithium\net\http\Message::type()
@param mixed $body
@return string
437330
protected function _encode($body) {
		$media = $this->_classes['media'];

		if ($media::type($this->_type)) {
			$encoded = $media::encode($this->_type, $body);
			$body = $encoded !== null ? $encoded : $body;
		}
		return $body;
	}
254127
public function setBody($body) {
        if (is_string($body) || is_null($body)) {
            $this->rawBody = $this->body = $body;
        } elseif (is_array($body) || is_bool($body) || is_numeric($body) || $body instanceof \JsonSerializable) {
            $this->rawBody = json_encode($body, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
            $this->body = $body;
        } else {
            $this->rawBody = '';
            $this->body = $body;
        }

        return $this;
    }
1371507
public function withBody($body): self {
        $clone = clone $this;

        if ($body === null) {
            $clone->body = new StringBody("");
        } elseif (\is_scalar($body)) {
            $clone->body = new StringBody((string) $body);
        } elseif ($body instanceof RequestBody) {
            $clone->body = $body;
        } else {
            throw new \TypeError("Invalid body type: " . gettype($body));
        }

        return $clone;
    }
179514
private function decodeJsonBody($body)
    {
        $data = json_decode($body, true);
        if ($data === null) {
            throw new InvalidRequestException('The request JSON body could not be decoded');
        }

        return $data;
    }
1077214
public function setBody(string $body, string $contentType = 'text/html')
    {
        $this->setHeader('Content-Type', $contentType);
        $this->body = $body;
        return $this;
    }
1254567
protected function configureBody($body){
        $upload = $this->getUpload();
        switch ($this->method) {
            case self::HTTP_GET:
                if (!$upload){
                    if (is_array($body) || is_object($body)){
                        $queryParams = http_build_query($body);
                    } else {
                        $queryParams = $body;
                    }
                    if (!empty($body)){
                        if (strpos($this->url, "?") === false) {
                            $queryParams = "?".$queryParams;
                        } else {
                            $queryParams = "&".$queryParams;
                        }
                        return $this->configureUrl($this->url.$queryParams);
                    }
                    break;
                }
            default:
                if ($upload){
                    $this->addHeader('Content-Type','multipart/form-data');
                }
                return $this->addCurlOption(CURLOPT_POSTFIELDS, $body);
        }
    }
1161737
protected function parse($body)
    {
        $disableEntities = libxml_disable_entity_loader(true);
        $internalErrors = libxml_use_internal_errors(true);

        try {
            $xml = new SimpleXMLElement((string) $body ?: '<root />', LIBXML_NONET);

            return json_decode(json_encode($xml), true);
        } catch (Exception $e) {
            //
        } catch (Throwable $e) {
            //
        } finally {
            libxml_disable_entity_loader($disableEntities);
            libxml_use_internal_errors($internalErrors);
        }
    }
1140751
protected function setBody($body = 'php://temp')
    {
        if (is_string($body) || is_resource($body)) {
            $body = new Stream($body, 'w+b');
        }
        if (! $body instanceof StreamInterface) {
            throw new InvalidArgumentException(sprintf(
                'Invalid body provided. Must be a string resource identifier, or resource, '
                . 'or instance of Psr\Http\Message\StreamInterface; received "%s".',
                is_object($body) ? get_class($body) : gettype($body)
            ));
        }
        $this->setBodyInstance($body);

        return $this;
    }
1178498
public function setBody($body, $contentType = null, $charset = null)
    {
        if (isset($charset)) {
            $this->setCharset($charset);
        }
        $body = $this->convertString($body);

        parent::setBody($body, $contentType);

        return $this;
    }
77938
public function parseJSON($body)
    {
        if ($body instanceof ResponseInterface) {
            $body = $body->getBody();
        }

        // XXX: json maybe contains special chars. So, let's FUCK the WeChat API developers ...
        $body = $this->fuckTheWeChatInvalidJSON($body);

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

        $contents = json_decode($body, true);

        Log::debug('API response decoded:', compact('contents'));

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new HttpException('Failed to parse JSON: '.json_last_error_msg());
        }

        return $contents;
    }
1056665
private function write($body) {
    if (!$this->file_handle) {
      return false;
    }

      $content = json_encode($body);
    $content.= "\n";

    return fwrite($this->file_handle, $content) == strlen($content);
  }
202946
public function setBody($body, $contentType = null)
    {
        if ($body !== $this->body) {
            $this->clearCache();
        }

        $this->body = $body;
        if (null !== $contentType) {
            $this->setContentType($contentType);
        }

        return $this;
    }
272535
protected static function decode($body, $encoding)
    {
        if (ENCQUOTEDPRINTABLE === $encoding) {
            return quoted_printable_decode($body);
        }

        if (ENCBASE64 === $encoding) {
            return base64_decode($body);
        }

        return $body;
    }
887494
protected function decodeErrorBody($body)
    {
        try {
            $decoded = Json::decode($body);
            if (isset($decoded['error']) && !is_array($decoded['error'])) {
                $decoded['error'] = preg_replace('/\b\w+?Exception\[/', "<span style=\"color: red;\">\\0</span>\n               ", $decoded['error']);
            }
            return $decoded;
        } catch(InvalidParamException $e) {
            return $body;
        }
    }
1318072
public function body($body)
    {
        if ($body instanceof ReadableStreamInterface) {
            return new ReadableBodyStream($body);
        }

        return \RingCentral\Psr7\stream_for($body);
    }
118475
public static function decodeChunkedBody($body)
    {
        $decBody = '';
        
        while (trim($body)) {
            if (! preg_match("/^([\da-fA-F]+)[^\r\n]*\r\n/sm", $body, $m)) {
                require_once 'Zend/Http/Exception.php';
                throw new Zend_Http_Exception("Error parsing body - doesn't seem to be a chunked message");
            }

            $length = hexdec(trim($m[1]));
            $cut = strlen($m[0]);

            $decBody .= substr($body, $cut, $length);
            $body = substr($body, $cut + $length + 2);
        }

        return $decBody;
    }
1301621
public function getResponseBody($encodeEntities = true)
    {
        $body = (string) $this->getResponse()->getBody();

        if ($encodeEntities) {
            return htmlentities($body);
        }

        return $body;
    }