[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/laminas/laminas-diactoros/src/Response/ -> XmlResponse.php (source)

   1  <?php
   2  
   3  /**
   4   * @see       https://github.com/laminas/laminas-diactoros for the canonical source repository
   5   * @copyright https://github.com/laminas/laminas-diactoros/blob/master/COPYRIGHT.md
   6   * @license   https://github.com/laminas/laminas-diactoros/blob/master/LICENSE.md New BSD License
   7   */
   8  
   9  declare(strict_types=1);
  10  
  11  namespace Laminas\Diactoros\Response;
  12  
  13  use Laminas\Diactoros\Exception;
  14  use Laminas\Diactoros\Response;
  15  use Laminas\Diactoros\Stream;
  16  use Psr\Http\Message\StreamInterface;
  17  
  18  use function get_class;
  19  use function gettype;
  20  use function is_object;
  21  use function is_string;
  22  use function sprintf;
  23  
  24  /**
  25   * XML response.
  26   *
  27   * Allows creating a response by passing an XML string to the constructor; by default,
  28   * sets a status code of 200 and sets the Content-Type header to application/xml.
  29   */
  30  class XmlResponse extends Response
  31  {
  32      use InjectContentTypeTrait;
  33  
  34      /**
  35       * Create an XML response.
  36       *
  37       * Produces an XML response with a Content-Type of application/xml and a default
  38       * status of 200.
  39       *
  40       * @param string|StreamInterface $xml String or stream for the message body.
  41       * @param int $status Integer status code for the response; 200 by default.
  42       * @param array $headers Array of headers to use at initialization.
  43       * @throws Exception\InvalidArgumentException if $text is neither a string or stream.
  44       */
  45      public function __construct(
  46          $xml,
  47          int $status = 200,
  48          array $headers = []
  49      ) {
  50          parent::__construct(
  51              $this->createBody($xml),
  52              $status,
  53              $this->injectContentType('application/xml; charset=utf-8', $headers)
  54          );
  55      }
  56  
  57      /**
  58       * Create the message body.
  59       *
  60       * @param string|StreamInterface $xml
  61       * @throws Exception\InvalidArgumentException if $xml is neither a string or stream.
  62       */
  63      private function createBody($xml) : StreamInterface
  64      {
  65          if ($xml instanceof StreamInterface) {
  66              return $xml;
  67          }
  68  
  69          if (! is_string($xml)) {
  70              throw new Exception\InvalidArgumentException(sprintf(
  71                  'Invalid content (%s) provided to %s',
  72                  (is_object($xml) ? get_class($xml) : gettype($xml)),
  73                  __CLASS__
  74              ));
  75          }
  76  
  77          $body = new Stream('php://temp', 'wb+');
  78          $body->write($xml);
  79          $body->rewind();
  80          return $body;
  81      }
  82  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer