selfphp/data-converter

Flexible data format converter library for PHP

Installs: 1

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 0

Open Issues: 0

pkg:composer/selfphp/data-converter

v1.1.0 2025-06-09 14:53 UTC

This package is auto-updated.

Last update: 2025-12-09 15:47:19 UTC


README

A lightweight and extensible PHP library to convert structured data between formats.
Currently supports: array โ†’ XML, XML โ†’ array and array โ†” JSON conversion.

๐Ÿš€ Features

  • โœ… Convert associative arrays to XML
  • โœ… Convert XML to associative arrays
  • โœ… Convert arrays to JSON (with optional flags)
  • โœ… Convert JSON strings to associative arrays
  • โœ… Custom root element name
  • โœ… Optional XML declaration
  • โœ… Null โ†’ xsi:nil conversion
  • โœ… Boolean โ†’ string normalization (true / false)
  • โœ… Recursive array handling
  • โœ… Clean and readable output (pretty print)
  • โœ… Invalid characters are automatically removed
  • โœ… Minimal and modern code (no dependencies)

๐Ÿ“ฆ Installation

composer require selfphp/data-converter

โœ… PHP 8.1 or higher required

โœจ Usage Example

Array to XML

use Selfphp\DataConverter\Format\ArrayToXmlConverter;

$array = [
    'user' => [
        'name' => 'Alice',
        'active' => true,
        'note' => null
    ]
];

$xml = ArrayToXmlConverter::convertArray(
    $array,
    rootElement: 'response',
    addXmlDeclaration: true,
    convertNullToXsiNil: true,
    convertBoolToString: true
);

echo $xml;

XML to Array

use Selfphp\DataConverter\Format\XmlToArrayConverter;

$xml = <<<XML
<user id="42" active="true">Alice</user>
XML;

$converter = new XmlToArrayConverter();
$array = $converter->convert($xml);

print_r($array);
// [
//     '@id' => '42',
//     '@active' => 'true',
//     '#text' => 'Alice'
// ]

Array to JSON

use Selfphp\DataConverter\Format\ArrayToJsonConverter;

$data = ['url' => 'https://example.com'];

$converter = (new ArrayToJsonConverter())
    ->withFlags(JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);

echo $converter->convert($data);

JSON to Array

use Selfphp\DataConverter\Format\JsonToArrayConverter;

$json = '{"name":"Alice","active":true}';

$converter = new JsonToArrayConverter();
$array = $converter->convert($json);

๐Ÿงช Tests

Run all PHPUnit tests:

vendor/bin/phpunit --testdox

๐Ÿ“ Project Structure

src/
โ””โ”€โ”€ Format/
    โ”œโ”€โ”€ ArrayToXmlConverter.php
    โ”œโ”€โ”€ XmlToArrayConverter.php
    โ”œโ”€โ”€ ArrayToJsonConverter.php
    โ””โ”€โ”€ JsonToArrayConverter.php

tests/
โ””โ”€โ”€ Format/
    โ”œโ”€โ”€ ArrayToXmlConverterTest.php
    โ”œโ”€โ”€ XmlToArrayConverterTest.php
    โ”œโ”€โ”€ ArrayToJsonConverterTest.php
    โ””โ”€โ”€ JsonToArrayConverterTest.php

โš ๏ธ Limitations and Edge Cases

While the XmlToArrayConverter is suitable for most real-world use cases, there are a few edge cases and known limitations to be aware of:

โŒ Mixed Content

XML nodes with both text and child elements (mixed content) are not fully preserved. For example:

<item>This is <b>bold</b> and normal text.</item>

Would result in:

['b' => 'bold'] // Text parts around <b> are not preserved

โš ๏ธ Empty Elements

Empty elements like <foo/> are interpreted as empty strings, not as null or empty arrays. If needed, you can post-process the result accordingly.

โš ๏ธ All values are strings

XML data types (numbers, booleans) are not automatically casted. For example:

<active>true</active>

Becomes:

['active' => 'true'] // not boolean true

โŒ XML Namespaces

Namespaces (e.g. xmlns or prefixed elements) are ignored and stripped automatically. Support for namespaces may be added in a future release.

If you encounter any of these scenarios in real-world data, feel free to contribute or open an issue ๐Ÿ™Œ

๐Ÿ›  Planned

  • XML โ†’ Array
  • JSON โ†” Array
  • JSON โ†” XML
  • CLI support (php convert input.json)
  • Stream support

๐Ÿ“„ License

MIT License โ€“ free for personal and commercial use.