Skip to content

Commit

Permalink
Ability to add processing instructions (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
prestonholt authored Sep 12, 2021
1 parent 8cdbed8 commit aaa512f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,27 @@ This will result in:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope/"><soap:Header/><soap:Body><soap:key>soap:value</soap:key></soap:Body></soap:Envelope>
```

### Adding Processing Instructions

Call `$arrayToXml->addProcessingInstruction($target, $data)` method on ArrayToXml object to prepend a processing instruction before the root element.

Example:

```php

$arrayToXml = new ArrayToXml($array);
$arrayToXml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');
$result = $arrayToXml->toXml();
```

This will result in:

```xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="base.xsl"?>
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>
```

## Testing

```bash
Expand Down
11 changes: 11 additions & 0 deletions src/ArrayToXml.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ public function dropXmlDeclaration(): self
return $this;
}

public function addProcessingInstruction($target, $data)
{
$elements = $this->document->getElementsByTagName('*');

$rootElement = $elements->count() > 0 ? $elements->item(0) : null;

$this->document->insertBefore($this->document->createProcessingInstruction($target, $data), $rootElement);

return $this;
}

protected function convertElement(DOMElement $element, mixed $value): void
{
$sequential = $this->isArrayAllKeySequential($value);
Expand Down
12 changes: 11 additions & 1 deletion tests/ArrayToXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ public function it_can_drop_xml_declaration()

$this->assertMatchesSnapshot($arrayToXml->dropXmlDeclaration()->toXml());
}

/** @test */
public function it_can_convert_an_array_with_null_value_to_xml()
{
Expand All @@ -525,4 +525,14 @@ public function it_can_convert_an_array_with_null_value_to_xml()

$this->assertMatchesXmlSnapshot(ArrayToXml::convert($arr));
}

/** @test */
public function it_can_add_processing_instructions()
{
$arrayToXml = new ArrayToXml($this->testArray);

$arrayToXml->addProcessingInstruction('xml-stylesheet', 'type="text/xsl" href="base.xsl"');

$this->assertMatchesSnapshot($arrayToXml->toXml());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="base.xsl"?>
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>

0 comments on commit aaa512f

Please sign in to comment.