- How do I install mtownsend/collection-xml in a Laravel app?
- Run composer require mtownsend/collection-xml. Laravel package auto-discovery registers the service provider, so the toXml() macro is globally available. There is no config to publish, no migrations, and no worker or external service change. Pin an explicit version range before adding it to production.
- Which Laravel and PHP versions does mtownsend/collection-xml support?
- The package is Laravel-native and PHP 8 compatible, with no coupling beyond illuminate/support. It is compatible with Laravel 10, 11, and 12 in principle, but verify the Composer constraint against your lockfile instead of assuming. Pin a version range because a minor bump could change XML output formatting.
- What is mtownsend/collection-xml used for?
- It is best for bounded, one-directional XML work: turning arrays or collections into XML for RSS/Atom feeds, sitemaps, APIs, exports, and legacy integrations. It can also turn XML responses into collections for map, filter, and pluck chains. It is not a general-purpose XML layer or schema validation tool.
- How do I convert a Laravel collection or array to XML with mtownsend/collection-xml?
- Call the toXml() macro on a collection or array after installation. Nested data becomes nested XML elements. Treat the result as a one-directional export and verify the exact output against a real payload. Do not call it ad hoc across the codebase; wrap it behind a narrow encoder interface.
- Can mtownsend/collection-xml handle XML attributes and namespaces?
- Assume it cannot preserve attributes, namespaces, CDATA, mixed content, or repeated sibling semantics reliably. Array-to-XML conversion is lossy, and this class of converter breaks exactly there. If your SOAP, RSS, or legacy contract requires attributes or namespaces, this is likely the wrong tool.
- Is mtownsend/collection-xml safe for parsing untrusted XML?
- XML from partners or customer uploads needs entity expansion disabled and external entity loading off. Confirm the underlying simplexml_load_string call uses safe libxml flags and behaves predictably on DOCTYPE input. Do not assume safety; add a parsing test with hostile fixtures before production.
- Does mtownsend/collection-xml support large XML files or streaming?
- No. The full document loads into memory, so it is fine at KB to low-MB scale but the wrong tool for large exports. For big documents, use a streaming writer such as XMLWriter behind the same internal interface. Measure your largest document and p95 volume before choosing it.
- Can I round-trip XML through a collection and back without losing data?
- No. Array and XML are not isomorphic, so round-trip fidelity is not guaranteed. Attributes, namespaces, CDATA, mixed content, and the difference between repeated siblings and numeric-keyed arrays are common loss points. Spike one real payload in both directions and record exactly what is lost.
- How should I test mtownsend/collection-xml output in CI?
- Use golden-file tests that assert byte-level output for 8 to 10 representative payloads. Include the XML declaration, whitespace, and self-closing tag shape because partner systems notice those details. A dependency bump should fail CI on output drift rather than fail a live integration. Also test the XML-to-collection direction with real fixtures.
- Is mtownsend/collection-xml maintained, and what are the alternatives?
- It has 44 GitHub stars, zero dependents, and a single maintainer, so treat it as adopt with a pre-planned exit rather than adopt and forget. The code is small and MIT licensed, making vendoring into app/Support a legitimate fallback if maintenance becomes a liability. Alternatives include symfony/serializer, XMLWriter for streaming, or a hand-rolled SimpleXMLElement boundary. Keep call sites behind App\Xml\XmlEncoder and App\Xml\XmlDecoder interfaces so swapping is a one-class change.