承接 thesameson/php-data-contracts 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

thesameson/php-data-contracts

Composer 安装命令:

composer require thesameson/php-data-contracts

包简介

Extensible PHP library for defining and serializing Data Contracts. Supports PHP Attributes, custom resolvers, and runtime enrichment. Ships with Open Data Contract Standard (ODCS) support, extensible for custom standards.

README 文档

README

Extensible PHP library for defining and serializing Data Contracts using PHP Attributes or programmatically at runtime. Ships with Open Data Contract Standard (ODCS) support, extensible for custom standards.

Work in progress The API is stabilizing but may still change.

Requirements

  • PHP 8.2+

Installation

composer require thesameson/php-data-contracts

Quick Start

Annotate your classes with the package's attributes to export them as data contracts:

use DataContracts\Attribute\Contract;
use DataContracts\Attribute\Schema;
use DataContracts\Attribute\Field;
use DataContracts\ContractBuilder;

#[Contract('user-contract', version: '1.0.0', owner: 'data-team')]
#[Schema(name: 'Users', physicalName: 'users', physicalType: 'table')]
class User
{
    #[Field(description: 'Primary key', primaryKey: true)]
    public int $id;

    #[Field(description: 'User email', required: true, format: 'email', maxLength: 255)]
    public string $email;

    #[Field(logicalType: 'timestamp', required: true)]
    public string $createdAt;
}

$result = ContractBuilder::create('user-contract')
    ->addSource(User::class)
    ->toYaml('user-contract.yaml');

Advanced Usage

The builder supports runtime enrichment and custom resolvers, all chainable:

use DataContracts\Config\ConfigurationBuilder;
use DataContracts\ContractBuilder;
use DataContracts\Extractor\Metadata\RawFieldMetadata;
use DataContracts\Model\Definition\ContractDefinition;
use DataContracts\Model\Definition\FieldDefinition;
use DataContracts\Resolver\Field\FieldResolverInterface;
use DataContracts\Resolver\Field\FieldResolutionResult;
use DataContracts\Resolver\ResolutionContext;

// Configuration: strict validation, alphabetical field ordering
$config = (new ConfigurationBuilder())
    ->strict()
    ->sortFieldsAlphabetically()
    ->build();

$result = ContractBuilder::create('user-contract', config: $config)
    ->addSource(User::class)

    // Add fields at runtime without modifying the source class
    ->addField('Users', 'updatedAt', function (FieldDefinition $field) {
        $field->setLogicalType('timestamp')
            ->setRequired(true);
    })

    // Enrichment: modify the contract/schema/field definition after extraction
    ->enrichContract(function (ContractDefinition $contract) {
        $contract->setDescription('User accounts and profile data');
        return $contract;
    })

    // Custom resolver: hook into the extraction pipeline (priority 100+)
    // This example marks all "id" fields as "required"
    ->addFieldResolver(new class implements FieldResolverInterface {
        public function getPriority(): int { return 100; }
        public function supports(RawFieldMetadata $raw, ResolutionContext $ctx): bool
        {
            return $raw->name === 'id' || str_ends_with($raw->name, '_id');
        }
        public function resolve(
            RawFieldMetadata $raw,
            FieldDefinition $current,
            ResolutionContext $ctx,
        ): FieldResolutionResult {
            $current->setRequired(true);
            return FieldResolutionResult::continue($current);
        }
    })

    ->toYaml();

ODCS Support

The built-in ODCS implementation covers fundamental contract, schema, and field properties. For ODCS properties that aren't modeled internally (e.g., classification, criticalDataElement), use customProperties - they are spread as top-level keys in the output:

#[Field(
    logicalType: 'string',
    required: true,
    customProperties: [
        'classification' => 'sensitive',
        'criticalDataElement' => true,
    ],
)]
public string $email;

The same works programmatically via setCustomProperties() on any definition object, or at the schema/contract level through #[Schema(customProperties: [...])] and #[Contract(customProperties: [...])].

Key Concepts

Concept Description
Attributes #[Contract], #[Schema], #[Field] declare metadata directly on PHP classes and properties
Resolvers Transform raw extracted metadata into normalized definitions (e.g., physical -> logical type mapping)
Enrichment Runtime callbacks to add, modify, or remove fields/schemas after extraction
Standards Pluggable output standards. ODCS ships built-in, implement StandardInterface for custom ones
Serializers Output to YAML, JSON, or array or bring your own SerializerInterface

License

MIT

thesameson/php-data-contracts 适用场景与选型建议

thesameson/php-data-contracts 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 02 月 15 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「yaml」 「schema」 「attributes」 「data-contracts」 「odcs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 thesameson/php-data-contracts 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 thesameson/php-data-contracts 我们能提供哪些服务?
定制开发 / 二次开发

基于 thesameson/php-data-contracts 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 28
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-02-15