vendor/store.shopware.com/nrlejpostdirektautocomplete/vendor/slim/psr7/src/Response.php line 21

Open in your IDE?
  1. <?php
  2. /**
  3.  * Slim Framework (https://slimframework.com)
  4.  *
  5.  * @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
  6.  */
  7. declare(strict_types=1);
  8. namespace Slim\Psr7;
  9. use Fig\Http\Message\StatusCodeInterface;
  10. use InvalidArgumentException;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\StreamInterface;
  13. use Slim\Psr7\Factory\StreamFactory;
  14. use Slim\Psr7\Interfaces\HeadersInterface;
  15. use function is_integer;
  16. use function is_object;
  17. use function is_string;
  18. use function method_exists;
  19. class Response extends Message implements ResponseInterface
  20. {
  21.     /**
  22.      * @var int
  23.      */
  24.     protected $status StatusCodeInterface::STATUS_OK;
  25.     /**
  26.      * @var string
  27.      */
  28.     protected $reasonPhrase '';
  29.     /**
  30.      * @var array
  31.      */
  32.     protected static $messages = [
  33.         // Informational 1xx
  34.         StatusCodeInterface::STATUS_CONTINUE => 'Continue',
  35.         StatusCodeInterface::STATUS_SWITCHING_PROTOCOLS => 'Switching Protocols',
  36.         StatusCodeInterface::STATUS_PROCESSING => 'Processing',
  37.         // Successful 2xx
  38.         StatusCodeInterface::STATUS_OK => 'OK',
  39.         StatusCodeInterface::STATUS_CREATED => 'Created',
  40.         StatusCodeInterface::STATUS_ACCEPTED => 'Accepted',
  41.         StatusCodeInterface::STATUS_NON_AUTHORITATIVE_INFORMATION => 'Non-Authoritative Information',
  42.         StatusCodeInterface::STATUS_NO_CONTENT => 'No Content',
  43.         StatusCodeInterface::STATUS_RESET_CONTENT => 'Reset Content',
  44.         StatusCodeInterface::STATUS_PARTIAL_CONTENT => 'Partial Content',
  45.         StatusCodeInterface::STATUS_MULTI_STATUS => 'Multi-Status',
  46.         StatusCodeInterface::STATUS_ALREADY_REPORTED => 'Already Reported',
  47.         StatusCodeInterface::STATUS_IM_USED => 'IM Used',
  48.         // Redirection 3xx
  49.         StatusCodeInterface::STATUS_MULTIPLE_CHOICES => 'Multiple Choices',
  50.         StatusCodeInterface::STATUS_MOVED_PERMANENTLY => 'Moved Permanently',
  51.         StatusCodeInterface::STATUS_FOUND => 'Found',
  52.         StatusCodeInterface::STATUS_SEE_OTHER => 'See Other',
  53.         StatusCodeInterface::STATUS_NOT_MODIFIED => 'Not Modified',
  54.         StatusCodeInterface::STATUS_USE_PROXY => 'Use Proxy',
  55.         StatusCodeInterface::STATUS_RESERVED => '(Unused)',
  56.         StatusCodeInterface::STATUS_TEMPORARY_REDIRECT => 'Temporary Redirect',
  57.         StatusCodeInterface::STATUS_PERMANENT_REDIRECT => 'Permanent Redirect',
  58.         // Client Error 4xx
  59.         StatusCodeInterface::STATUS_BAD_REQUEST => 'Bad Request',
  60.         StatusCodeInterface::STATUS_UNAUTHORIZED => 'Unauthorized',
  61.         StatusCodeInterface::STATUS_PAYMENT_REQUIRED => 'Payment Required',
  62.         StatusCodeInterface::STATUS_FORBIDDEN => 'Forbidden',
  63.         StatusCodeInterface::STATUS_NOT_FOUND => 'Not Found',
  64.         StatusCodeInterface::STATUS_METHOD_NOT_ALLOWED => 'Method Not Allowed',
  65.         StatusCodeInterface::STATUS_NOT_ACCEPTABLE => 'Not Acceptable',
  66.         StatusCodeInterface::STATUS_PROXY_AUTHENTICATION_REQUIRED => 'Proxy Authentication Required',
  67.         StatusCodeInterface::STATUS_REQUEST_TIMEOUT => 'Request Timeout',
  68.         StatusCodeInterface::STATUS_CONFLICT => 'Conflict',
  69.         StatusCodeInterface::STATUS_GONE => 'Gone',
  70.         StatusCodeInterface::STATUS_LENGTH_REQUIRED => 'Length Required',
  71.         StatusCodeInterface::STATUS_PRECONDITION_FAILED => 'Precondition Failed',
  72.         StatusCodeInterface::STATUS_PAYLOAD_TOO_LARGE => 'Request Entity Too Large',
  73.         StatusCodeInterface::STATUS_URI_TOO_LONG => 'Request-URI Too Long',
  74.         StatusCodeInterface::STATUS_UNSUPPORTED_MEDIA_TYPE => 'Unsupported Media Type',
  75.         StatusCodeInterface::STATUS_RANGE_NOT_SATISFIABLE => 'Requested Range Not Satisfiable',
  76.         StatusCodeInterface::STATUS_EXPECTATION_FAILED => 'Expectation Failed',
  77.         StatusCodeInterface::STATUS_IM_A_TEAPOT => 'I\'m a teapot',
  78.         StatusCodeInterface::STATUS_MISDIRECTED_REQUEST => 'Misdirected Request',
  79.         StatusCodeInterface::STATUS_UNPROCESSABLE_ENTITY => 'Unprocessable Entity',
  80.         StatusCodeInterface::STATUS_LOCKED => 'Locked',
  81.         StatusCodeInterface::STATUS_FAILED_DEPENDENCY => 'Failed Dependency',
  82.         StatusCodeInterface::STATUS_UPGRADE_REQUIRED => 'Upgrade Required',
  83.         StatusCodeInterface::STATUS_PRECONDITION_REQUIRED => 'Precondition Required',
  84.         StatusCodeInterface::STATUS_TOO_MANY_REQUESTS => 'Too Many Requests',
  85.         StatusCodeInterface::STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE => 'Request Header Fields Too Large',
  86.         444 => 'Connection Closed Without Response',
  87.         StatusCodeInterface::STATUS_UNAVAILABLE_FOR_LEGAL_REASONS => 'Unavailable For Legal Reasons',
  88.         499 => 'Client Closed Request',
  89.         // Server Error 5xx
  90.         StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR => 'Internal Server Error',
  91.         StatusCodeInterface::STATUS_NOT_IMPLEMENTED => 'Not Implemented',
  92.         StatusCodeInterface::STATUS_BAD_GATEWAY => 'Bad Gateway',
  93.         StatusCodeInterface::STATUS_SERVICE_UNAVAILABLE => 'Service Unavailable',
  94.         StatusCodeInterface::STATUS_GATEWAY_TIMEOUT => 'Gateway Timeout',
  95.         StatusCodeInterface::STATUS_VERSION_NOT_SUPPORTED => 'HTTP Version Not Supported',
  96.         StatusCodeInterface::STATUS_VARIANT_ALSO_NEGOTIATES => 'Variant Also Negotiates',
  97.         StatusCodeInterface::STATUS_INSUFFICIENT_STORAGE => 'Insufficient Storage',
  98.         StatusCodeInterface::STATUS_LOOP_DETECTED => 'Loop Detected',
  99.         StatusCodeInterface::STATUS_NOT_EXTENDED => 'Not Extended',
  100.         StatusCodeInterface::STATUS_NETWORK_AUTHENTICATION_REQUIRED => 'Network Authentication Required',
  101.         599 => 'Network Connect Timeout Error',
  102.     ];
  103.     /**
  104.      * @param int                   $status  The response status code.
  105.      * @param HeadersInterface|null $headers The response headers.
  106.      * @param StreamInterface|null  $body    The response body.
  107.      */
  108.     public function __construct(
  109.         int $status StatusCodeInterface::STATUS_OK,
  110.         ?HeadersInterface $headers null,
  111.         ?StreamInterface $body null
  112.     ) {
  113.         $this->status $this->filterStatus($status);
  114.         $this->headers $headers $headers : new Headers([], []);
  115.         $this->body $body $body : (new StreamFactory())->createStream();
  116.     }
  117.     /**
  118.      * This method is applied to the cloned object after PHP performs an initial shallow-copy.
  119.      * This method completes a deep-copy by creating new objects for the cloned object's internal reference pointers.
  120.      */
  121.     public function __clone()
  122.     {
  123.         $this->headers = clone $this->headers;
  124.     }
  125.     /**
  126.      * {@inheritdoc}
  127.      */
  128.     public function getStatusCode(): int
  129.     {
  130.         return $this->status;
  131.     }
  132.     /**
  133.      * {@inheritdoc}
  134.      */
  135.     public function withStatus($code$reasonPhrase '')
  136.     {
  137.         $code $this->filterStatus($code);
  138.         $reasonPhrase $this->filterReasonPhrase($reasonPhrase);
  139.         $clone = clone $this;
  140.         $clone->status $code;
  141.         $clone->reasonPhrase $reasonPhrase;
  142.         return $clone;
  143.     }
  144.     /**
  145.      * {@inheritdoc}
  146.      */
  147.     public function getReasonPhrase(): string
  148.     {
  149.         if ($this->reasonPhrase !== '') {
  150.             return $this->reasonPhrase;
  151.         }
  152.         if (isset(static::$messages[$this->status])) {
  153.             return static::$messages[$this->status];
  154.         }
  155.         return '';
  156.     }
  157.     /**
  158.      * Filter HTTP status code.
  159.      *
  160.      * @param  int $status HTTP status code.
  161.      *
  162.      * @return int
  163.      *
  164.      * @throws InvalidArgumentException If an invalid HTTP status code is provided.
  165.      */
  166.     protected function filterStatus($status): int
  167.     {
  168.         if (!is_integer($status) || $status StatusCodeInterface::STATUS_CONTINUE || $status 599) {
  169.             throw new InvalidArgumentException('Invalid HTTP status code.');
  170.         }
  171.         return $status;
  172.     }
  173.     /**
  174.      * Filter Reason Phrase
  175.      *
  176.      * @param mixed $reasonPhrase
  177.      *
  178.      * @return string
  179.      *
  180.      * @throws InvalidArgumentException
  181.      */
  182.     protected function filterReasonPhrase($reasonPhrase ''): string
  183.     {
  184.         if (is_object($reasonPhrase) && method_exists($reasonPhrase'__toString')) {
  185.             $reasonPhrase = (string) $reasonPhrase;
  186.         }
  187.         if (!is_string($reasonPhrase)) {
  188.             throw new InvalidArgumentException('Response reason phrase must be a string.');
  189.         }
  190.         if (strpos($reasonPhrase"\r") || strpos($reasonPhrase"\n")) {
  191.             throw new InvalidArgumentException(
  192.                 'Reason phrase contains one of the following prohibited characters: \r \n'
  193.             );
  194.         }
  195.         return $reasonPhrase;
  196.     }
  197. }