Formats
Serializer and Deserializer turn a document into a string and back, across three formats:
json, yaml and xml. Both resolve the right format handler by name internally, so the
same two classes are the only thing a consumer ever needs, regardless of format.
Supported Formats
| Format | Dependency | Notes |
|---|---|---|
json |
PHP built-in | Always available. |
yaml |
symfony/yaml |
Optional: see Optional Dependencies below. |
xml |
xterr/php-ubl |
The real UBL XML, checked by Validation. |
Serializing a Document
use Derafu\Ubl\Service\Serializer;
$serializer = new Serializer();
$yaml = $serializer->serialize($quotation, 'yaml');
$json = $serializer->serialize($quotation, 'json');
$xml = $serializer->serialize($quotation, 'xml');
The version of the document is the one of its class (see Versions).
Deserializing a Document
use Derafu\Ubl\Service\Deserializer;
$deserializer = new Deserializer();
$quotation = $deserializer->deserialize($yaml, 'yaml');
The third argument is an array of options:
| Option | Meaning |
|---|---|
version |
The UBL version of the content, '2.2' for example. Without it, the default version (2.1). See Versions. |
validate |
When true, the content is validated against the official schema of its version before it becomes a document. See Validation. |
class |
The document class the content has to be, Quotation::class for example. The type is always resolved from the content, so this is an assertion: a content of another type throws DeserializationException. |
use Derafu\Ubl\V22\Oasis\Doc\Quotation;
$quotation = $deserializer->deserialize($yaml, 'yaml', [
'version' => '2.2',
'class' => Quotation::class,
'validate' => true,
]);
An option that does not exist throws DeserializationException.
Self-Describing Content
Neither json nor yaml needs to be told what to deserialize into: the property of the
decoded content that names the document type does it, e.g. {"Quotation": {...}} or Quotation:
in a YAML file. xml works the same way through its own mechanism: the document type is read
from the XML root element (<Quotation xmlns="...:Quotation-2">).
The JSON and YAML Representation
JSON and YAML are the model-based JSON representation of the OASIS “UBL JSON Alternative Representation” (Committee Note 01, published for UBL 2.1, 2.2 and 2.3), and YAML is exactly the same structure. It is a non-normative note, but it is the only published JSON binding of UBL, and this package does not invent another one.
{
"_D": "urn:oasis:names:specification:ubl:schema:xsd:Quotation-2",
"_A": "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
"_B": "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
"Quotation": {
"ID": {"_": "Q-001"},
"IssueDate": {"_": "2026-01-15"},
"QuotedMonetaryTotal": {
"PayableAmount": {"_": 100, "currencyID": "EUR"}
},
"QuotationLine": [
{"ID": {"_": "1"}}
]
}
}
- The apex has the namespaces of the document (
_D), of the aggregate components (_A), of the basic components (_B) and, when there are extensions, of the extensions (_E), and the property named as the document. The namespaces are documentary: reading ignores them. - A value is always an object: its content under
_, its XML attributes next to it ({"_": 100, "currencyID": "EUR"}). - Cardinality: only what UBL allows more than once is an array.
- Types: the content of an
Amount,Quantity,MeasureandNumericis a JSON number, an indicator is a boolean, everything else is a string (dates too). Decimals are numbers, so"100.00"in XML comes back from JSON as"100": the same decimal. - Empty: an empty element (
<cbc:ID/>) is{"_": ""}and an empty aggregate is{}.
The same document, as YAML:
_D: 'urn:oasis:names:specification:ubl:schema:xsd:Quotation-2'
Quotation:
ID:
_: Q-001
QuotedMonetaryTotal:
PayableAmount:
_: 100
currencyID: EUR
The Array API
Every class also has toArray()/fromArray()/jsonSerialize(), used internally by the json
and yaml handlers and available directly to a consumer who wants a plain array with no
format involved:
$array = $quotation->toArray();
$quotation = Quotation::fromArray($array);
fromArray() reads that shape and only that one: any other throws DeserializationException.
Normalizer
Contract\NormalizerInterface (Service\Normalizer) is the one place where data in another
shape (the legacy-based form of the OASIS note, bare scalars where it asks for an object…)
can be brought to the shape fromArray() reads. The json and yaml handlers call it right
before fromArray(). The default one does nothing: it returns the data it is given. To accept
another shape, implement the interface and give it to the FormatHandlerFactory.
Extensions
UBL leaves the content of an extension (UBLExtensions/UBLExtension/ExtensionContent) free: XML
of any vocabulary. The OASIS note leaves it out of its scope on purpose (“trading partners must
agree on the significance”), and its schemas only ask for an object.
The extension is identified by its ExtensionURI, so the agreement on a vocabulary is a
Contract\ExtensionContentCodecInterface: it says whether it is the codec of an ExtensionURI
(supports()), turns the XML fragments of the extension into the JSON object of that vocabulary
(encode()) and back (decode()). The codecs are registered in Service\ExtensionContentCodecs
(with the tag derafu_ubl.extension_content_codec in Symfony).
An extension that no codec supports keeps its content as the XML it is, as strings under any:
"ExtensionContent": [{"any": ["<BusinessActivity xmlns=\"urn:example\">Software</BusinessActivity>"]}]
Nothing is lost that way, a digital signature included: the XML of an extension is kept as it was read and written as it was kept.
Optional Dependencies
symfony/yaml is not a hard dependency of this package: the yaml handler is only autoloaded
the first time a caller actually serializes or deserializes with 'yaml'. A consumer who never
touches that format never needs it installed; one who does gets PHP’s own “Class not found”
error if it is missing, with no extra exception type or class_exists() guard in between.