Qiang Xue
12 years ago
5 changed files with 288 additions and 7 deletions
@ -0,0 +1,25 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\web; |
||||||
|
|
||||||
|
/** |
||||||
|
* ResponseFormatter specifies the interface needed to format data for a Web response object. |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
interface ResponseFormatter |
||||||
|
{ |
||||||
|
/** |
||||||
|
* Formats the given data for the response. |
||||||
|
* @param Response $response the response object that will accept the formatted result |
||||||
|
* @param mixed $data the data to be formatted |
||||||
|
* @return string the formatted result |
||||||
|
*/ |
||||||
|
function format($response, $data); |
||||||
|
} |
@ -0,0 +1,99 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yii\web; |
||||||
|
|
||||||
|
use DOMDocument; |
||||||
|
use DOMElement; |
||||||
|
use DOMText; |
||||||
|
use yii\base\Arrayable; |
||||||
|
use yii\base\Component; |
||||||
|
use yii\helpers\StringHelper; |
||||||
|
|
||||||
|
|
||||||
|
/** |
||||||
|
* XmlResponseFormatter formats the given data into an XML response content. |
||||||
|
* |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class XmlResponseFormatter extends Component implements ResponseFormatter |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var string the Content-Type header for the response |
||||||
|
*/ |
||||||
|
public $contentType = 'application/xml'; |
||||||
|
/** |
||||||
|
* @var string the XML version |
||||||
|
*/ |
||||||
|
public $version = '1.0'; |
||||||
|
/** |
||||||
|
* @var string the XML encoding. If not set, it will use the value of [[Response::charset]]. |
||||||
|
*/ |
||||||
|
public $encoding; |
||||||
|
/** |
||||||
|
* @var string the name of the root element. |
||||||
|
*/ |
||||||
|
public $rootTag = 'response'; |
||||||
|
/** |
||||||
|
* @var string the name of the elements that represent the array elements with numeric keys. |
||||||
|
*/ |
||||||
|
public $itemTag = 'item'; |
||||||
|
|
||||||
|
/** |
||||||
|
* Formats the given data for the response. |
||||||
|
* @param Response $response the response object that will accept the formatted result |
||||||
|
* @param mixed $data the data to be formatted |
||||||
|
* @return string the formatted result |
||||||
|
*/ |
||||||
|
public function format($response, $data) |
||||||
|
{ |
||||||
|
$response->getHeaders()->set('Content-Type', $this->contentType); |
||||||
|
$dom = new DOMDocument($this->version, $this->encoding === null ? $response->charset : $this->encoding); |
||||||
|
$root = new DOMElement($this->rootTag); |
||||||
|
$dom->appendChild($root); |
||||||
|
$this->buildXml($root, $data); |
||||||
|
return $dom->saveXML(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @param DOMElement $element |
||||||
|
* @param mixed $data |
||||||
|
*/ |
||||||
|
protected function buildXml($element, $data) |
||||||
|
{ |
||||||
|
if (is_object($data)) { |
||||||
|
$child = new DOMElement(StringHelper::basename(get_class($data))); |
||||||
|
$element->appendChild($child); |
||||||
|
if ($data instanceof Arrayable) { |
||||||
|
$this->buildXml($child, $data->toArray()); |
||||||
|
} else { |
||||||
|
$array = array(); |
||||||
|
foreach ($data as $name => $value) { |
||||||
|
$array[$name] = $value; |
||||||
|
} |
||||||
|
$this->buildXml($child, $array); |
||||||
|
} |
||||||
|
} elseif (is_array($data)) { |
||||||
|
foreach ($data as $name => $value) { |
||||||
|
if (is_int($name) && is_object($value)) { |
||||||
|
$this->buildXml($element, $value); |
||||||
|
} elseif (is_array($value) || is_object($value)) { |
||||||
|
$child = new DOMElement(is_int($name) ? $this->itemTag : $name); |
||||||
|
$element->appendChild($child); |
||||||
|
$this->buildXml($child, $value); |
||||||
|
} else { |
||||||
|
$child = new DOMElement(is_int($name) ? $this->itemTag : $name); |
||||||
|
$element->appendChild($child); |
||||||
|
$child->appendChild(new DOMText((string)$value)); |
||||||
|
} |
||||||
|
} |
||||||
|
} else { |
||||||
|
$element->appendChild(new DOMText((string)$data)); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,117 @@ |
|||||||
|
<?php |
||||||
|
/** |
||||||
|
* @link http://www.yiiframework.com/ |
||||||
|
* @copyright Copyright (c) 2008 Yii Software LLC |
||||||
|
* @license http://www.yiiframework.com/license/ |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace yiiunit\framework\web; |
||||||
|
|
||||||
|
use yii\base\Object; |
||||||
|
use yii\web\Response; |
||||||
|
use yii\web\XmlResponseFormatter; |
||||||
|
|
||||||
|
class Post extends Object |
||||||
|
{ |
||||||
|
public $id; |
||||||
|
public $title; |
||||||
|
|
||||||
|
public function __construct($id, $title) |
||||||
|
{ |
||||||
|
$this->id = $id; |
||||||
|
$this->title = $title; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* @author Qiang Xue <qiang.xue@gmail.com> |
||||||
|
* @since 2.0 |
||||||
|
*/ |
||||||
|
class XmlResponseFormatterTest extends \yiiunit\TestCase |
||||||
|
{ |
||||||
|
/** |
||||||
|
* @var Response |
||||||
|
*/ |
||||||
|
public $response; |
||||||
|
/** |
||||||
|
* @var XmlResponseFormatter |
||||||
|
*/ |
||||||
|
public $formatter; |
||||||
|
|
||||||
|
protected function setUp() |
||||||
|
{ |
||||||
|
$this->mockApplication(); |
||||||
|
$this->response = new Response; |
||||||
|
$this->formatter = new XmlResponseFormatter; |
||||||
|
} |
||||||
|
|
||||||
|
public function testFormatScalars() |
||||||
|
{ |
||||||
|
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||||||
|
|
||||||
|
$xml = $head . "<response></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, null)); |
||||||
|
|
||||||
|
$xml = $head . "<response>1</response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, 1)); |
||||||
|
|
||||||
|
$xml = $head . "<response>abc</response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, 'abc')); |
||||||
|
|
||||||
|
$xml = $head . "<response>1</response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, true)); |
||||||
|
} |
||||||
|
|
||||||
|
public function testFormatArrays() |
||||||
|
{ |
||||||
|
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||||||
|
|
||||||
|
$xml = $head . "<response/>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array())); |
||||||
|
|
||||||
|
$xml = $head . "<response><item>1</item><item>abc</item></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array(1, 'abc'))); |
||||||
|
|
||||||
|
$xml = $head . "<response><a>1</a><b>abc</b></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array( |
||||||
|
'a' => 1, |
||||||
|
'b' => 'abc', |
||||||
|
))); |
||||||
|
|
||||||
|
$xml = $head . "<response><item>1</item><item>abc</item><item><item>2</item><item>def</item></item><item>1</item></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array( |
||||||
|
1, |
||||||
|
'abc', |
||||||
|
array(2, 'def'), |
||||||
|
true, |
||||||
|
))); |
||||||
|
|
||||||
|
$xml = $head . "<response><a>1</a><b>abc</b><c><item>2</item><item>def</item></c><item>1</item></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array( |
||||||
|
'a' => 1, |
||||||
|
'b' => 'abc', |
||||||
|
'c' => array(2, 'def'), |
||||||
|
true, |
||||||
|
))); |
||||||
|
} |
||||||
|
|
||||||
|
public function testFormatObjects() |
||||||
|
{ |
||||||
|
$head = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; |
||||||
|
|
||||||
|
$xml = $head . "<response><Post><id>123</id><title>abc</title></Post></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, new Post(123, 'abc'))); |
||||||
|
|
||||||
|
$xml = $head . "<response><Post><id>123</id><title>abc</title></Post><Post><id>456</id><title>def</title></Post></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array( |
||||||
|
new Post(123, 'abc'), |
||||||
|
new Post(456, 'def'), |
||||||
|
))); |
||||||
|
|
||||||
|
$xml = $head . "<response><Post><id>123</id><title>abc</title></Post><a><Post><id>456</id><title>def</title></Post></a></response>\n"; |
||||||
|
$this->assertEquals($xml, $this->formatter->format($this->response, array( |
||||||
|
new Post(123, 'abc'), |
||||||
|
'a' => new Post(456, 'def'), |
||||||
|
))); |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue