xterr/php-ubl-generator
Composer 安装命令:
composer require xterr/php-ubl-generator
包简介
UBL 2.x XSD-to-PHP 8.2+ code generator — produces typed classes with XML mapping attributes from OASIS XSD schemas
README 文档
README
XSD-to-PHP code generator for UBL 2.x documents. Reads OASIS UBL schemas and emits typed PHP 8.2+ classes with XML mapping attributes, optional codelist enums from Genericode files, and configurable property bindings.
Features
- UBL 2.x Schemas — Supports versions 2.1, 2.2, 2.3, 2.4
- All UBL Namespaces — CBC, CAC, EXT, SBC, SIG, SAC with universal resolution
- Typed Classes —
final classwith private properties, typed getters, fluent setters - PHP 8 Attributes —
#[XmlRoot],#[XmlType],#[XmlElement],#[XmlAttribute],#[XmlValue] - Codelist Enums — Parse OASIS Genericode (
.gc) files intostring-backed PHP enums with#[CodelistMeta] - Union Type Bindings — Bind multiple codelist enums to a single property as PHP union types
- Setter Validation — Inline type checks, patterns, and enumeration validation
- Validator Attributes — Optional
symfony/validator#[Assert\*]attribute generation - Configurable — YAML config with namespace overrides, include/exclude filters, naming customization
- Bring Your Own Schemas — Download UBL schemas from OASIS and point the generator at them
Installation
composer require --dev xterr/php-ubl-generator
Generated classes depend on the runtime package:
composer require xterr/php-ubl
Requirements:
- PHP 8.2 or higher
ext-domext-libxmlext-mbstring
Quick Start
Generate classes from UBL schemas
# Dry-run (show what would be generated) php vendor/bin/php-ubl-generator ubl:generate --schema-dir=path/to/UBL-2.4/xsd # Generate files php vendor/bin/php-ubl-generator ubl:generate --schema-dir=path/to/UBL-2.4/xsd --force # With codelist enums php vendor/bin/php-ubl-generator ubl:generate \ --schema-dir=path/to/UBL-2.4/xsd \ --codelist-dir=path/to/codelists/gc \ --force # From YAML config php vendor/bin/php-ubl-generator ubl:generate --config=ubl-generator.yaml --force # Override namespace and output php vendor/bin/php-ubl-generator ubl:generate \ --schema-dir=path/to/UBL-2.4/xsd \ --namespace='App\Ubl' \ --output-dir=src/Ubl \ --force
Generated output structure
src/
├── Cbc/ # Leaf types (Amount, Code, Text, Identifier, ...)
├── Cac/ # Aggregate types (Party, Address, TaxTotal, ...)
├── Doc/ # Document roots (Invoice, CreditNote, ...)
├── Enum/ # XSD restriction enumerations
└── Codelist/ # Codelist enums from .gc files (when configured)
Configuration
Copy the default config and customize:
cp vendor/xterr/php-ubl-generator/resources/config/ubl-generator.yaml.dist ubl-generator.yaml
schema_version: '2.4' schema_dir: '/path/to/UBL-2.4/xsd' output_dir: 'src' namespace: 'App\UBL' namespaces: cbc: 'Cbc' cac: 'Cac' doc: 'Doc' enum: 'Enum' include: [] # Glob patterns to include (empty = all) exclude: [] # Glob patterns to exclude type_overrides: {} # XSD type → PHP type class_name_overrides: {} # XSD type name → PHP class name property_name_overrides: {} # XSD element name → PHP property name include_documentation: true generate_validation: true generate_validator_attributes: false include_generated_tag: true codelists: dir: ~ # Path to .gc files (null = disabled) namespace: 'Codelist' name_overrides: {} # listID → custom enum class name bindings: {} # XsdType.Element → listID or [listID1, listID2]
Codelist Support
Point the generator at a directory of OASIS Genericode (.gc) files to generate string-backed PHP enums:
codelists: dir: 'resources/codelists/gc' namespace: 'Codelist' name_overrides: 'http://publications.europa.eu/resource/authority/country': 'CountryCode'
Generated enum:
use Xterr\UBL\Xml\Mapping\CodelistMeta; #[CodelistMeta(listID: 'criterion-element-type', listAgencyID: 'OP', listVersionID: '4.1.0')] enum CriterionElementType: string { case QUESTION = 'QUESTION'; case REQUIREMENT = 'REQUIREMENT'; case CAPTION = 'CAPTION'; // ... }
Property Bindings
Bind codelist enums to specific XSD properties, replacing the default CBC Code type:
codelists: dir: 'resources/codelists/gc' bindings: # Single binding — property typed as one enum 'TenderingCriterionPropertyType.TypeCode': 'criterion-element-type' 'CountryType.IdentificationCode': 'http://publications.europa.eu/resource/authority/country' # Union binding — property accepts multiple enums 'TenderingCriterionPropertyType.ExpectedCode': - 'boolean-gui-control-type' - 'financial-ratio-type' - 'http://publications.europa.eu/resource/authority/occupation'
Single bindings produce:
private ?CriterionElementType $typeCode = null;
Union bindings produce PHP union types:
private BooleanGUIControl|FinancialRatio|OccupationCode|null $expectedCode = null; public function getExpectedCode(): BooleanGUIControl|FinancialRatio|OccupationCode|null { return $this->expectedCode; } public function setExpectedCode(BooleanGUIControl|FinancialRatio|OccupationCode|null $expectedCode = null): self { $this->expectedCode = $expectedCode; return $this; }
Namespace Handling
All standard UBL 2.x namespaces are supported with universal resolution:
| Namespace | Prefix | Output | Description |
|---|---|---|---|
| CommonBasicComponents | cbc |
Cbc/ |
Leaf types (value + attributes) |
| SignatureBasicComponents | sbc |
Cbc/ |
Signature leaf types (same pattern) |
| CommonExtensionComponents | ext |
Cbc/ or Cac/ |
Leaf types → Cbc/, aggregates → Cac/ |
| CommonAggregateComponents | cac |
Cac/ |
Aggregate types (child elements) |
| SignatureAggregateComponents | sac |
Cac/ |
Signature aggregate types |
| CommonSignatureComponents | sig |
Cac/ |
Signature components |
Types are classified structurally: complex types with simpleContent extensions become leaf classes in Cbc/, complex types with child element sequences become aggregate classes in Cac/.
CLI Options
| Option | Description |
|---|---|
--config, -c |
Path to YAML configuration file |
--schema-dir, -s |
Path to XSD schema directory |
--schema-version |
UBL schema version (2.1, 2.2, 2.3, 2.4) |
--output-dir, -o |
Output directory for generated classes |
--namespace |
Root PHP namespace |
--codelist-dir |
Path to directory containing .gc codelist files |
--force, -f |
Actually generate files (without this, dry-run only) |
Architecture
Two packages work together:
| Package | Role | Install |
|---|---|---|
| xterr/php-ubl-generator (this) | Dev-time code generator | composer require --dev |
| xterr/php-ubl | Runtime XML mapping & serialization | composer require |
The generator reads XSD schemas and Genericode files, then emits PHP classes that use attributes from xterr/php-ubl. At runtime, only the lightweight xterr/php-ubl package is needed for XML serialization and deserialization.
Development
# Install dependencies composer install # Run tests composer test # Run static analysis composer analyze
CI
Tests run on PHP 8.2, 8.3, and 8.4 via GitHub Actions. Tagged releases are automatically notified to Packagist.
License
MIT — Copyright (c) 2026 Ceana Razvan
xterr/php-ubl-generator 适用场景与选型建议
xterr/php-ubl-generator 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 43 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「xml」 「xsd」 「oasis」 「code-generator」 「procurement」 「ubl」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 xterr/php-ubl-generator 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 xterr/php-ubl-generator 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 xterr/php-ubl-generator 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Load DOM document safety
A OASIS/XACML library for policy/permissioning
Convert XSD (XML Schema) definitions into PHP classes
XSD schema validation constraint for PHPUnit
Added a method to Laravel response for handling xml response and also converting Eloquent return to XML.
Convert PHP to XML and viceversa using XML Schema definition as encoding style
统计信息
- 总下载量: 43
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 38
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-10