Download the PHP package magicsunday/xmlmapper without Composer
On this page you can find all versions of the php package magicsunday/xmlmapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package xmlmapper
XmlMapper: PHP Object to XML Mapping
Map strongly-typed PHP objects to XML using Symfony's PropertyInfo and TypeInfo components.
📌 Overview
XmlMapper is a PHP library that maps strongly-typed PHP objects (DTOs, value objects, entities) to XML using reflection and PHPDoc annotations. It leverages Symfony's PropertyInfo and TypeInfo components to derive each property's type and render a matching XML document.
| Key | Value |
|---|---|
| Package | magicsunday/xmlmapper |
| PHP | ^8.4 |
| Main API | MagicSunday\XmlEncoder |
| Output | XML string (string); false only if serialization itself fails |
| Errors | CircularReferenceException for a cyclic object graph, InvalidXmlValueException for a value XML cannot carry, UnmappableValueException for a value the encoder cannot map (false as the third constructor argument drops such a value silently instead), DOMException for an invalid element name |
❓ What is this?
XmlMapper takes a PHP object implementing MagicSunday\XmlSerializable and renders it as XML, including nested objects, scalar and object collections, and custom types. Property values are routed to elements, attributes, raw text nodes or CDATA sections via a small set of annotations, and property names can be converted on the fly (e.g. snake_case to camelCase).
🎯 Why does this exist?
Serializing domain objects to XML usually means hand-writing DOMDocument/XMLWriter boilerplate that drifts from the underlying model. XmlMapper derives the XML structure from the object's typed properties and a few annotations, so the output follows the PHP model, with explicit hooks (attributes, CDATA, node values, custom type closures) where you need to deviate.
🚀 Usage
Quick start
Annotate the classes you want to serialize and let them implement XmlSerializable:
Build an encoder and map an instance:
The name converter is optional; without it the raw class and property names are used verbatim. For collections, annotate the property with the phpDocumentor collection type so the value type can be resolved:
Property markers
Each marker is applied as a native PHP attribute (shown above).
| Marker | Effect |
|---|---|
XmlAttribute |
Render the value as an attribute of the surrounding element. |
XmlNodeValue |
Render the value as the raw text content of the surrounding element. |
XmlCDataSection |
Wrap the value in a <![CDATA[ … ]]> section (markup left intact). |
XmlIgnore |
Leave the property out of the XML; it is not read at all. |
Custom types
Register a closure to transform every value of a given class or builtin type (Money::class, bool, int, array, object, …) before it is written:
📚 Documentation
- API reference
- Recipes
- Manual instantiation — wiring the Symfony extractor and name converter
- Markers: attributes, node values, CDATA and ignored properties — native attribute syntax
- Custom types — transforming values with
addType() - Custom name converter — element naming
- Collections — scalar, object, nullable and union-typed collections
🛠️ Development
Prerequisites:
- PHP
^8.4 - Extensions:
dom,xml - Node.js (for the copy-paste detection gate, installed from the lockfile with
npm ci)
Install dependencies:
Run the mandatory quality gate:
ci:test includes:
- Linting (
phplint) - Unit tests (
phpunit) - Static analysis (
phpstan, max level) - Refactoring dry-run (
rector --dry-run) - Coding standards dry-run (
php-cs-fixer --dry-run) - Architecture layers (
deptrac, plus unassigned-class and layer-cycle checks) - Tool configs against the shared coding-standard templates (
check-consumer-config.php) - Copy-paste detection (
jscpd)
The tool configurations import the shared rulesets of
magicsunday/coding-standard, which also
delivers the whole QA toolchain as a single dev dependency.
🤝 Contributing
Contributions are welcome. Please run the full composer ci:test quality gate before submitting a pull request, and keep changes covered by tests.
All versions of xmlmapper with dependencies
ext-xml Version *
ext-dom Version *
symfony/property-info Version ^7.3 || ^8.0
symfony/type-info Version ^7.3 || ^8.0
doctrine/inflector Version ^2.0