Formula for setting header / status code / content.

@param mixed $content Any content that will be serialized to output.
@param mixed $code The response status code.
@return string
970019
public function response($content, $code)
    {
        $this->setJsonHeader();

        $this->setStatusCode($code);

        return $this->content($this->serializer->serialize($content));
    }
751358
public function setStatusCode($code, $text = null)
    {
        $this->statusCode = (int)$code;

        if ($text) {
            $this->statusText = $text;
        } elseif (isset($this->statusTexts[$code])) {
            $this->statusText = $this->statusTexts[$code];
        }

        return $this;
    }
1062445
protected function encode($content)
    {
        $jsonString = parent::encode($content);

        if ($this->hasValidCallback()) {
            return \sprintf('%s(%s);', $this->getCallback(), $jsonString);
        }

        return $jsonString;
    }
1163523
public function setStatusCode($code)
    {
        $this->headerBag->setStatusCode($code);
        $this->statusCode = $code;

        return $this;
    }
1225894
public static function setStatus($code) {

		if(false === ('-' . intval($code) == '-' . $code)) {
			return trigger_error(sprintf('Argument 1 passed to %s() must be of the type integer, "%s" given', __METHOD__, gettype($code)), E_USER_ERROR);
		}

		$protocol = isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0';

		switch($code) {

			case 100 : header($protocol . ' 100 Continue'); break;
			case 101 : header($protocol . ' 101 Switching Protocols'); break;
			case 102 : header($protocol . ' 102 Processing'); break;
			case 200 : header($protocol . ' 200 OK'); break;
			case 201 : header($protocol . ' 201 Created'); break;
			case 202 : header($protocol . ' 202 Accepted'); break;
			case 203 : header($protocol . ' 203 Non-Authoritative Information'); break;
			case 204 : header($protocol . ' 204 No Content'); break;
			case 205 : header($protocol . ' 205 Reset Content'); break;
			case 206 : header($protocol . ' 206 Partial Content'); break;
			case 207 : header($protocol . ' 207 Multi-Status'); break;
			case 208 : header($protocol . ' 208 Already Reported'); break;
			case 226 : header($protocol . ' 226 IM Used'); break;
			case 300 : header($protocol . ' 300 Multiple Choices'); break;
			case 301 : header($protocol . ' 301 Moved Permanently'); break;
			case 302 : header($protocol . ' 302 Found'); break;
			case 303 : header($protocol . ' 303 See Other'); break;
			case 304 : header($protocol . ' 304 Not Modified'); break;
			case 305 : header($protocol . ' 305 Use Proxy'); break;
			case 306 : header($protocol . ' 306 Switch Proxy'); break;
			case 307 : header($protocol . ' 307 Temporary Redirect'); break;
			case 308 : header($protocol . ' 308 Permanent Redirect'); break;
			case 400 : header($protocol . ' 400 Bad Request'); break;
			case 401 : header($protocol . ' 401 Unauthorized'); break;
			case 402 : header($protocol . ' 402 Payment Required'); break;
			case 403 : header($protocol . ' 403 Forbidden'); break;
			case 404 : header($protocol . ' 404 Not Found'); break;
			case 405 : header($protocol . ' 405 Method Not Allowed'); break;
			case 406 : header($protocol . ' 406 Not Acceptable'); break;
			case 407 : header($protocol . ' 407 Proxy Authentication Required'); break;
			case 408 : header($protocol . ' 408 Request Timeout'); break;
			case 409 : header($protocol . ' 409 Conflict'); break;
			case 410 : header($protocol . ' 410 Gone'); break;
			case 411 : header($protocol . ' 411 Length Required'); break;
			case 412 : header($protocol . ' 412 Precondition Failed'); break;
			case 413 : header($protocol . ' 413 Payload Too Large'); break;
			case 414 : header($protocol . ' 414 Request-URI Too Long'); break;
			case 415 : header($protocol . ' 415 Unsupported Media Type'); break;
			case 416 : header($protocol . ' 416 Requested Range Not Satisfiable'); break;
			case 417 : header($protocol . ' 417 Expectation Failed'); break;
			case 418 : header($protocol . ' 418 I\'m a teapot'); break;
			case 419 : header($protocol . ' 419 Authentication Timeout'); break;
			case 420 : header($protocol . ' 420 Method Failure'); break;
			case 421 : header($protocol . ' 421 Misdirected Request'); break;
			case 422 : header($protocol . ' 422 Unprocessable Entity'); break;
			case 423 : header($protocol . ' 423 Locked'); break;
			case 424 : header($protocol . ' 424 Failed Dependency'); break;
			case 426 : header($protocol . ' 426 Upgrade Required'); break;
			case 428 : header($protocol . ' 428 Precondition Required'); break;
			case 429 : header($protocol . ' 429 Too Many Requests'); break;
			case 431 : header($protocol . ' 431 Request Header Fields Too Large'); break;
			case 440 : header($protocol . ' 440 Login Timeout '); break;
			case 444 : header($protocol . ' 444 No Response'); break;
			case 449 : header($protocol . ' 449 Retry With'); break;
			case 450 : header($protocol . ' 450 Blocked by Windows Parental Controls'); break;
			case 451 : header($protocol . ' 451 Unavailable For Legal Reasons'); break;
			case 494 : header($protocol . ' 494 Request Header Too Large'); break;
			case 495 : header($protocol . ' 495 Cert Error'); break;
			case 496 : header($protocol . ' 496 No Cert'); break;
			case 497 : header($protocol . ' 497 HTTP to HTTPS'); break;
			case 498 : header($protocol . ' 498 Token expired/invalid'); break;
			case 499 : header($protocol . ' 499 Client Closed Request'); break;
			case 500 : header($protocol . ' 500 Internal Server Error'); break;
			case 501 : header($protocol . ' 501 Not Implemented'); break;
			case 502 : header($protocol . ' 502 Bad Gateway'); break;
			case 503 : header($protocol . ' 503 Service Unavailable'); break;
			case 504 : header($protocol . ' 504 Gateway Timeout'); break;
			case 505 : header($protocol . ' 505 HTTP Version Not Supported'); break;
			case 506 : header($protocol . ' 506 Variant Also Negotiates'); break;
			case 507 : header($protocol . ' 507 Insufficient Storage'); break;
			case 508 : header($protocol . ' 508 Loop Detected'); break;
			case 509 : header($protocol . ' 509 Bandwidth Limit Exceeded'); break;
			case 510 : header($protocol . ' 510 Not Extended'); break;
			case 511 : header($protocol . ' 511 Network Authentication Required'); break;
			case 520 : header($protocol . ' 520 Unknown Error'); break;
			case 522 : header($protocol . ' 522 Origin Connection Time-out'); break;
			case 598 : header($protocol . ' 598 Network read timeout error'); break;
			case 599 : header($protocol . ' 599 Network connect timeout error'); break;
		}
	}
1020548
function responseCode($code = null) {
		if ($code === null) {
			return $this->http_response_code;
		}
		else { // $code !== null, act as setter
			$this->http_response_code = $code;
			return $this;
		}
	}
140813
public function seeResponseCodeIs($code)
    {
        $failureMessage = sprintf(
            'Expected HTTP Status Code: %s. Actual Status Code: %s',
            HttpCode::getDescription($code),
            HttpCode::getDescription($this->getResponseStatusCode())
        );
        $this->assertEquals($code, $this->getResponseStatusCode(), $failureMessage);
    }
626710
public function response($content, int $statusCode = 200, string $statusMessage = 'OK'): Response
    {
        $response = [
            'statusCode' => $statusCode,
            'statusMessage' => $statusMessage,
            'content' => $content,
        ];

        if ($this->config->application->debug->logRequest) {
            $this->log->addInfo('RESPONSE', $response);
        }

        // Create a response since it's an ajax
        //in order to use the current response instead of having to create a new object , this is needed for swoole servers
        $response = $this->response ?? new Response(); 
        $response->setStatusCode($statusCode, $statusMessage);
        $response->setContentType('application/vnd.api+json', 'UTF-8');
        $response->setJsonContent($content);

        return $response;
    }
929406
public function setContent($content)
    {
        if (!$json = json_encode($content)) {
            $this->setResponseCode(self::HTTP_INTERNAL_SERVER_ERROR);
            $json = json_encode(['status' => 'error', 'error_message' => json_last_error_msg()]);
        }

        $this->content = $json;

        return $this;
    }
560629
protected function detectFormatByContent($content)
    {
        if (preg_match('/^(\\{|\\[\\{).*(\\}|\\}\\])$/is', $content)) {
            return Client::FORMAT_JSON;
        }
        if (preg_match('/^([^=&])+=[^=&]+(&[^=&]+=[^=&]+)*$/', $content)) {
            return Client::FORMAT_URLENCODED;
        }
        if (preg_match('/^<\?xml.*>$/s', $content)) {
            return Client::FORMAT_XML;
        }
        return null;
    }
1100712
protected function createJsonApiResponse(
        ?string $content,
        int $statusCode,
        array $headers = [],
        $addContentType = true
    ) {
        if ($addContentType === true) {
            $headers[self::HEADER_CONTENT_TYPE] = $this->getMediaType()->getMediaType();
        }

        return $this->createResponse($content, $statusCode, $headers);
    }
1210082
public static function statusCode($code)
    {
        $status = [
            // Informational 1xx
            100 => 'Continue',
            101 => 'Switching Protocols',
            // Success 2xx
            200 => 'OK',
            201 => 'Created',
            202 => 'Accepted',
            203 => 'Non-Authoritative Information',
            204 => 'No Content',
            205 => 'Reset Content',
            206 => 'Partial Content',
            // Redirection 3xx
            300 => 'Multiple Choices',
            301 => 'Moved Permanently',
            302 => 'Moved Temporarily ',  // 1.1
            303 => 'See Other',
            304 => 'Not Modified',
            305 => 'Use Proxy',
            // 306 is deprecated but reserved
            307 => 'Temporary Redirect',
            // Client Error 4xx
            400 => 'Bad Request',
            401 => 'Unauthorized',
            402 => 'Payment Required',
            403 => 'Forbidden',
            404 => 'Not Found',
            405 => 'Method Not Allowed',
            406 => 'Not Acceptable',
            407 => 'Proxy Authentication Required',
            408 => 'Request Timeout',
            409 => 'Conflict',
            410 => 'Gone',
            411 => 'Length Required',
            412 => 'Precondition Failed',
            413 => 'Request Entity Too Large',
            414 => 'Request-URI Too Long',
            415 => 'Unsupported Media Type',
            416 => 'Requested Range Not Satisfiable',
            417 => 'Expectation Failed',
            // Server Error 5xx
            500 => 'Internal Server Error',
            501 => 'Not Implemented',
            502 => 'Bad Gateway',
            503 => 'Service Unavailable',
            504 => 'Gateway Timeout',
            505 => 'HTTP Version Not Supported',
            509 => 'Bandwidth Limit Exceeded'
        ];
        if (array_key_exists($code, $status)) {
            header('HTTP/1.1 ' . $code . ' ' . $status[$code]);
        }
    }
870022
public function setContent(string $content = null): ResponseContextAbstract
    {
        if (is_null($content)) {
            return $this;
        }

        $this->assertIsValid($content);

        $this->content = $content;

        return $this;
    }
899009
public function getHeader($code)
    {
        if (isset($this->headers[$code])) {
            return $this->headers[$code];
        }

        return null;
    }
104421
protected function createErrorResponse($content = 'Unknown Error', $code = 500)
    {
        $data = [
            'message' => $content
        ];

        $response = $this->createJsonResponse($data, $code);
        return $response;
    }
1371809
private static function parseHeaderContent($content)
    {
        $parts = explode(';', $content);
        $headerValue = array_shift($parts);
        $options = array();
        // Parse options
        foreach ($parts as $part) {
            if (false === empty($part)) {
                $partSplit = explode('=', $part, 2);
                if (2 === count($partSplit)) {
                    list ($key, $value) = $partSplit;
                    if ('*' === substr($key, -1)) {
                        // RFC 5987
                        $key = substr($key, 0, -1);
                        if (preg_match(
                            "/(?P<charset>[\w!#$%&+^_`{}~-]+)'(?P<language>[\w-]*)'(?P<value>.*)$/",
                            $value,
                            $matches
                        )) {
                            $value = mb_convert_encoding(
                                rawurldecode($matches['value']),
                                'utf-8',
                                $matches['charset']
                            );
                        }
                    }
                    $options[trim($key)] = trim($value, ' "');
                } else {
                    // Bogus option
                    $options[$partSplit[0]] = '';
                }
            }
        }

        return array($headerValue, $options);
    }
1252400
public function setStatusCode($code, $text = null)
    {
        $code = intval($code);
        if ($code < 100 || $code > 999)
        {
            throw new InvalidArgumentException('Status code must be from 100 to 999');
        }
        $this->status = $code;
        $this->statusText = $text;
        return $this;
    }