/** * Format the response into the right content type. * * @access private * @param string $type * @return mixed */ private function __format($type) { $response = array('success' => $this->_success, 'data' => $this->_data); if (!empty($this->_code)) { $response['code'] = $this->_code; } switch (strtolower($type)) { case 'json': $format = TypeConverter::toJson($response); break; case 'xml': $format = TypeConverter::toXml($response); break; case 'html': case 'text': default: $format = (string) $this->_data; break; } return $format; }
public function testArrayOfMultipleComplexObjectsToXml() { // Create objects $object1 = $this->createComplexObject(1); $object2 = $this->createComplexObject(2); $object3 = $this->createComplexObject(3); // Create aray of objects $data = array($object1, $object2, $object3); // Convert to XML, with root of 'root' and tag wrappers of 'item' $actual_xml = TypeConverter::toXml($data); // Compose expected XML structure $expected_xml = $this->getXmlDeclaration() . '<root>' . '<item><id>1</id><name>Object 1</name><foo><bar>string</bar><baz>' . '<item><id>1.1</id><name>Object 1.1</name></item>' . '<item><id>1.2</id><name>Object 1.2</name></item>' . '<item><id>1.3</id><name>Object 1.3</name></item>' . '</baz></foo></article>' . '<item><id>2</id><name>Object 2</name><foo><bar>string</bar><baz>' . '<item><id>2.1</id><name>Object 2.1</name></item>' . '<item><id>2.2</id><name>Object 2.2</name></item>' . '<item><id>2.3</id><name>Object 2.3</name></item>' . '</baz></foo></article>' . '<item><id>3</id><name>Object 3</name><foo><bar>string</bar><baz>' . '<item><id>3.1</id><name>Object 3.1</name></item>' . '<item><id>3.2</id><name>Object 3.2</name></item>' . '<item><id>3.3</id><name>Object 3.3</name></item>' . '</baz></foo></item>' . '</root>'; // Now check with single string tag. // Convert to XML, with root of 'root' and tag wrappers of 'item' $actual_xml = TypeConverter::toXml($data, 'root', 'article'); // Compose expected XML structure $expected_xml = $this->getXmlDeclaration() . '<root>' . '<article><id>1</id><name>Object 1</name><foo><bar>string</bar><baz>' . '<article><id>1.1</id><name>Object 1.1</name></article>' . '<article><id>1.2</id><name>Object 1.2</name></article>' . '<article><id>1.3</id><name>Object 1.3</name></article>' . '</baz></foo></article>' . '<article><id>2</id><name>Object 2</name><foo><bar>string</bar><baz>' . '<article><id>2.1</id><name>Object 2.1</name></article>' . '<article><id>2.2</id><name>Object 2.2</name></article>' . '<article><id>2.3</id><name>Object 2.3</name></article>' . '</baz></foo></article>' . '<article><id>3</id><name>Object 3</name><foo><bar>string</bar><baz>' . '<article><id>3.1</id><name>Object 3.1</name></article>' . '<article><id>3.2</id><name>Object 3.2</name></article>' . '<article><id>3.3</id><name>Object 3.3</name></article>' . '</baz></foo></article>' . '</root>'; $this->assertXMLStringEqualsXmlString($expected_xml, $actual_xml); // Now check with array of tags. // Convert to XML, with root of 'root' and tag wrappers of 'item' $actual_xml = TypeConverter::toXml($data, 'root', array('outer', 'inner')); // Compose expected XML structure $expected_xml = $this->getXmlDeclaration() . '<root>' . '<outer><id>1</id><name>Object 1</name><foo><bar>string</bar><baz>' . '<inner><id>1.1</id><name>Object 1.1</name></inner>' . '<inner><id>1.2</id><name>Object 1.2</name></inner>' . '<inner><id>1.3</id><name>Object 1.3</name></inner>' . '</baz></foo></outer>' . '<outer><id>2</id><name>Object 2</name><foo><bar>string</bar><baz>' . '<inner><id>2.1</id><name>Object 2.1</name></inner>' . '<inner><id>2.2</id><name>Object 2.2</name></inner>' . '<inner><id>2.3</id><name>Object 2.3</name></inner>' . '</baz></foo></outer>' . '<outer><id>3</id><name>Object 3</name><foo><bar>string</bar><baz>' . '<inner><id>3.1</id><name>Object 3.1</name></inner>' . '<inner><id>3.2</id><name>Object 3.2</name></inner>' . '<inner><id>3.3</id><name>Object 3.3</name></inner>' . '</baz></foo></outer>' . '</root>'; $this->assertXMLStringEqualsXmlString($expected_xml, $actual_xml); }