magebitcom/ucp-php-spec
Composer 安装命令:
composer require magebitcom/ucp-php-spec
包简介
Universal Commerce Protocol specification for PHP
README 文档
README
PHP interfaces for the Universal Commerce Protocol (UCP) specification. This package provides type-safe PHP interfaces automatically generated from the official UCP JSON Schema definitions.
About UCP
The Universal Commerce Protocol (UCP) is a standardized protocol for commerce operations, providing a unified interface for shopping, checkout, payment, and fulfillment operations across different platforms and systems.
What's Included
This package contains PHP interfaces for:
- Shopping - Cart, checkout, and order management
- Payment - Payment handlers, instruments, and credentials
- Fulfillment - Shipping, pickup, and delivery operations
- Discovery - Service discovery and capability negotiation
All interfaces are generated from the official UCP JSON Schema specifications located in the spec/ directory.
Installation
composer require magebitcom/ucp-php-spec
Usage
Two Interface Variants
This package generates two sets of interfaces:
Api- Immutable interfaces with getters only (read-only)MutableApi- Mutable interfaces with getters and setters (read-write)
Using Immutable Interfaces (Api)
The Api namespace contains immutable interfaces with only getter methods:
<?php use Magebit\UcpSpec\Api\Schemas\Shopping\OrderInterface; use Magebit\UcpSpec\Api\Schemas\Shopping\Types\OrderLineItemInterface; // Implement the immutable interface class MyOrder implements OrderInterface { public function getId(): string { return $this->orderId; } public function getLineItems(): array { return $this->lineItems; // OrderLineItemInterface[] } // ... implement other getter methods }
Using Mutable Interfaces (MutableApi)
The MutableApi namespace contains mutable interfaces with both getters and setters:
<?php use Magebit\UcpSpec\MutableApi\Schemas\Shopping\OrderInterface; use Magebit\UcpSpec\MutableApi\Schemas\Shopping\Types\OrderLineItemInterface; // Implement the mutable interface class MyMutableOrder implements OrderInterface { private string $orderId; private array $lineItems; public function getId(): string { return $this->orderId; } public function setId(string $id): self { $this->orderId = $id; return $this; } public function getLineItems(): array { return $this->lineItems; } public function setLineItems(array $lineItems): self { $this->lineItems = $lineItems; return $this; // Fluent interface for method chaining } // ... implement other getter and setter methods }
Available Namespaces
Immutable (Api):
Magebit\UcpSpec\Api\Schemas\Shopping- Shopping and order interfacesMagebit\UcpSpec\Api\Schemas\Shopping\Types- Common typesMagebit\UcpSpec\Api\Discovery- Discovery profile interfacesMagebit\UcpSpec\Api\Services- Service definition interfaces
Mutable (MutableApi):
Magebit\UcpSpec\MutableApi\Schemas\Shopping- Shopping and order interfacesMagebit\UcpSpec\MutableApi\Schemas\Shopping\Types- Common typesMagebit\UcpSpec\MutableApi\Discovery- Discovery profile interfacesMagebit\UcpSpec\MutableApi\Services- Service definition interfaces
Type Safety
All interfaces include:
- ✅ Full PHP 8.1+ type hints
- ✅ Nullable types for optional properties
- ✅ Union types where applicable
- ✅ PHPDoc with array item types
- ✅ Fluent interface for setters (return
self)
Immutable Interface Example (Api):
interface OrderInterface { /** * Unique order identifier. * * @return string */ function getId(): string; /** * Immutable line items — source of truth for what was ordered. * * @return OrderLineItemInterface[] */ function getLineItems(): array; /** * Append-only event log of money movements. * * @return AdjustmentInterface[]|null */ function getAdjustments(): array|null; }
Mutable Interface Example (MutableApi):
interface OrderInterface { function getId(): string; function setId(string $id): self; /** * @return OrderLineItemInterface[] */ function getLineItems(): array; /** * @param OrderLineItemInterface[] $lineItems * @return self */ function setLineItems(array $lineItems): self; /** * @return AdjustmentInterface[]|null */ function getAdjustments(): array|null; /** * @param AdjustmentInterface[]|null $adjustments * @return self */ function setAdjustments(?array $adjustments): self; }
Regenerating Interfaces
If you need to regenerate the interfaces from the JSON Schema files (e.g., after updating the spec):
Prerequisites
- PHP 8.1 or higher
- Composer dependencies installed
Generate Command
# Generate interfaces from spec files php generate.php # Clean and regenerate all interfaces php generate.php --clean
The generator automatically creates both Api (immutable) and MutableApi (mutable) namespaces in a single run.
Namespace Mapping
The generator preserves directory structure in namespaces and creates both immutable and mutable variants:
| Spec File | Immutable Interface (Api) | Mutable Interface (MutableApi) |
|---|---|---|
spec/schemas/shopping/order.json |
Magebit\UcpSpec\Api\Schemas\Shopping\OrderInterface |
Magebit\UcpSpec\MutableApi\Schemas\Shopping\OrderInterface |
spec/schemas/shopping/types/postal_address.json |
Magebit\UcpSpec\Api\Schemas\Shopping\Types\PostalAddressInterface |
Magebit\UcpSpec\MutableApi\Schemas\Shopping\Types\PostalAddressInterface |
spec/discovery/profile_schema.json |
Magebit\UcpSpec\Api\Discovery\UCPDiscoveryProfileInterface |
Magebit\UcpSpec\MutableApi\Discovery\UCPDiscoveryProfileInterface |
Type Mapping
JSON Schema types are mapped to PHP as follows:
| JSON Schema Type | PHP Type | PHPDoc Example |
|---|---|---|
string |
string |
@return string |
integer |
int |
@return int |
number |
float |
@return float |
boolean |
bool |
@return bool |
array |
array |
@return TypeInterface[] |
object |
Interface | @return InterfaceName |
null |
null |
@return TypeInterface|null |
oneOf/anyOf |
Union | @return string|int |
Contributing
When updating the UCP specification:
- Update JSON Schema files in
spec/ - Run
php generate.php --cleanto regenerate interfaces - Run
composer dump-autoloadto update autoloader - Test the generated interfaces
- Commit both spec files and generated interfaces
License
MIT License - see LICENSE.md for details.
Links
- UCP Specification: https://ucp.dev
- JSON Schema: https://json-schema.org
- Generator: Built with nette/php-generator
Have questions or need help? Contact us at info@magebit.com
magebitcom/ucp-php-spec 适用场景与选型建议
magebitcom/ucp-php-spec 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 27 次下载、GitHub Stars 达 1, 最近一次更新时间为 2026 年 01 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 magebitcom/ucp-php-spec 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 magebitcom/ucp-php-spec 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 27
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 20
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-19