/** * {@inheritdoc} */ public function dump(array $data) : string { // @codeCoverageIgnoreStart if (!class_exists('Spatie\\ArrayToXml\\ArrayToXml')) { throw new RuntimeException('Unable to dump XML, the ArrayToXml dumper is not installed.'); } // @codeCoverageIgnoreEnd try { return ArrayToXml::convert($data); } catch (DOMException $exception) { throw new DumpException($exception->getMessage()); } }
/** * Translate from json to xml * * @param PayloadInterface $payload */ public static function translate(PayloadInterface $payload) { switch ($payload->getContentType()) { case 'text/xml': case 'application/xml': return clone $payload; case 'application/json': $obj = new Payload('text/xml'); $data = json_decode($payload->getContent(), TRUE); static::insureValidKeys($data); $xml = ArrayToXml::convert($data); $obj->setContent($xml); return $obj; default: return static::failedTranslation($payload); } }
private function arrayToXML($array) { return \Spatie\ArrayToXml\ArrayToXml::convert($array); }
public function toXml() { return ArrayToXml::convert($this->result); }
/** * @test */ public function it_can_convert_attributes_to_xml() { $expectedXml = '<?xml version="1.0"?> <root><Good_guy nameType="1"><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>' . PHP_EOL; $withAttributes = $this->testArray; $withAttributes['Good guy']['_attributes'] = ['nameType' => 1]; $result = ArrayToXml::convert($withAttributes); $this->assertEquals($expectedXml, $result); }
/** * @test */ public function it_will_raise_an_exception_when_spaces_should_not_be_replaced_and_a_key_contains_a_space() { $this->setExpectedException('DOMException'); ArrayToXml::convert($this->testArray, '', false); }
/** * @test */ public function it_can_group_by_values_when_values_are_in_a_numeric_array() { $array = ['user' => ['foo', 'bar']]; $expectedXml = '<?xml version="1.0"?> <root><user>foo</user><user>bar</user></root>' . PHP_EOL; $result = ArrayToXml::convert($array); $this->assertEquals($expectedXml, $result); }