mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 09:31:51 +01:00
composer.json - move tests-related classes to "autoload-dev" section (used for development purposes only and avoid polluting the autoloader in production)
This commit is contained in:
54
src/Utilities/Xml.php
Normal file
54
src/Utilities/Xml.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* (c) Meritoo.pl, http://www.meritoo.pl
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Meritoo\Common\Utilities;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMXPath;
|
||||
use SimpleXMLElement;
|
||||
|
||||
/**
|
||||
* Useful XML-related methods (only static functions)
|
||||
*
|
||||
* @author Krzysztof Niziol <krzysztof.niziol@meritoo.pl>
|
||||
* @copyright Meritoo.pl
|
||||
*/
|
||||
class Xml
|
||||
{
|
||||
/**
|
||||
* Merges nodes of given elements.
|
||||
* Returns merged instance of SimpleXMLElement.
|
||||
*
|
||||
* @param SimpleXMLElement $element1 First element to merge
|
||||
* @param SimpleXMLElement $element2 Second element to merge
|
||||
* @return SimpleXMLElement
|
||||
*/
|
||||
public static function mergeNodes(SimpleXMLElement $element1, SimpleXMLElement $element2)
|
||||
{
|
||||
$document1 = new DOMDocument();
|
||||
$document2 = new DOMDocument();
|
||||
|
||||
$document1->loadXML($element1->asXML());
|
||||
$document2->loadXML($element2->asXML());
|
||||
|
||||
$path = new DOMXPath($document2);
|
||||
$query = $path->query('/*/*');
|
||||
$nodesCount = $query->length;
|
||||
|
||||
if (0 == $nodesCount) {
|
||||
return $element1;
|
||||
}
|
||||
|
||||
for ($i = 0; $i < $nodesCount; ++$i) {
|
||||
$node = $document1->importNode($query->item($i), true);
|
||||
$document1->documentElement->appendChild($node);
|
||||
}
|
||||
|
||||
return simplexml_import_dom($document1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user