One Language for Every Business Document
Derafu UBL is a typed PHP model of UBL (Universal Business Language): one PHP class per document type and per building block defined by the official UBL XSD, for every version from 2.1 to 2.5, with services to read and write those documents in multiple formats and validate them against the real schemas.
It is deliberately generic: nothing in it knows about any one business (no fixed seller, no fixed tax rate, no fixed currency) and nothing about any e-invoicing profile (no Peppol rules, no country-specific mandatory fields or codelists). That kind of logic belongs in a package built on top of this one, for whichever network or country it targets.
Features
- One class per UBL type, per version: every document (
Quotation,Invoice,CreditNote, …) and every shared building block (Party,MonetaryTotal,TaxTotal, every*Linetype) as a plain, typed PHP class, generated from the official XSD of each UBL version (2.1, 2.2, 2.3, 2.4 and 2.5). See Versions. - Three wire formats: serialize and deserialize the same document as
json,yamlorxml, through one consistent API. JSON and YAML follow the OASIS “UBL JSON Alternative Representation”: there is no format of our own. See Formats. - Schema validation: validate a document against the official OASIS schemas bundled with the package, the XSD for XML and the JSON schemas for JSON and YAML. See Validation.
- Digital signature: sign a document and validate its signatures with the UBL Digital Signature Profile of OASIS, enveloped and detached, with co-signatures and countersignatures. See Signature.
- Array native: every class also has
toArray()/fromArray()/jsonSerialize(), for working with a document as plain data with no XML involved at all.
Installation
composer require derafu/ubl
Two packages are optional, and only needed for what they are for:
symfony/yaml, to read and write theyamlformat.opis/json-schema, to validate JSON and YAML against the official JSON schemas.
Applying the Bundled Patch to xterr/php-ubl
derafu/ubl wraps xterr/php-ubl for xml, and ships a fix for a few of its known defects
as a script that patches it directly in vendor/ (tools/patch-xterr-ubl.php): how dates
are read, and how the content of an extension (a digital signature, for example) is kept and
written back, byte by byte, with the namespaces it uses. That script is wired as derafu/ubl’s
own Composer post-install-cmd/post-update-cmd, so it reapplies automatically whenever
derafu/ubl itself is installed or updated as the root project.
Composer does not run a dependency’s own scripts for the project that requires it - only the
root project’s own composer.json scripts run. A project that depends on derafu/ubl must
add the same command to its own composer.json to actually get the patch; otherwise it never
runs, and xterr/php-ubl keeps its original, unpatched behavior silently.
post-install-cmd/post-update-cmd each accept either a single command or an array of them -
use the array form, so this is added alongside whatever else is already there instead of
replacing it:
{
"scripts": {
"post-install-cmd": [
"php vendor/derafu/ubl/tools/patch-xterr-ubl.php"
],
"post-update-cmd": [
"php vendor/derafu/ubl/tools/patch-xterr-ubl.php"
]
}
}
Run composer install (or composer update) once more after adding it, so the patch also
applies to the xterr/php-ubl already sitting in vendor/.
Basic Usage
Building a Document
Using an object-oriented API. The classes are the ones of one UBL version, V21 here (see
Versions):
use Derafu\Ubl\V21\Oasis\Cac\Item;
use Derafu\Ubl\V21\Oasis\Cac\LineItem;
use Derafu\Ubl\V21\Oasis\Cac\MonetaryTotal;
use Derafu\Ubl\V21\Oasis\Cac\Party;
use Derafu\Ubl\V21\Oasis\Cac\PartyName;
use Derafu\Ubl\V21\Oasis\Cac\Price;
use Derafu\Ubl\V21\Oasis\Cac\QuotationLine;
use Derafu\Ubl\V21\Oasis\Cac\SupplierParty;
use Derafu\Ubl\V21\Oasis\Cbc\Amount;
use Derafu\Ubl\V21\Oasis\Cbc\Identifier;
use Derafu\Ubl\V21\Oasis\Cbc\Text;
use Derafu\Ubl\V21\Oasis\Doc\Quotation;
$quotation = (new Quotation())
->setId((new Identifier())->setValue('Q-001'))
->setIssueDate(new DateTimeImmutable('2026-01-15'))
->setSellerSupplierParty(
(new SupplierParty())->setParty(
(new Party())->setPartyNames([
(new PartyName())->setName((new Text())->setValue('Acme Corp')),
])
)
)
->setQuotedMonetaryTotal(
(new MonetaryTotal())->setPayableAmount(
(new Amount())->setValue('100')->setCurrencyID('EUR')
)
)
->setQuotationLines([
(new QuotationLine())
->setId((new Identifier())->setValue('1'))
->setLineItem(
(new LineItem())
->setId((new Identifier())->setValue('1'))
->setPrice(
(new Price())->setPriceAmount(
(new Amount())->setValue('100')->setCurrencyID('EUR')
)
)
->setItem((new Item())->setName((new Text())->setValue('Widget')))
),
]);
Only ID, IssueDate, SellerSupplierParty, QuotedMonetaryTotal and one QuotationLine
(with LineItem.ID and LineItem.Item) are actually required: true in UBL - an Item with
no Name, and a line with no Price, are schema-valid too. The seller’s and item’s names, and
the price, are kept here only so the example reads like a real quotation, the same choice
behind this package’s own committed Quotation fixture.
Or using a plain PHP array, in the shape toArray() produces and json/yaml carry (see
Formats): every value is an object with its content under _ and its attributes next to
it, and only what UBL allows more than once is a list.
use Derafu\Ubl\V21\Oasis\Doc\Quotation;
$quotation = Quotation::fromArray([
'ID' => ['_' => 'Q-001'],
'IssueDate' => ['_' => '2026-01-15'],
'SellerSupplierParty' => [
'Party' => [
'PartyName' => [
['Name' => ['_' => 'Acme Corp']],
],
],
],
'QuotedMonetaryTotal' => [
'PayableAmount' => ['_' => 100, 'currencyID' => 'EUR'],
],
'QuotationLine' => [
[
'ID' => ['_' => '1'],
'LineItem' => [
'ID' => ['_' => '1'],
'Price' => [
'PriceAmount' => ['_' => 100, 'currencyID' => 'EUR'],
],
'Item' => [
'Name' => ['_' => 'Widget'],
],
],
],
],
]);
Serializing and Deserializing
use Derafu\Ubl\Service\Deserializer;
use Derafu\Ubl\Service\Serializer;
$serializer = new Serializer();
$deserializer = new Deserializer();
$yaml = $serializer->serialize($quotation, 'yaml');
$json = $serializer->serialize($quotation, 'json');
$xml = $serializer->serialize($quotation, 'xml');
$sameQuotation = $deserializer->deserialize($yaml, 'yaml');
Without more, a document is read with UBL 2.1. To read it with another version, or to validate it while reading it:
$quotation = $deserializer->deserialize($xml, 'xml', [
'version' => '2.2',
'validate' => true,
]);
See Formats for the exact shape of each format and Versions for the versions.
Validating Against the Official Schema
use Derafu\Ubl\Exception\ValidationException;
use Derafu\Ubl\Service\XmlSchemaValidator;
try {
(new XmlSchemaValidator())->validate($xml);
} catch (ValidationException $e) {
foreach ($e->getErrors() as $error) {
echo $error->message, "\n";
}
}
See Validation for how the document type is resolved, how to validate JSON and YAML, and what else can be inspected on a failure.
What This Package Is Not
- Not a profile. PEPPOL BIS, EN 16931, and any country’s CIUS (Core Invoice Usage Specification) restrict or extend this base syntax with their own mandatory fields, codelists and Schematron business rules. Those are separate packages that depend on this one, never the other way around.
- Not a mapping layer. Turning your own application’s data (prices, customers, products) into one of these documents is the job of your application, not of this package.