CoreFiles by mySites.guru
XmlResponse.php75 lines, 2,137 bytes, from Joomla_6.2.0-beta2-Beta-Full_Package.zip
Raw History Compare with 6.2.0-beta1
md5
21f8546d16dd95254b72e406b35cfabd
sha1
3c15c7856dc037ef0e8855351e0685483affbaa0
sha256
520b357544bfaebebc928c401561f9893256840ca86323591dc611bf22925274
<?php

declare(strict_types=1);

namespace Laminas\Diactoros\Response;

use Laminas\Diactoros\Exception;
use Laminas\Diactoros\Response;
use Laminas\Diactoros\Stream;
use Psr\Http\Message\StreamInterface;

use function get_debug_type;
use function is_string;
use function sprintf;

/**
 * XML response.
 *
 * Allows creating a response by passing an XML string to the constructor; by default,
 * sets a status code of 200 and sets the Content-Type header to application/xml.
 */
class XmlResponse extends Response
{
    use InjectContentTypeTrait;

    /**
     * Create an XML response.
     *
     * Produces an XML response with a Content-Type of application/xml and a default
     * status of 200.
     *
     * @param string|StreamInterface $xml String or stream for the message body.
     * @param int $status Integer status code for the response; 200 by default.
     * @param array<non-empty-string, string|string[]> $headers Array of headers to use at initialization.
     * @throws Exception\InvalidArgumentException If $text is neither a string or stream.
     */
    public function __construct(
        $xml,
        int $status = 200,
        array $headers = []
    ) {
        parent::__construct(
            $this->createBody($xml),
            $status,
            $this->injectContentType('application/xml; charset=utf-8', $headers)
        );
    }

    /**
     * Create the message body.
     *
     * @param string|StreamInterface $xml
     * @throws Exception\InvalidArgumentException If $xml is neither a string or stream.
     */
    private function createBody($xml): StreamInterface
    {
        if ($xml instanceof StreamInterface) {
            return $xml;
        }

        /** @psalm-suppress DocblockTypeContradiction */
        if (! is_string($xml)) {
            throw new Exception\InvalidArgumentException(sprintf(
                'Invalid content (%s) provided to %s',
                get_debug_type($xml),
                self::class
            ));
        }

        $body = new Stream('php://temp', 'wb+');
        $body->write($xml);
        $body->rewind();
        return $body;
    }
}

The same content is in 32 releases, Joomla 6.1.0-alpha2 to 6.3.0-alpha1. See them all, or every version of this file.