PHP code example of carlos-veizaga / xml-flow

1. Go to this page and download the library: Download carlos-veizaga/xml-flow library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

carlos-veizaga / xml-flow example snippets


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();

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

use CarlosVeizaga\XmlFlow\Node;

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

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

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

// 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'

// 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));

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}";
    }
}

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
}

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