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
Requires
- php: >=8.1
Requires (Dev)
- phpunit/phpunit: ^10.0
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:nilconversion - โ
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.