Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

php xml parser

## Usage

use TiagoFrancaHelpersXmlXmlHandler;

$xmlContent = file_get_contents(__DIR__ . '/myFile.xml');
$data = (new XmlHandler())->toArray($xmlContent);
// $data = XmlHandler::toArray($xmlContent);
var_dump($data);

### Class here
<?php

namespace TiagoFrancaHelpersXml;

use SimpleXMLElement;
use BadMethodCallException;

class XmlHandler
{
    public const UNAVAILABLE_FUNCTIONS = [
        // 'toArray',
    ];

    public const AVAILABLE_FUNCTIONS = [
        'parse',
        'toJson',
        'toObject',
        'toArray',
    ];

    /**
     * xmlParse function
     *
     * @param string|null $xmlContent
     * @return SimpleXMLElement|null
     */
    public static function xmlParse(?string $xmlContent): ?SimpleXMLElement
    {
        if (!$xmlContent) {
            return null;
        }

        return new SimpleXMLElement($xmlContent);
    }

    /**
     * xmlToJson function
     *
     * @param string|SimpleXMLElement|null $xmlContent
     * @param integer $flags json_encode $flags
     * @param integer $depth json_encode $depth
     * @return string|null
     */
    public static function xmlToJson(
        string|SimpleXMLElement|null $xmlContent,
        int $flags = 0,
        int $depth = 512
    ): ?string {
        if (!$xmlContent) {
            return null;
        }

        $xmlContent = is_string($xmlContent) ? static::xmlParse($xmlContent) : $xmlContent;

        return json_encode($xmlContent, $flags, $depth) ?: null;
    }

    /**
     * function xmlToObject
     *
     * @param string|SimpleXMLElement|null $xmlContent
     * @return object|null
     */
    public static function xmlToObject(string|SimpleXMLElement|null $xmlContent): ?object
    {
        $xmlContent = static::xmlToJson($xmlContent ?: null);

        if (!$xmlContent) {
            return null;
        }

        return json_decode($xmlContent, false);
    }

    /**
     * function xmlToArray
     *
     * @param string|SimpleXMLElement|null $xmlContent
     * @return array|null
     */
    public static function xmlToArray(string|SimpleXMLElement|null $xmlContent): ?array
    {
        $xmlContent = static::xmlToJson($xmlContent ?: null);

        if (!$xmlContent) {
            return null;
        }

        return json_decode($xmlContent, true);
    }

    public function __call(string $method, array $arguments): mixed
    {
        if (
            !in_array($method, static::AVAILABLE_FUNCTIONS)
            ||
            in_array($method, static::UNAVAILABLE_FUNCTIONS)
        ) {
            throw new BadMethodCallException("Invalid method: ${method}");
        }

        $methodToCall = 'xml' . ucfirst($method);

        if (in_array($methodToCall, static::UNAVAILABLE_FUNCTIONS)) {
            throw new BadMethodCallException("Invalid method: ${method}");
        }

        return call_user_func_array(
            static::class . '::' . $methodToCall,
            $arguments
        );
    }

    public static function __callStatic(string $method, array $arguments)
    {
        $availableMethods = static::availableFunctions();

        if (!in_array($method, $availableMethods)) {
            throw new BadMethodCallException("Invalid method: ${method}");
        }

        return (new static())->__call($method, $arguments);
    }

    /**
     * function availableFunctions
     *
     * @return array|null
     */
    public static function availableFunctions(): ?array
    {
        $allMethods = array_values(
            array_unique(
                array_merge(
                    get_class_methods(static::class),
                    static::AVAILABLE_FUNCTIONS
                )
            )
        );

        return array_filter(
            $allMethods,
            fn ($functionName) => !in_array($functionName, static::UNAVAILABLE_FUNCTIONS)
        );
    }
}
Source by www.php.net #
 
PREVIOUS NEXT
Tagged: #php #xml #parser
ADD COMMENT
Topic
Name
9+4 =