0 follower

Final Class Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter

InheritanceYiisoft\DataResponse\Formatter\XmlDataResponseFormatter
ImplementsYiisoft\DataResponse\DataResponseFormatterInterface
Uses TraitsYiisoft\DataResponse\ResponseContentTrait

XmlDataResponseFormatter formats the response data as XML.

Public Methods

Hide inherited methods

Method Description Defined By
format() Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter
withContentType() Returns a new instance with the specified content type. Yiisoft\DataResponse\ResponseContentTrait
withEncoding() Returns a new instance with the specified encoding. Yiisoft\DataResponse\ResponseContentTrait
withRootTag() Returns a new instance with the specified root tag. Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter
withVersion() Returns a new instance with the specified version. Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter

Constants

Hide inherited constants

Constant Value Description Defined By
DEFAULT_ITEM_TAG_NAME 'item' Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter

Method Details

Hide inherited methods

format() public method

public \Psr\Http\Message\ResponseInterface format ( Yiisoft\DataResponse\DataResponse $dataResponse )
$dataResponse Yiisoft\DataResponse\DataResponse

                public function format(DataResponse $dataResponse): ResponseInterface
{
    if (!$dataResponse->hasData()) {
        return $this->addToResponse($dataResponse->getResponse());
    }
    $dom = new DOMDocument($this->version, $this->encoding);
    $data = $dataResponse->getData();
    if (empty($this->rootTag)) {
        $this->buildXml($dom, $dom, $data);
        return $this->addToResponse($dataResponse->getResponse(), (string) $dom->saveXML());
    }
    $root = new DOMElement($this->rootTag);
    $dom->appendChild($root);
    $this->buildXml($dom, $root, $data);
    return $this->addToResponse($dataResponse->getResponse(), (string) $dom->saveXML());
}

            
withContentType() public method

Defined in: Yiisoft\DataResponse\ResponseContentTrait::withContentType()

Returns a new instance with the specified content type.

public self withContentType ( string $contentType )
$contentType string

The content type. For example, "text/html".

                public function withContentType(string $contentType): self
{
    $new = clone $this;
    $new->contentType = $contentType;
    return $new;
}

            
withEncoding() public method

Defined in: Yiisoft\DataResponse\ResponseContentTrait::withEncoding()

Returns a new instance with the specified encoding.

public self withEncoding ( string $encoding )
$encoding string

The encoding. For example, "UTF-8".

                public function withEncoding(string $encoding): self
{
    $new = clone $this;
    $new->encoding = $encoding;
    return $new;
}

            
withRootTag() public method

Returns a new instance with the specified root tag.

public self withRootTag ( string $rootTag )
$rootTag string

The name of the root element. Default is "response". If an empty value is set, the root tag should not be added.

                public function withRootTag(string $rootTag): self
{
    $new = clone $this;
    $new->rootTag = $rootTag;
    return $new;
}

            
withVersion() public method

Returns a new instance with the specified version.

public self withVersion ( string $version )
$version string

The XML version. Default is "1.0".

                public function withVersion(string $version): self
{
    $new = clone $this;
    $new->version = $version;
    return $new;
}