mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-03 05:48:11 +08:00
Fix #18783: Add support for URI namespaced tags in XmlResponseFormatter, add XmlResponseFormatter::$objectTagToLowercase option to lowercase object tags
Co-authored-by: Alexander Makarov <sam@rmcreative.ru>
This commit is contained in:
@ -8,7 +8,8 @@ Yii Framework 2 Change Log
|
||||
- Enh #18826: Add ability to turn the sorting off for a clicked column in GridView with multisort (ditibal)
|
||||
- Bug #18646: Remove stale identity data from session if `IdentityInterface::findIdentity()` returns `null` (mikehaertl)
|
||||
- Bug #18832: Fix `Inflector::camel2words()` adding extra spaces (brandonkelly)
|
||||
|
||||
- Enh #18783: Add support for URI namespaced tags in `XmlResponseFormatter` (WinterSilence, samdark)
|
||||
- Enh #18783: Add `XmlResponseFormatter::$objectTagToLowercase` option to lowercase object tags (WinterSilence, samdark)
|
||||
|
||||
2.0.43 August 09, 2021
|
||||
----------------------
|
||||
|
||||
@ -10,7 +10,6 @@ namespace yii\web;
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use DOMException;
|
||||
use DOMText;
|
||||
use yii\base\Arrayable;
|
||||
use yii\base\Component;
|
||||
use yii\helpers\StringHelper;
|
||||
@ -38,7 +37,10 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
*/
|
||||
public $encoding;
|
||||
/**
|
||||
* @var string the name of the root element. If set to false, null or is empty then no root tag should be added.
|
||||
* @var string|string[]|false the name of the root element. If set to false, null or is empty then no root tag
|
||||
* should be added.
|
||||
*
|
||||
* Since 2.0.43 URI namespace could be specified by passing `[namespace, tag name]` array.
|
||||
*/
|
||||
public $rootTag = 'response';
|
||||
/**
|
||||
@ -52,14 +54,26 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
*/
|
||||
public $useTraversableAsArray = true;
|
||||
/**
|
||||
* @var bool if object tags should be added
|
||||
* @var bool if object class names should be used as tag names
|
||||
* @since 2.0.11
|
||||
*/
|
||||
public $useObjectTags = true;
|
||||
/**
|
||||
* @var bool if true, converts object tags to lowercase, `$useObjectTags` must be enabled
|
||||
* @since 2.0.43
|
||||
*/
|
||||
public $objectTagToLowercase = false;
|
||||
|
||||
/**
|
||||
* @var DOMDocument the XML document, serves as the root of the document tree
|
||||
* @since 2.0.43
|
||||
*/
|
||||
protected $dom;
|
||||
|
||||
|
||||
/**
|
||||
* Formats the specified response.
|
||||
*
|
||||
* @param Response $response the response to be formatted.
|
||||
*/
|
||||
public function format($response)
|
||||
@ -70,21 +84,27 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
}
|
||||
$response->getHeaders()->set('Content-Type', $this->contentType);
|
||||
if ($response->data !== null) {
|
||||
$dom = new DOMDocument($this->version, $charset);
|
||||
$this->dom = new DOMDocument($this->version, $charset);
|
||||
if (!empty($this->rootTag)) {
|
||||
$root = new DOMElement($this->rootTag);
|
||||
$dom->appendChild($root);
|
||||
if (is_array($this->rootTag)) {
|
||||
$root = $this->dom->createElementNS($this->rootTag[0], $this->rootTag[1]);
|
||||
} else {
|
||||
$root = $this->dom->createElement($this->rootTag);
|
||||
}
|
||||
$this->dom->appendChild($root);
|
||||
$this->buildXml($root, $response->data);
|
||||
} else {
|
||||
$this->buildXml($dom, $response->data);
|
||||
$this->buildXml($this->dom, $response->data);
|
||||
}
|
||||
$response->content = $dom->saveXML();
|
||||
$response->content = $this->dom->saveXML();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param DOMElement $element
|
||||
* @param mixed $data
|
||||
* Recursively adds data to XML document.
|
||||
*
|
||||
* @param DOMElement|DOMDocument $element current element
|
||||
* @param mixed $data content of the current element
|
||||
*/
|
||||
protected function buildXml($element, $data)
|
||||
{
|
||||
@ -95,18 +115,22 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
if (is_int($name) && is_object($value)) {
|
||||
$this->buildXml($element, $value);
|
||||
} elseif (is_array($value) || is_object($value)) {
|
||||
$child = new DOMElement($this->getValidXmlElementName($name));
|
||||
$child = $this->dom->createElement($this->getValidXmlElementName($name));
|
||||
$element->appendChild($child);
|
||||
$this->buildXml($child, $value);
|
||||
} else {
|
||||
$child = new DOMElement($this->getValidXmlElementName($name));
|
||||
$child = $this->dom->createElement($this->getValidXmlElementName($name));
|
||||
$child->appendChild($this->dom->createTextNode($this->formatScalarValue($value)));
|
||||
$element->appendChild($child);
|
||||
$child->appendChild(new DOMText($this->formatScalarValue($value)));
|
||||
}
|
||||
}
|
||||
} elseif (is_object($data)) {
|
||||
if ($this->useObjectTags) {
|
||||
$child = new DOMElement(StringHelper::basename(get_class($data)));
|
||||
$name = StringHelper::basename(get_class($data));
|
||||
if ($this->objectTagToLowercase) {
|
||||
$name = strtolower($name);
|
||||
}
|
||||
$child = $this->dom->createElement($name);
|
||||
$element->appendChild($child);
|
||||
} else {
|
||||
$child = $element;
|
||||
@ -121,7 +145,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
$this->buildXml($child, $array);
|
||||
}
|
||||
} else {
|
||||
$element->appendChild(new DOMText($this->formatScalarValue($data)));
|
||||
$element->appendChild($this->dom->createTextNode($this->formatScalarValue($data)));
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,7 +176,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
*
|
||||
* Falls back to [[itemTag]] otherwise.
|
||||
*
|
||||
* @param mixed $name
|
||||
* @param mixed $name the original name
|
||||
* @return string
|
||||
* @since 2.0.12
|
||||
*/
|
||||
@ -168,7 +192,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
/**
|
||||
* Checks if name is valid to be used in XML.
|
||||
*
|
||||
* @param mixed $name
|
||||
* @param mixed $name the name to test
|
||||
* @return bool
|
||||
* @see http://stackoverflow.com/questions/2519845/how-to-check-if-string-is-a-valid-xml-element-name/2519943#2519943
|
||||
* @since 2.0.12
|
||||
@ -176,8 +200,7 @@ class XmlResponseFormatter extends Component implements ResponseFormatterInterfa
|
||||
protected function isValidXmlName($name)
|
||||
{
|
||||
try {
|
||||
new DOMElement($name);
|
||||
return true;
|
||||
return $this->dom->createElement($name) !== false;
|
||||
} catch (DOMException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@ -46,6 +46,7 @@ class XmlResponseFormatterTest extends FormatterTest
|
||||
[true, "<response>true</response>\n"],
|
||||
[false, "<response>false</response>\n"],
|
||||
['<>', "<response><></response>\n"],
|
||||
['a&b', "<response>a&b</response>\n"],
|
||||
]);
|
||||
}
|
||||
|
||||
@ -58,6 +59,10 @@ class XmlResponseFormatterTest extends FormatterTest
|
||||
'a' => 1,
|
||||
'b' => 'abc',
|
||||
], "<response><a>1</a><b>abc</b></response>\n"],
|
||||
[
|
||||
['image:loc' => 'url'],
|
||||
"<response><image:loc>url</image:loc></response>\n"
|
||||
],
|
||||
[[
|
||||
1,
|
||||
'abc',
|
||||
@ -79,7 +84,7 @@ class XmlResponseFormatterTest extends FormatterTest
|
||||
'b:c' => 'b:c',
|
||||
'a b c' => 'a b c',
|
||||
'äøñ' => 'äøñ',
|
||||
], "<response><item>1</item><item>2015-06-18</item><item>b:c</item><item>a b c</item><äøñ>äøñ</äøñ></response>\n"],
|
||||
], "<response><item>1</item><item>2015-06-18</item><b:c>b:c</b:c><item>a b c</item><äøñ>äøñ</äøñ></response>\n"],
|
||||
]);
|
||||
}
|
||||
|
||||
@ -162,4 +167,13 @@ class XmlResponseFormatterTest extends FormatterTest
|
||||
$formatter->format($this->response);
|
||||
$this->assertEquals($this->xmlHead . "<response><id>123</id><title>abc</title></response>\n", $this->response->content);
|
||||
}
|
||||
|
||||
public function testObjectTagToLowercase()
|
||||
{
|
||||
$formatter = $this->getFormatterInstance(['objectTagToLowercase' => true]);
|
||||
|
||||
$this->response->data = new Post(123, 'abc');
|
||||
$formatter->format($this->response);
|
||||
$this->assertEquals($this->xmlHead . "<response><post><id>123</id><title>abc</title></post></response>\n", $this->response->content);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user