Beispiel #1
0
 /**
  * Create a new HTTP text response.
  * 
  * @param string $contents Text to be transfered as response body.
  * @param string $type media type of the text payload.
  * @param string $charset Charset to be used (default to UTF-8)..
  */
 public function __construct(string $contents, string $type = 'text/plain', string $charset = null)
 {
     $type = new ContentType($type);
     if ($type->getMediaType()->isText()) {
         $type->setParam('charset', $charset ?? 'utf-8');
     }
     parent::__construct(Http::OK, ['Content-Type' => (string) $type]);
     $this->body = new StringBody($contents);
 }
Beispiel #2
0
 /**
  * Create HTTP file response.
  * 
  * @param string $file Absolute path of the file to be transfered.
  * @param string $type Media type of the file (will be guessed from extension if not provided).
  * @param string $charset Charset to be supplied when media type is text format.
  */
 public function __construct(string $file, string $type = null, string $charset = null)
 {
     $type = new ContentType($type ?? Filesystem::guessMimeTypeFromFilename($file));
     if ($type->getMediaType()->isText()) {
         $type->setParam('charset', $charset ?? 'utf-8');
     }
     parent::__construct(Http::OK, ['Content-Type' => (string) $type]);
     $this->body = new FileBody($file);
     $this->file = $file;
 }
Beispiel #3
0
 /**
  * Create an XML repsonse from the given payload.
  * 
  * Supports XML strings, DOM documents / nodes and SimpleXML elements.
  * 
  * @param mixed $payload
  */
 public function __construct($payload)
 {
     if ($payload instanceof \DOMDocument) {
         $payload = $payload->saveXML();
     } elseif ($payload instanceof \DOMNode) {
         $payload = $payload->ownerDocument->saveXml($payload);
     } elseif ($payload instanceof \SimpleXMLElement) {
         $payload = $payload->asXML();
     } else {
         $payload = (string) $payload;
     }
     parent::__construct(Http::OK, ['Content-Type' => 'application/xml']);
     $this->body = new StringBody($payload);
 }
 /**
  * Create an HTTP redirect response.
  * 
  * @param string $uri The target URI to redirect the recipient to.
  * @param int $status HTTP status code to be used for the redirect.
  * 
  * @throws \InvalidArgumentException When the given HTTP status code is not usable as a redirect code.
  */
 public function __construct($uri, int $status = Http::SEE_OTHER)
 {
     switch ($status) {
         case Http::MOVED_PERMANENTLY:
         case Http::FOUND:
         case Http::SEE_OTHER:
         case Http::TEMPORARY_REDIRECT:
         case Http::PERMANENT_REDIRECT:
             // These are valid redirect codes ;)
             break;
         default:
             throw new \InvalidArgumentException(\sprintf('Invalid HTTP redirect status code: "%s"', $status));
     }
     parent::__construct($status, ['Location' => (string) Uri::parse($uri)]);
 }
Beispiel #5
0
 /**
  * Create a new JSON-encoded HTTP response.
  * 
  * @param mixed $payload Payload to be transfered.
  * @param bool $encode Encode payload as JSON (turn this off for payloads that are already encoded as JSON)?
  * @param int $options Options to be passed to JSON encoder.
  */
 public function __construct($payload, bool $encode = true, int $options = null)
 {
     static $defaultOptions = \JSON_UNESCAPED_SLASHES | \JSON_HEX_AMP | \JSON_HEX_APOS | \JSON_HEX_QUOT | \JSON_HEX_TAG;
     parent::__construct(Http::OK, ['Content-Type' => 'application/json;charset="utf-8"']);
     $this->body = new StringBody($encode ? \json_encode($payload, $options ?? $defaultOptions) : $payload);
 }