Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XML attributes in XmlResponseFormatter #5996

Closed
arturf opened this issue Nov 10, 2014 · 1 comment
Closed

XML attributes in XmlResponseFormatter #5996

arturf opened this issue Nov 10, 2014 · 1 comment

Comments

@arturf
Copy link
Contributor

arturf commented Nov 10, 2014

At current implementation XmlResponseFormatter allows to build xml from array/objects but there is no ability to add some attributes for nodes.
In my project I'm using an inherited class with some additions.

/**
     * @inheritdoc
     */
    protected function buildXml($element, $data)
    {
    if (isset($data['attributes'])) {
            $this->attributes = $data['attributes'];
            unset ($data['attributes']);
        }
        parent::buildXml($element, $data);

        $this->buildAttributes($element);
    }

    /**
     * @param DOMElement $element
     */
    protected function buildAttributes($element)
    {
        if (is_null($this->attributes)) {
            return;
        }

        foreach ($this->attributes as $name => $value) {
            $element->setAttribute($name, $value);
        }

        $this->attributes = null;
    }

And some part of code, that adds ability to add attributes for rootTag

I think that this can be good in core. If ok, I will send a PR.

@qiangxue
Copy link
Member

We do not have plan to support this out-of-box for the following reasons:

  • XmlResponseFormatter is a generic class which cannot assume attributes as a special one;
  • XmlResponseFormatter is meant to be extended if you have special format requirement like this;
  • There is a special requirement for the data format of attributes if you want to save them as tag attributes;
  • When tag attribute is supported, it will not be easy to convert back to arrays from XMLs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants