PHPackages                             carlos-veizaga/xml-flow - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Parsing &amp; Serialization](/categories/parsing)
4. /
5. carlos-veizaga/xml-flow

ActiveLibrary[Parsing &amp; Serialization](/categories/parsing)

carlos-veizaga/xml-flow
=======================

A fluent, agnostic XML builder with native support for xsi:nil and complex namespaces.

v1.1.0(1mo ago)07MITPHPPHP &gt;=8.2CI passing

Since Apr 11Pushed 1mo agoCompare

[ Source](https://github.com/carlosJCVC/xml-flow)[ Packagist](https://packagist.org/packages/carlos-veizaga/xml-flow)[ RSS](/packages/carlos-veizaga-xml-flow/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (1)Dependencies (2)Versions (3)Used By (0)

XmlFlow
=======

[](#xmlflow)

A fluent PHP library for building well-formed XML documents, validating them against XSD schemas, and signing them with RSA-SHA256 digital signatures — purpose-built for electronic invoicing systems and any integration that demands strict schema compliance.

---

Features
--------

[](#features)

- **Fluent builder** — chainable API from root declaration to final XML string
- **Native `xsi:nil` support** — emit `` for absent but schema-required fields
- **Root element attributes** — set any attribute on the root element (e.g. `xsi:noNamespaceSchemaLocation`) without string manipulation
- **Multi-driver architecture** — swap rendering backends without touching application code
- **XSD validation** — validate against any schema with a structured error report
- **XMLDSig signing** — enveloped RSA-SHA256 signatures via a dedicated, decoupled `XmlSigner`
- **Sequential array collections** — arrays of items automatically repeat the parent tag
- **Precision-safe decimal formatting** — `asDecimal(n)` serializes strings without float conversion; `transform()` lets you bring your own arithmetic (BCMath, GMP, etc.)

---

Requirements
------------

[](#requirements)

- PHP 8.2+
- Extensions: `ext-dom`, `ext-xmlwriter`, `ext-openssl`

---

Installation
------------

[](#installation)

```
composer require carlos-veizaga/xml-flow
```

---

Basic usage
-----------

[](#basic-usage)

```
use CarlosVeizaga\XmlFlow\XmlFlow;
use CarlosVeizaga\XmlFlow\Node;

$xml = XmlFlow::create('invoice')
    ->namespaces(['xsi' => 'http://www.w3.org/2001/XMLSchema-instance'])
    ->body([
        'documentId'   => 1234567,
        'businessName' => 'ACME Inc.',
        'line'         => [
            ['description' => 'Laptop', 'quantity' => 1, 'unitPrice' => 4500],
            ['description' => 'Mouse',  'quantity' => 2, 'unitPrice' => 120],
        ],
    ])
    ->toXml();
```

Output:

```

  1234567
  ACME Inc.

    Laptop
    1
    4500

    Mouse
    2
    120

```

---

Power features
--------------

[](#power-features)

### Root element attributes — `->attributes()`

[](#root-element-attributes---attributes)

Some schemas require attributes on the root element that are not namespace declarations — for example `xsi:noNamespaceSchemaLocation`. Use `->attributes()` to set them directly on the builder:

```
$xml = XmlFlow::create('invoice')
    ->namespaces(['xsi' => 'http://www.w3.org/2001/XMLSchema-instance'])
    ->attributes(['xsi:noNamespaceSchemaLocation' => 'invoice.xsd'])
    ->body([...])
    ->toXml();
```

```

  ...

```

Multiple calls to `->attributes()` are merged, consistent with `->namespaces()`.

---

### Null fields — `Node::nil()`

[](#null-fields--nodenil)

Some XSD schemas require absent fields to appear explicitly with `xsi:nil="true"` rather than being omitted. XmlFlow handles this natively.

```
use CarlosVeizaga\XmlFlow\Node;

$xml = XmlFlow::create('invoice')
    ->namespaces(['xsi' => 'http://www.w3.org/2001/XMLSchema-instance'])
    ->body([
        'documentId' => 1234567,
        'discount'   => Node::nil('discount'),   //
        'reference'  => Node::nil('reference'),  //
    ])
    ->toXml();
```

> **Note:** The `xsi` namespace must be declared via `->namespaces()`. XmlFlow does not inject it automatically — neither driver does.

Elements with custom attributes and text content use `Node::create()`:

```
Node::create('unit')
    ->value('KGM')
    ->attributes(['code' => '58']);
// KGM
```

---

### Decimal precision — `asDecimal()` and `transform()`

[](#decimal-precision--asdecimal-and-transform)

PHP silently drops decimal places when casting numbers to strings — `(string) 150.0` produces `'150'`, not `'150.00'`. If your XSD field is `xs:decimal`, this causes validation failures. `asDecimal()` fixes it:

```
Node::create('unitPrice')->value(150.0)->asDecimal(2)   // 150.00
Node::create('taxRate')->value(0.13)->asDecimal(4)      // 0.1300
Node::create('total')->value(1234.5)->asDecimal(2)      // 1234.50
```

`value()` accepts `string`, `int`, or `float`. The precision defaults to `2` if omitted.

**String values — no float conversion**

When `value()` receives a string, `asDecimal()` normalizes decimal places via string operations — no `(float)` cast. This is safe for values that exceed IEEE 754 double precision (e.g. amounts from a BCMath-based SDK):

```
// Pads missing decimals
Node::create('total')->value('9999999.9')->asDecimal(2)    // '9999999.90'

// Truncates extra decimals (caller owns rounding)
Node::create('total')->value('9999999.999')->asDecimal(2)  // '9999999.99'

// Preserves precision beyond float range
Node::create('total')->value('1234567890123456.78')->asDecimal(2)  // '1234567890123456.78'
```

**Custom arithmetic — `transform(callable)`**

For rounding, BCMath, GMP, or any other arithmetic strategy, use `transform()`. The callable receives the raw value and returns the final string. It takes full precedence over `asDecimal()`.

```
// BCMath round-half-up (consumer provides the logic, library stays dependency-free)
Node::create('total')
    ->value($this->totalPayable)
    ->transform(fn($v) => bcadd($v, '0.005', 2));
```

```
// Any other custom formatter
Node::create('rate')
    ->value($rate)
    ->transform(fn($v) => MyArithmetic::round($v, 4));
```

---

### XSD validation

[](#xsd-validation)

```
use CarlosVeizaga\XmlFlow\Exceptions\XsdValidationException;

try {
    $xml = XmlFlow::create('invoice')
        ->namespaces([...])
        ->body([...])
        ->validate('/schemas/invoice.xsd')
        ->toXml();
} catch (XsdValidationException $e) {
    foreach ($e->getErrors() as $error) {
        echo "[line {$error->line}] {$error->message}";
    }
}
```

`validate()` returns `$this`, so it composes naturally in the chain. It throws `XsdValidationException` with the raw `LibXMLError[]` list on failure.

---

### Digital signing — XMLDSig RSA-SHA256

[](#digital-signing--xmldsig-rsa-sha256)

```
use CarlosVeizaga\XmlFlow\Security\XmlSigner;
use CarlosVeizaga\XmlFlow\Exceptions\SignatureException;

$xml = XmlFlow::create('invoice')
    ->namespaces([...])
    ->body([...])
    ->toXml();

try {
    $signedXml = XmlSigner::create()->sign($xml, '/certs/company.p12', 'password');
} catch (SignatureException $e) {
    // certificate not found, wrong password, or corrupt P12
}
```

The signature is inserted as the **last child of the root element**, as required by enveloped XMLDSig. The `` node declares `xmlns:ds` explicitly to satisfy strict schema validators.

> **Note:** `XmlSigner` accepts any XML string — it is not coupled to `XmlFlow`. You can sign documents generated by other means.

---

### High-volume generation — `useStreamMode()`

[](#high-volume-generation--usestreammode)

For batch jobs generating thousands of documents, switch to the `StreamDriver`. It uses PHP's `XMLWriter` extension and keeps memory usage **O(1)** regardless of document size.

```
$xml = XmlFlow::create('invoice')
    ->useStreamMode()
    ->namespaces([...])
    ->body([...])
    ->toXml();
```

> **When to use each driver:**
>
> ScenarioDriverXMLDSig digital signing`DomDriver` (default) — signing requires DOM node accessXSD validation via `->validate()``DomDriver` (default)Batch / high-volume generation (no signing)`StreamDriver` via `->useStreamMode()`

Switching drivers is transparent — both implement `XmlDriverInterface` and produce semantically identical output.

---

Architecture
------------

[](#architecture)

XmlFlow follows SOLID principles throughout:

- **Strategy pattern** — `XmlDriverInterface` decouples the rendering backend from the builder. Implement it to add a custom driver without modifying any existing class.
- **Value Object** — `Node` is immutable by convention; it carries no identity, only data.
- **Decoupled signing** — `XmlSigner` depends on `robrichards/xmlseclibs` and is entirely separate from the builder. Applications that do not need signing do not pay any cost for it.

```
XmlFlow (builder)
  └── XmlDriverInterface
        ├── DomDriver    — DOMDocument, supports XMLDSig
        └── StreamDriver — XMLWriter, O(1) memory

XmlSigner (independent)
  └── SignerInterface
        └── XmlSigner — RSA-SHA256 enveloped signature

```

---

Running the tests
-----------------

[](#running-the-tests)

```
composer install
./vendor/bin/phpunit
```

The suite covers the builder, Node semantics, driver parity (DomDriver vs StreamDriver), and cryptographic signature verification.

---

License
-------

[](#license)

MIT © [Carlos Veizaga](https://github.com/carlosJCVC)

###  Health Score

40

—

FairBetter than 86% of packages

Maintenance94

Actively maintained with recent releases

Popularity5

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity47

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~27 days

Total

2

Last Release

32d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/1bf108adf015ee7e1254df68d057bf29b6ee75bc15b8a8aa66172a323641772a?d=identicon)[carlosJCVC](/maintainers/carlosJCVC)

---

Top Contributors

[![carlosJCVC](https://avatars.githubusercontent.com/u/16903487?v=4)](https://github.com/carlosJCVC "carlosJCVC (13 commits)")

---

Tags

xmlfluentxmldsigxsdinvoicingxml-builder

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/carlos-veizaga-xml-flow/health.svg)

```
[![Health](https://phpackages.com/badges/carlos-veizaga-xml-flow/health.svg)](https://phpackages.com/packages/carlos-veizaga-xml-flow)
```

###  Alternatives

[veewee/xml

XML without worries

1836.6M38](/packages/veewee-xml)[goetas-webservices/xsd2php

Convert XSD (XML Schema) definitions into PHP classes and JMS metadata

2411.7M43](/packages/goetas-webservices-xsd2php)[goetas-webservices/xsd2php-runtime

Convert XSD (XML Schema) definitions into PHP classes

4912.0M43](/packages/goetas-webservices-xsd2php-runtime)[goetas-webservices/xsd-reader

Read any XML Schema (XSD) programmatically with PHP

615.0M19](/packages/goetas-webservices-xsd-reader)[marcelxyz/php-xml-digital-signature

A PHP library for signing XML documents using digital signatures

7355.7k](/packages/marcelxyz-php-xml-digital-signature)[goetas-webservices/wsdl-reader

Pure PHP WSDL parser

10350.8k7](/packages/goetas-webservices-wsdl-reader)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
