Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/old/app/code/Soon/DataSync/Model/Data/Type/Xml.php
<?php
/**
 * @license http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 * @author Hervé Guétin <[email protected]> <@herveguetin>
 * @copyright Copyright (c) 2017 Agence Soon (http://www.agence-soon.fr)
 */

namespace Soon\DataSync\Model\Data\Type;

use BenTools\SimpleXMLExtended;

class Xml extends DataTypeAbstract
{
    /**
     * @var string
     */
    protected $rootNode = 'root';

    /**
     * @param string $string
     * @return array
     */
    public function parse(string $string): array
    {
        if (is_array($string)) {
            $xmlString = reset($string);
        } else {
            $xmlString = $string;
        }

        $xmlElement = simplexml_load_string($xmlString);

        $json = json_encode($xmlElement);

        return json_decode($json, true);
    }

    /**
     * @param array $array
     * @return string
     */
    public function convert(array $array): string
    {
        return $this->convertXML($array, $this->rootNode);
    }

    /**
     * The main function for converting to an XML document.
     * Pass in a multi dimensional array and this recursively loops through and builds up an XML document.
     *
     * @param array $data
     * @param string $rootNodeName - what you want the root node to be.
     * @param \SimpleXMLElement $xml - should only be used recursively
     * @return string XML
     *
     * @see https://snipt.net/robertbanh/php-array-to-xml/
     */
    private function convertXML(array $data, $rootNodeName = 'ResultSet', &$xml = null)
    {
        // turn off compatibility mode as simple xml throws a wobbly if you don't.
        if (ini_get('zend.ze1_compatibility_mode') == 1) {
            ini_set('zend.ze1_compatibility_mode', 0);
        }
        if (is_null($xml)) //$xml = simplexml_load_string( "" );
        {
            /** @var SimpleXMLExtended $xml */
            $xml = simplexml_load_string("<?xml version='1.0' encoding='utf-8'?><$rootNodeName />", "\\BenTools\\SimpleXMLExtended");
        }

        // loop through the data passed in.
        foreach ($data as $key => $value) {

            $numeric = false;

            // no numeric keys in our xml please!
            if (is_numeric($key)) {
                $numeric = 1;
                $key = $rootNodeName;
            }

            // delete any char not allowed in XML element names
            $key = preg_replace('/[^a-z0-9\-\_\.\:\@]/i', '', $key);

            // manage @attributes key in array in order to add attributes to the XML node
            if ($key == "@attributes") {
                foreach ($value as $keyAtt => $valueAtt) {
                    $xml->addAttribute($keyAtt, $valueAtt);
                }
            } elseif ($key == "@cdata") {
                foreach ($value as $keyAtt => $valueAtt) {
                    $xml->addCDataChild($keyAtt, $valueAtt);
                }
            } else {
                // if there is another array found recrusively call this function
                if (is_array($value)) {
                    $node = $this->isAssoc($value) || $numeric ? $xml->addChild($key) : $xml;
                    // recrusive call.
                    if ($numeric) {
                        $key = 'anon';
                    }
                    $this->convertXML($value, $key, $node);
                } else {
                    // add single node.
                    $xml->addChild($key, $value);
                }
            }
        }

        // pass back as XML
        return $xml->asXML();
    }

    /**
     * @param $value
     * @return bool
     */
    private function isAssoc($value)
    {
        return (is_array($value) && 0 !== count(array_diff_key($value, array_keys(array_keys($value)))));
    }
}

Spamworldpro Mini