selfphp/data-converter
最新稳定版本:v1.1.0
Composer 安装命令:
composer require selfphp/data-converter
包简介
Flexible data format converter library for PHP
关键字:
README 文档
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.
统计信息
- 总下载量: 1
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-06-09