/** * Formats response data in JSONP format. * @param Response $response */ protected function formatJsonp($response) { $response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8'); if (is_array($response->data) && isset($response->data['data'], $response->data['callback'])) { $response->content = sprintf('%s(%s);', $response->data['callback'], Json::htmlEncode($response->data['data'])); } elseif ($response->data !== null) { $response->content = ''; Leaps::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__); } }
/** * Formats the specified response. * * @param Response $response the response to be formatted. */ public function format($response) { if (stripos($this->contentType, 'charset') === false) { $this->contentType .= '; charset=' . $response->charset; } $response->getHeaders()->set('Content-Type', $this->contentType); if ($response->data !== null) { $response->content = $response->data; } }
/** * Formats the specified response. * @param Response $response the response to be formatted. */ public function format($response) { $charset = $this->encoding === null ? $response->charset : $this->encoding; if (stripos($this->contentType, 'charset') === false) { $this->contentType .= '; charset=' . $charset; } $response->getHeaders()->set('Content-Type', $this->contentType); if ($response->data !== null) { $dom = new DOMDocument($this->version, $charset); $root = new DOMElement($this->rootTag); $dom->appendChild($root); $this->buildXml($root, $response->data); $response->content = $dom->saveXML(); } }
/** * Adds the rate limit headers to the response * @param Response $response * @param integer $limit the maximum number of allowed requests during a period * @param integer $remaining the remaining number of allowed requests within the current period * @param integer $reset the number of seconds to wait before having maximum number of allowed requests again */ public function addRateLimitHeaders($response, $limit, $remaining, $reset) { if ($this->enableRateLimitHeaders) { $response->getHeaders()->set('X-Rate-Limit-Limit', $limit)->set('X-Rate-Limit-Remaining', $remaining)->set('X-Rate-Limit-Reset', $reset); } }
/** * Adds the CORS headers to the response * @param Response $response * @param array CORS headers which have been computed */ public function addCorsHeaders($response, $headers) { if (empty($headers) === false) { $responseHeaders = $response->getHeaders(); foreach ($headers as $field => $value) { $responseHeaders->set($field, $value); } } }
/** * Restores response properties from the given data * @param Response $response the response to be restored * @param array $data the response property data * @since 2.0.3 */ protected function restoreResponse($response, $data) { if (isset($data['format'])) { $response->format = $data['format']; } if (isset($data['version'])) { $response->version = $data['version']; } if (isset($data['statusCode'])) { $response->statusCode = $data['statusCode']; } if (isset($data['statusText'])) { $response->statusText = $data['statusText']; } if (isset($data['headers']) && is_array($data['headers'])) { $headers = $response->getHeaders()->toArray(); $response->getHeaders()->fromArray(array_merge($data['headers'], $headers)); } if (isset($data['cookies']) && is_array($data['cookies'])) { $cookies = $response->getCookies()->toArray(); $response->getCookies()->fromArray(array_merge($data['cookies'], $cookies)); } }