Services

Available Services

Interface Class Purpose
Contract\SerializerInterface Service\Serializer Turn a document into a json/yaml/xml string.
Contract\DeserializerInterface Service\Deserializer Turn a json/yaml/xml string into a document.
Contract\XmlValidatorInterface Service\XmlSchemaValidator Validate a document’s XML against the official XSD.
Contract\JsonValidatorInterface Service\JsonSchemaValidator Validate the decoded JSON or YAML of a document against the official JSON schemas.
Contract\VersionRegistryInterface Service\VersionRegistry The UBL versions, and the default one.
Contract\VersionInterface Service\Version One UBL version: its model, its schemas.
Contract\NormalizerInterface Service\Normalizer Bring the decoded JSON or YAML to the shape fromArray() reads.
Contract\ExtensionContentCodecInterface Service\ExtensionContentCodecs The registry of the codecs of the extensions.
Contract\FormatHandlerFactoryInterface Service\FormatHandlerFactory Resolve a format name to the handler that implements it; register a custom one.
Contract\SignerInterface Service\Signer Sign a document with the UBL Digital Signature Profile, see Signature.
Contract\SignatureValidatorInterface Service\SignatureValidator Validate the signatures of a document and the conformance of the profile.

Every class is namespaced under Derafu\Ubl\. See Formats for Serializer/Deserializer usage, Validation for the validators, Signature for the signer and Versions for the registry.

Exceptions

Every exception this package throws extends Derafu\Ubl\Exception\UblException, itself a Derafu\Translation\Exception\Core\TranslatableException:

Exception Thrown when
UnsupportedFormatException A format name is not json, yaml or xml (or a custom registered one).
UnsupportedVersionException A UBL version is not one of the supported ones.
SerializationException A document cannot be turned into the requested format.
DeserializationException Content cannot be turned into a document (invalid JSON/YAML, an unknown document type, a shape fromArray() does not read, an option that does not exist…).
ValidationException A document does not validate against the schema. Carries the errors, see Validation.

Catching the base UblException catches any of them:

use Derafu\Ubl\Exception\UblException;

try {
    $document = (new Deserializer())->deserialize($content, 'yaml');
} catch (UblException $e) {
    // SerializationException, DeserializationException, ValidationException, ...
}

Dependency Injection

For an application using symfony/dependency-injection, resources/config/ubl-services.yaml wires every service above to its interface, ready to autowire:

imports:
    - { resource: '../vendor/derafu/ubl/resources/config/ubl-services.yaml' }

With that imported, a service can simply type-hint the interface it needs:

use Derafu\Ubl\Contract\SerializerInterface;

final class QuotationController
{
    public function __construct(
        private readonly SerializerInterface $serializer,
    ) {
    }
}

The file also defines the parameter derafu.ubl.version, the UBL version used when a call does not ask for one ('2.1'). Override it in your own services.yaml:

parameters:
    derafu.ubl.version: '2.5'

The signer and the validator of the signatures have their own file, ubl-signature-services.yaml, that needs the services of derafu/signature (see Signature).

A package that knows an extension registers its codec with the tag derafu_ubl.extension_content_codec (the tag has to be explicit: _instanceof does not propagate across imported files):

services:
    App\Ubl\MyExtensionCodec:
        tags: ['derafu_ubl.extension_content_codec']
On this page

Last updated on 09/11/2026 by Anonymous