/** * Converts an object to an XML string. The root element name is passed in as * a parameter, and each field of the object becomes a child element. Array * values are represented by multiples instances of that element. Methods on * the object are ignored. * * @param mixed $object the object to serialize * @param string $rootElementName the name of the root element * @param bool $useXsiType whether the xsi:type will be added into XML tags * when available * @return string the XML serialized string of the object */ public function ConvertObjectToXml($object, $rootElementName, $useXsiType) { $document = new DOMDocument('1.0', 'UTF-8'); $document->appendChild(self::ConvertObjectToElement($object, $rootElementName, $document, $useXsiType)); $xml = XmlUtils::GetXmlFromDom($document); if ($useXsiType) { // Insert links to definitions of the ns1 and xsi namespaces. $xml = preg_replace('/<ns1:mutate/', '<ns1:mutate ' . self::ADWORDS_NS_ATTR_PREFIX . $object::WSDL_NAMESPACE . '"', $xml); $xml = preg_replace('/<operations/', '<operations ' . self::XSI_ATTRIBUTE, $xml); } return $xml; }
/** * Generates the parameters to use for the download request. * @param mixed $reportDefinition the report definition, as an ID or object * @return array the parameters */ private static function GetParams($reportDefinition) { $params = array(); if (is_numeric($reportDefinition)) { $params['__rd'] = $reportDefinition; } else { if (is_object($reportDefinition) || is_array($reportDefinition)) { $document = XmlUtils::ConvertObjectToDocument($reportDefinition, 'reportDefinition'); $document->formatOutput = TRUE; $params['__rdxml'] = XmlUtils::GetXmlFromDom($document); } else { throw new ReportDownloadException('Invalid report definition type: ' . $reportDefinition); } } return $params; }
/** * Test converting an object to a DOM document. * @param string $expected the expected XML * @param Object $object the object to convert * @covers XmlUtils::ConvertObjectToDocument * @covers XmlUtils::ConvertObjectToElement * @covers XmlUtils::ConvertObjectToNodeValue * @dataProvider ObjectToXmlProvider */ public function testConvertObjectToDocument($object, $expected) { $document = XmlUtils::ConvertObjectToDocument($object, 'root'); $result = XmlUtils::GetXmlFromDom($document); $this->assertEquals($expected, $result); }
/** * @covers XmlUtils::GetXmlFromDom * @dataProvider ValidXmlProvider */ public function testGetXmlFromDom($xml) { $document = XmlUtils::GetDomFromXml($xml); $result = XmlUtils::GetXmlFromDom($document); $this->assertTrue(!empty($result)); }