Install psalm as dev, sync scripts updates

This commit is contained in:
Clemens Schwaighofer
2023-03-09 16:27:10 +09:00
parent 6bec59e387
commit feba79a2e8
2099 changed files with 283333 additions and 32 deletions

View File

@@ -0,0 +1,44 @@
<?php
use PhpCsFixer\Config;
use PhpCsFixer\Finder;
$finder = Finder::create()
->in([
__DIR__ . '/src',
__DIR__ . '/tests',
])
->name('*.php')
->notName('*.blade.php')
->ignoreDotFiles(true)
->ignoreVCS(true);
$config = new Config();
return $config->setFinder($finder)
->setRules([
'@PSR2' => true,
'array_syntax' => ['syntax' => 'short'],
'ordered_imports' => ['sort_algorithm' => 'alpha'],
'no_unused_imports' => true,
'not_operator_with_successor_space' => true,
'trailing_comma_in_multiline' => ['elements' => ['arrays']],
'phpdoc_scalar' => true,
'unary_operator_spaces' => true,
'binary_operator_spaces' => true,
'blank_line_before_statement' => [
'statements' => ['break', 'continue', 'declare', 'return', 'throw', 'try'],
],
'phpdoc_single_line_var_spacing' => true,
'phpdoc_var_without_name' => true,
'class_attributes_separation' => [
'elements' => [
'method' => 'one',
],
],
'method_argument_space' => [
'on_multiline' => 'ensure_fully_multiline',
'keep_multiple_spaces_after_comma' => true,
],
'single_trait_insert_per_statement' => true,
])
->setRiskyAllowed(true);

198
vendor/spatie/array-to-xml/CHANGELOG.md vendored Executable file
View File

@@ -0,0 +1,198 @@
# Changelog
All notable changes to `array-to-xml` will be documented in this file
## 3.1.4 - 2022-11-24
### What's Changed
- PHP 8.2 support by @SuperDJ in https://github.com/spatie/array-to-xml/pull/194
- Added more types by @SuperDJ in https://github.com/spatie/array-to-xml/pull/195
### New Contributors
- @SuperDJ made their first contribution in https://github.com/spatie/array-to-xml/pull/194
**Full Changelog**: https://github.com/spatie/array-to-xml/compare/3.1.3...3.1.4
## 3.1.3 - 2022-05-08
## What's Changed
- Rewrite phpunit tests to pest by @otsch in https://github.com/spatie/array-to-xml/pull/183
- PHP 8.1 fix deprecated null parameters by @gigerIT in https://github.com/spatie/array-to-xml/pull/187
## New Contributors
- @otsch made their first contribution in https://github.com/spatie/array-to-xml/pull/183
- @gigerIT made their first contribution in https://github.com/spatie/array-to-xml/pull/187
**Full Changelog**: https://github.com/spatie/array-to-xml/compare/3.1.2...3.1.3
## 3.1.2 - 2022-03-03
## What's Changed
- Fix basic collection with namespace by @vaclavvanik in https://github.com/spatie/array-to-xml/pull/182
## New Contributors
- @vaclavvanik made their first contribution in https://github.com/spatie/array-to-xml/pull/182
**Full Changelog**: https://github.com/spatie/array-to-xml/compare/3.1.1...3.1.2
## 3.1.1 - 2022-02-11
## What's Changed
- Fix a typo in the result by @olsza in https://github.com/spatie/array-to-xml/pull/172
## New Contributors
- @olsza made their first contribution in https://github.com/spatie/array-to-xml/pull/172
**Full Changelog**: https://github.com/spatie/array-to-xml/compare/3.1.0...3.1.1
## 3.1.0 - 2021-09-12
- add support for processing instructions
## 3.0.1 - 2021-09-05
- allow null inside array to be converted to xml (#170)
## 3.0.0 - 2021-04-23
- require PHP 8+
- drop support for PHP 7.x
- convert syntax to PHP 8
## 2.16.0 - 2020-11-18
- add escapable colons in custom keys (#151)
## 2.15.1 - 2020-11-12
- add support for PHP 8
## 2.15.0 - 2020-10-29
- add $xmlStandalone as a new parameter (#148)
## 2.14.0 - 2020-09-14
- add support for dropping XML declaration (#145)
## 2.13.0 - 2020-08-24
- add support for custom keys (#140)
## 2.12.1 - 2020-06-17
- add XML prettification (#136)
## 2.11.2 - 2019-08-21
- fix XML structure when using numeric keys
## 2.11.1 - 2019-07-25
- do not interpret "0" as a non-empty value
## 2.11.0 - 2019-09-26
- drop support for PHP 7.1
## 2.10.0 - 2019-09-26
- add `setDomProperties`
## 2.9.0 - 2019-05-06
- add support for numeric keys
## 2.8.1 - 2019-03-15
- fix tests
- drop support for PHP 7.0
## 2.8.0 - 2018-11-29
- added support for mixed content
## 2.7.3 - 2018-10-30
- fix for `DomExeception`s being thrown
## 2.7.2 - 2018-09-17
- remove control characters
## 2.7.1 - 2018-02-02
- fix setting attributes
## 2.7.0 - 2017-09-07
- allow wrapping data in a CDATA section
## 2.6.1- 2017-08-29
- add fix for multiple empty/self-closing child elements
## 2.6.0 - 2017-08-25
- add support for naming a root element and adding properties to it
## 2.5.2 - 2017-08-03
- avoid pulling in the snapshot package on install
## 2.5.1 - 2017-05-30
- PHP 7 is now required
## 2.5.0 - 2017-05-22
- allow encoding and version to be set
## 2.4.0 - 2017-02-18
- attributes and value can be set in SimpleXMLElement style
## 2.3.0 - 2017-02-18
- attributes and value can be set in SimpleXMLElement style
## 2.2.1 - 2016-12-08
- fixed an error when there is a special character to the value set in _value
## 2.2.0 - 2016-06-04
- added `toDom` method
## 2.1.1 - 2016-02-23
- Fixed typo in the name of the `addSequentialNode`-function
## 2.1.0 - 2015-10-08
- Add ability to use attributes
## 2.0.0 - 2015-10-08
- Add support to collection arrays and dynamically XML convertion when keys are numeric
## 1.0.3 - 2015-10-03
- handle values with special characters
## 1.0.1 - 2015-03-18
- use DOMDocument for better validation
- added an option to opt out of the automatic space replacement
## 1.0.0 - 2015-03-17
- initial release

21
vendor/spatie/array-to-xml/LICENSE.md vendored Executable file
View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) Spatie bvba <info@spatie.be>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

536
vendor/spatie/array-to-xml/README.md vendored Executable file
View File

@@ -0,0 +1,536 @@
[<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)
[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)
![Tests](https://github.com/spatie/array-to-xml/workflows/Tests/badge.svg)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/array-to-xml.svg?style=flat-square)](https://packagist.org/packages/spatie/array-to-xml)
This package provides a very simple class to convert an array to an xml string.
## Support us
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/array-to-xml.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/array-to-xml)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Install
You can install this package via composer.
``` bash
composer require spatie/array-to-xml
```
## Usage
```php
use Spatie\ArrayToXml\ArrayToXml;
...
$array = [
'Good guy' => [
'name' => 'Luke Skywalker',
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
]
];
$result = ArrayToXml::convert($array);
```
After running this piece of code `$result` will contain:
```xml
<?xml version="1.0"?>
<root>
<Good_guy>
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Good_guy>
<Bad_guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
</root>
```
### Setting the name of the root element
Optionally you can set the name of the rootElement by passing it as the second argument. If you don't specify
this argument (or set it to an empty string) "root" will be used.
```
$result = ArrayToXml::convert($array, 'customrootname');
```
### Handling key names
By default all spaces in the key names of your array will be converted to underscores. If you want to opt out of
this behaviour you can set the third argument to false. We'll leave all keynames alone.
```
$result = ArrayToXml::convert($array, 'customrootname', false);
```
### Adding attributes
You can use a key named `_attributes` to add attributes to a node, and `_value` to specify the value.
```php
$array = [
'Good guy' => [
'_attributes' => ['attr1' => 'value'],
'name' => 'Luke Skywalker',
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
],
'The survivor' => [
'_attributes' => ['house'=>'Hogwarts'],
'_value' => 'Harry Potter'
]
];
$result = ArrayToXml::convert($array);
```
This code will result in:
```xml
<?xml version="1.0"?>
<root>
<Good_guy attr1="value">
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Good_guy>
<Bad_guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
<The_survivor house="Hogwarts">
Harry Potter
</The_survivor>
</root>
```
*Note, that the value of the `_value` field must be a string. [(More)](https://github.com/spatie/array-to-xml/issues/75#issuecomment-413726065)*
### Using reserved characters
It is also possible to wrap the value of a node into a CDATA section. This allows you to use reserved characters.
```php
$array = [
'Good guy' => [
'name' => [
'_cdata' => '<h1>Luke Skywalker</h1>'
],
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => '<h1>Sauron</h1>',
'weapon' => 'Evil Eye'
]
];
$result = ArrayToXml::convert($array);
```
This code will result in:
```xml
<?xml version="1.0"?>
<root>
<Good_guy>
<name><![CDATA[<h1>Luke Skywalker</h1>]]></name>
<weapon>Lightsaber</weapon>
</Good_guy>
<Bad_guy>
<name>&lt;h1&gt;Sauron&lt;/h1&gt;</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
</root>
```
If your input contains something that cannot be parsed a `DOMException` will be thrown.
### Customize the XML declaration
You could specify specific values in for:
- encoding as the fourth argument (string)
- version as the fifth argument (string)
- standalone as sixth argument (boolean)
```php
$result = ArrayToXml::convert($array, [], true, 'UTF-8', '1.1', [], true);
```
This will result in:
```xml
<?xml version="1.1" encoding="UTF-8" standalone="yes"?>
```
### Adding attributes to the root element
To add attributes to the root element provide an array with an `_attributes` key as the second argument.
The root element name can then be set using the `rootElementName` key.
```php
$result = ArrayToXml::convert($array, [
   'rootElementName' => 'helloyouluckypeople',
'_attributes' => [
'xmlns' => 'https://github.com/spatie/array-to-xml',
],
], true, 'UTF-8');
```
### Using a multi-dimensional array
Use a multi-dimensional array to create a collection of elements.
```php
$array = [
'Good guys' => [
'Guy' => [
['name' => 'Luke Skywalker', 'weapon' => 'Lightsaber'],
['name' => 'Captain America', 'weapon' => 'Shield'],
],
],
'Bad guys' => [
'Guy' => [
['name' => 'Sauron', 'weapon' => 'Evil Eye'],
['name' => 'Darth Vader', 'weapon' => 'Lightsaber'],
],
],
];
```
This will result in:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<helloyouluckypeople xmlns="https://github.com/spatie/array-to-xml">
<Good_guys>
<Guy>
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Guy>
<Guy>
<name>Captain America</name>
<weapon>Shield</weapon>
</Guy>
</Good_guys>
<Bad_guys>
<Guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Guy>
<Guy>
<name>Darth Vader</name>
<weapon>Lightsaber</weapon>
</Guy>
</Bad_guys>
</helloyouluckypeople>
```
### Handling numeric keys
The package can also can handle numeric keys:
```php
$array = [
100 => [
'name' => 'Vladimir',
'nickname' => 'greeflas',
],
200 => [
'name' => 'Marina',
'nickname' => 'estacet',
],
];
$result = ArrayToXml::convert(['__numeric' => $array]);
```
This will result in:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<numeric_100>
<name>Vladimir</name>
<nickname>greeflas</nickname>
</numeric_100>
<numeric_200>
<name>Marina</name>
<nickname>estacet</nickname>
</numeric_200>
</root>
```
You can change key prefix with setter method called `setNumericTagNamePrefix()`.
### Using custom keys
The package can also can handle custom keys:
```php
$array = [
'__custom:custom-key:1' => [
'name' => 'Vladimir',
'nickname' => 'greeflas',
],
'__custom:custom-key:2' => [
'name' => 'Marina',
'nickname' => 'estacet',
'tags' => [
'__custom:tag:1' => 'first-tag',
'__custom:tag:2' => 'second-tag',
]
],
];
$result = ArrayToXml::convert($array);
```
This will result in:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<custom-key>
<name>Vladimir</name>
<nickname>greeflas</nickname>
</custom-key>
<custom-key>
<name>Marina</name>
<nickname>estacet</nickname>
<tags>
<tag>first-tag</tag>
<tag>second-tag</tag>
</tags>
</custom-key>
</root>
```
A custom key contains three, colon-separated parts: "__custom:[custom-tag]:[unique-string]".
- "__custom"
- The key always starts with "__custom".
- [custom-tag]
- The string to be rendered as the XML tag.
- [unique-string]
- A unique string that avoids overwriting of duplicate keys in PHP arrays.
a colon character can be included within the custom-tag portion by escaping it with a backslash:
```php
$array = [
'__custom:ns\\:custom-key:1' => [
'name' => 'Vladimir',
'nickname' => 'greeflas',
],
'__custom:ns\\:custom-key:2' => [
'name' => 'Marina',
'nickname' => 'estacet',
'tags' => [
'__custom:ns\\:tag:1' => 'first-tag',
'__custom:ns\\:tag:2' => 'second-tag',
]
],
];
```
This will result in:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<ns:custom-key>
<name>Vladimir</name>
<nickname>greeflas</nickname>
</ns:custom-key>
<ns:custom-key>
<name>Marina</name>
<nickname>estacet</nickname>
<tags>
<ns:tag>first-tag</ns:tag>
<ns:tag>second-tag</ns:tag>
</tags>
</ns:custom-key>
</root>
```
### Setting DOMDocument properties
To set properties of the internal DOMDocument object just pass an array consisting of keys and values. For a full list of valid properties consult https://www.php.net/manual/en/class.domdocument.php.
You can use the constructor to set DOMDocument properties.
```php
$result = ArrayToXml::convert(
$array,
$rootElement,
$replaceSpacesByUnderScoresInKeyNames,
$xmlEncoding,
$xmlVersion,
['formatOutput' => true]
);
```
Alternatively you can use `setDomProperties`
```php
$arrayToXml = new ArrayToXml($array);
$arrayToXml->setDomProperties(['formatOutput' => true]);
$result = $arrayToXml->toXml();
```
### XML Prettification
Call `$arrayToXml->prettify()` method on ArrayToXml to set XML in pretty form.
Example:
```php
$array = [
'Good guy' => [
'name' => 'Luke Skywalker',
'weapon' => 'Lightsaber'
],
'Bad guy' => [
'name' => 'Sauron',
'weapon' => 'Evil Eye'
]
];
$arrayToXml = new ArrayToXml($array);
```
With prettification:
```php
$arrayToXml->prettify()->toXml();
```
will result in:
```xml
<?xml version="1.0"?>
<root>
<Good_guy>
<name>Luke Skywalker</name>
<weapon>Lightsaber</weapon>
</Good_guy>
<Bad_guy>
<name>Sauron</name>
<weapon>Evil Eye</weapon>
</Bad_guy>
</root>
```
Without prettification:
```php
$arrayToXml->toXml();
```
will result in:
```xml
<?xml version="1.0"?>
<root><Good_guy><name>Luke Skywalker</name><weapon>Lightsaber</weapon></Good_guy><Bad_guy><name>Sauron</name><weapon>Evil Eye</weapon></Bad_guy></root>
```
### Dropping XML declaration
Call `$arrayToXml->dropXmlDeclaration()` method on ArrayToXml object to omit default XML declaration on top of the generated XML.
Example:
```php
$root = [
'rootElementName' => 'soap:Envelope',
'_attributes' => [
'xmlns:soap' => 'http://www.w3.org/2003/05/soap-envelope/',
],
];
$array = [
'soap:Header' => [],
'soap:Body' => [
'soap:key' => 'soap:value',
],
];
$arrayToXml = new ArrayToXml($array, $root);
$result = $arrayToXml->dropXmlDeclaration()->toXml();
```
This will result in:
```xml
<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
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Postcardware
You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.
We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).
## Credits
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

46
vendor/spatie/array-to-xml/composer.json vendored Executable file
View File

@@ -0,0 +1,46 @@
{
"name": "spatie/array-to-xml",
"description": "Convert an array to xml",
"keywords": [
"convert",
"array",
"xml"
],
"homepage": "https://github.com/spatie/array-to-xml",
"license": "MIT",
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"homepage": "https://freek.dev",
"role": "Developer"
}
],
"require": {
"php" : "^8.0",
"ext-dom": "*"
},
"require-dev": {
"mockery/mockery": "^1.2",
"pestphp/pest": "^1.21",
"spatie/pest-plugin-snapshots": "^1.1"
},
"autoload": {
"psr-4": {
"Spatie\\ArrayToXml\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Spatie\\ArrayToXml\\Test\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/pest"
},
"config": {
"allow-plugins": {
"pestphp/pest-plugin": true
}
}
}

View File

@@ -0,0 +1,275 @@
<?php
namespace Spatie\ArrayToXml;
use DOMDocument;
use DOMElement;
use DOMException;
use Exception;
class ArrayToXml
{
protected DOMDocument $document;
protected bool $replaceSpacesByUnderScoresInKeyNames = true;
protected bool $addXmlDeclaration = true;
protected string $numericTagNamePrefix = 'numeric_';
public function __construct(
array $array,
string | array $rootElement = '',
bool $replaceSpacesByUnderScoresInKeyNames = true,
string | null $xmlEncoding = null,
string $xmlVersion = '1.0',
array $domProperties = [],
bool | null $xmlStandalone = null
) {
$this->document = new DOMDocument($xmlVersion, $xmlEncoding ?? '');
if (! is_null($xmlStandalone)) {
$this->document->xmlStandalone = $xmlStandalone;
}
if (! empty($domProperties)) {
$this->setDomProperties($domProperties);
}
$this->replaceSpacesByUnderScoresInKeyNames = $replaceSpacesByUnderScoresInKeyNames;
if (! empty($array) && $this->isArrayAllKeySequential($array)) {
throw new DOMException('Invalid Character Error');
}
$root = $this->createRootElement($rootElement);
$this->document->appendChild($root);
$this->convertElement($root, $array);
}
public function setNumericTagNamePrefix(string $prefix): void
{
$this->numericTagNamePrefix = $prefix;
}
public static function convert(
array $array,
$rootElement = '',
bool $replaceSpacesByUnderScoresInKeyNames = true,
string $xmlEncoding = null,
string $xmlVersion = '1.0',
array $domProperties = [],
bool $xmlStandalone = null
): string {
$converter = new static(
$array,
$rootElement,
$replaceSpacesByUnderScoresInKeyNames,
$xmlEncoding,
$xmlVersion,
$domProperties,
$xmlStandalone
);
return $converter->toXml();
}
public function toXml(): string
{
return $this->addXmlDeclaration
? $this->document->saveXML()
: $this->document->saveXml($this->document->documentElement);
}
public function toDom(): DOMDocument
{
return $this->document;
}
protected function ensureValidDomProperties(array $domProperties): void
{
foreach ($domProperties as $key => $value) {
if (! property_exists($this->document, $key)) {
throw new Exception("{$key} is not a valid property of DOMDocument");
}
}
}
public function setDomProperties(array $domProperties): self
{
$this->ensureValidDomProperties($domProperties);
foreach ($domProperties as $key => $value) {
$this->document->{$key} = $value;
}
return $this;
}
public function prettify(): self
{
$this->document->preserveWhiteSpace = false;
$this->document->formatOutput = true;
return $this;
}
public function dropXmlDeclaration(): self
{
$this->addXmlDeclaration = false;
return $this;
}
public function addProcessingInstruction(string $target, string $data): self
{
$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;
}
protected function convertElement(DOMElement $element, mixed $value): void
{
$sequential = $this->isArrayAllKeySequential($value);
if (! is_array($value)) {
$value = htmlspecialchars($value ?? '');
$value = $this->removeControlCharacters($value);
$element->nodeValue = $value;
return;
}
foreach ($value as $key => $data) {
if (! $sequential) {
if (($key === '_attributes') || ($key === '@attributes')) {
$this->addAttributes($element, $data);
} elseif ((($key === '_value') || ($key === '@value')) && is_string($data)) {
$element->nodeValue = htmlspecialchars($data);
} elseif ((($key === '_cdata') || ($key === '@cdata')) && is_string($data)) {
$element->appendChild($this->document->createCDATASection($data));
} elseif ((($key === '_mixed') || ($key === '@mixed')) && is_string($data)) {
$fragment = $this->document->createDocumentFragment();
$fragment->appendXML($data);
$element->appendChild($fragment);
} elseif ($key === '__numeric') {
$this->addNumericNode($element, $data);
} elseif (str_starts_with($key, '__custom:')) {
$this->addNode($element, str_replace('\:', ':', preg_split('/(?<!\\\):/', $key)[1]), $data);
} else {
$this->addNode($element, $key, $data);
}
} elseif (is_array($data)) {
$this->addCollectionNode($element, $data);
} else {
$this->addSequentialNode($element, $data);
}
}
}
protected function addNumericNode(DOMElement $element, mixed $value): void
{
foreach ($value as $key => $item) {
$this->convertElement($element, [$this->numericTagNamePrefix.$key => $item]);
}
}
protected function addNode(DOMElement $element, string $key, mixed $value): void
{
if ($this->replaceSpacesByUnderScoresInKeyNames) {
$key = str_replace(' ', '_', $key);
}
$child = $this->document->createElement($key);
$element->appendChild($child);
$this->convertElement($child, $value);
}
protected function addCollectionNode(DOMElement $element, mixed $value): void
{
if ($element->childNodes->length === 0 && $element->attributes->length === 0) {
$this->convertElement($element, $value);
return;
}
$child = $this->document->createElement($element->tagName);
$element->parentNode->appendChild($child);
$this->convertElement($child, $value);
}
protected function addSequentialNode(DOMElement $element, mixed $value): void
{
if (empty($element->nodeValue) && ! is_numeric($element->nodeValue)) {
$element->nodeValue = htmlspecialchars($value);
return;
}
$child = $this->document->createElement($element->tagName);
$child->nodeValue = htmlspecialchars($value);
$element->parentNode->appendChild($child);
}
protected function isArrayAllKeySequential(array | string | null $value): bool
{
if (! is_array($value)) {
return false;
}
if (count($value) <= 0) {
return true;
}
if (\key($value) === '__numeric') {
return false;
}
return array_unique(array_map('is_int', array_keys($value))) === [true];
}
protected function addAttributes(DOMElement $element, array $data): void
{
foreach ($data as $attrKey => $attrVal) {
$element->setAttribute($attrKey, $attrVal ?? '');
}
}
protected function createRootElement(string|array $rootElement): DOMElement
{
if (is_string($rootElement)) {
$rootElementName = $rootElement ?: 'root';
return $this->document->createElement($rootElementName);
}
$rootElementName = $rootElement['rootElementName'] ?? 'root';
$element = $this->document->createElement($rootElementName);
foreach ($rootElement as $key => $value) {
if ($key !== '_attributes' && $key !== '@attributes') {
continue;
}
$this->addAttributes($element, $rootElement[$key]);
}
return $element;
}
protected function removeControlCharacters(string $value): string
{
return preg_replace('/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F]/', '', $value);
}
}