You know the drill, Updates

This commit is contained in:
2023-05-01 19:51:20 -07:00
parent af6f76fbcb
commit 006e3d3314
105 changed files with 1725 additions and 1430 deletions

View File

@ -1,3 +1,6 @@
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/support-ukraine.svg?t=1" />](https://supportukrainenow.org)
# Convert an array to xml
[![Latest Version](https://img.shields.io/github/release/spatie/array-to-xml.svg?style=flat-square)](https://github.com/spatie/array-to-xml/releases)
@ -360,7 +363,7 @@ This will result in:
<nickname>estacet</nickname>
<tags>
<ns:tag>first-tag</ns:tag>
<tns:ag>second-tag</tns:ag>
<ns:tag>second-tag</ns:tag>
</tags>
</ns:custom-key>
</root>
@ -477,23 +480,43 @@ 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
vendor/bin/phpunit
```
### Changelog
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
## Security
## Security Vulnerabilities
If you discover any security related issues, please email freek@spatie.be instead of using the issue tracker.
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Postcardware

View File

@ -26,7 +26,7 @@ class ArrayToXml
$domProperties = [],
$xmlStandalone = null
) {
$this->document = new DOMDocument($xmlVersion, $xmlEncoding);
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');
if (! is_null($xmlStandalone)) {
$this->document->xmlStandalone = $xmlStandalone;
@ -38,7 +38,7 @@ class ArrayToXml
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
if ($this->isArrayAllKeySequential($array) && ! empty($array)) {
if (! empty($array) && $this->isArrayAllKeySequential($array)) {
throw new DOMException('Invalid Character Error');
}
@ -78,11 +78,9 @@ class ArrayToXml
public function toXml(): string
{
if ($this->addXmlDeclaration === false) {
return $this->document->saveXml($this->document->documentElement);
}
return $this->document->saveXML();
return $this->addXmlDeclaration
? $this->document->saveXML()
: $this->document->saveXml($this->document->documentElement);
}
public function toDom(): DOMDocument
@ -94,7 +92,7 @@ class ArrayToXml
{
foreach ($domProperties as $key => $value) {
if (! property_exists($this->document, $key)) {
throw new Exception($key.' is not a valid property of DOMDocument');
throw new Exception("{$key} is not a valid property of DOMDocument");
}
}
}
@ -125,12 +123,25 @@ class ArrayToXml
return $this;
}
public function addProcessingInstruction($target, $data)
{
$elements = $this->document->getElementsByTagName('*');
$rootElement = $elements->count() > 0 ? $elements->item(0) : null;
$processingInstruction = $this->document->createProcessingInstruction($target, $data);
$this->document->insertBefore($processingInstruction, $rootElement);
return $this;
}
private function convertElement(DOMElement $element, $value)
{
$sequential = $this->isArrayAllKeySequential($value);
if (! is_array($value)) {
$value = htmlspecialchars($value);
$value = htmlspecialchars($value ?? '');
$value = $this->removeControlCharacters($value);
@ -205,7 +216,7 @@ class ArrayToXml
return;
}
$child = new DOMElement($element->tagName);
$child = $this->document->createElement($element->tagName);
$child->nodeValue = htmlspecialchars($value);
$element->parentNode->appendChild($child);
}
@ -230,7 +241,7 @@ class ArrayToXml
protected function addAttributes(DOMElement $element, array $data)
{
foreach ($data as $attrKey => $attrVal) {
$element->setAttribute($attrKey, $attrVal);
$element->setAttribute($attrKey, $attrVal ?? '');
}
}