flyokai/data-mate 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

flyokai/data-mate

Composer 安装命令:

composer require flyokai/data-mate

包简介

Data/Dto helpers and base definitions

README 文档

README

User docs → README.md · Agent quick-ref → CLAUDE.md · Agent deep dive → AGENTS.md

The DTO foundation of the Flyokai framework — Solid/Draft/GreyData states, identity, enums, and collections.

data-mate defines the contract every Data Transfer Object in Flyokai obeys. It pairs the safety of constructor-validated immutables (Solid) with the flexibility of dynamic schemas (Draft, GreyData), and it standardises identity (HasId / HasAltId), enum helpers, and item collections so the rest of the framework can treat data uniformly.

It is built on top of cuyz/valinor for type-safe deserialization.

Features

  • Dto interfacetoArray(), toDbRow(), with(), cloneWith(), fromArgs()
  • Three data statesSolid (immutable, validated), Draft (flexible), GreyData (key/value bag)
  • Reflection-cached DtoTrait — automatic Valinor mapping with undefined-vs-null tracking
  • Identity systemHasId, HasAltId, HasIdDto with primary + alternative/composite keys
  • Enum helpersStringEnumTrait, IntEnumTrait, LCStringEnumTrait, UCStringEnumTrait
  • CollectionsItemJar interface and JarTrait for type-safe iterables
  • Built-in DTOsConfigJar, DdlTable, DdlTableColumn, DdlTableJar
  • ValidationAssertions, ValidationException, InvalidEntity factories

Installation

composer require flyokai/data-mate

Quick start

A Solid DTO (strict, immutable)

use Flyokai\DataMate\Dto;
use Flyokai\DataMate\DtoTrait;
use Flyokai\DataMate\Solid;

final class UserSolid implements Dto, Solid
{
    use DtoTrait;

    public function __construct(
        public readonly int $userId,
        public readonly string $username,
        public readonly string $email,
        public readonly ?string $firstName = null,
    ) {}
}

$user = UserSolid::fromArray([
    'userId' => 1,
    'username' => 'alice',
    'email' => 'alice@example.com',
]);

$row    = $user->toDbRow();             // ['user_id' => 1, 'username' => 'alice', ...]
$copy   = $user->cloneWith(email: 'a@b.com');
$loose  = $user->with(email: 'a@b.com'); // Valinor-mapped, accepts loose types

A Draft / Solid pairing

use Flyokai\DataMate\Draft;
use Flyokai\DataMate\DtoTrait;

final class User implements Dto, Draft
{
    use DtoTrait;
    protected static string $solidClassName = UserSolid::class;
    // …mutable, flexible properties for input/staging
}

Identity

use Flyokai\DataMate\HasId;
use Flyokai\DataMate\HasAltId;
use Flyokai\DataMate\HasIdDto;

final class UserSolid implements HasIdDto, Solid
{
    use DtoTrait;

    protected static string $type      = 'user';
    protected static string $idKey     = 'user_id';
    protected static array  $altIdKeys = ['username', 'email'];

    public function __construct(
        public readonly int $userId,
        public readonly string $username,
        public readonly string $email,
    ) {}
}

$user->id();                   // 1
$user->idKey();                // 'user_id'
$user->altId('username');      // 'alice'
$user->altId(['username', 'email']);
$user->extractIdentity();      // 1, with fallback to alt IDs

The data-state model

Marker Mutability Construction Use it for
Solid Immutable, public readonly props Constructor, validated Domain objects, repositories return these
Draft Mutable, flexible Same DTO class as Solid but relaxed Input staging, partial updates
GreyData Dynamic key/value Backed by $data array API payloads, third-party blobs

fromArray() uses Valinor for tolerant deserialisation; cloneWith() goes through the constructor for strict validation.

toArray() skips properties that were never assigned (undefined ≠ explicit null). toDbRow() builds on top of it and JSON-encodes any remaining array values — so ['data' => ['k' => 'v']] becomes ['data' => '{"k":"v"}'].

GreyData

GreyDataTrait implements a path-aware key/value bag for objects whose schema is dynamic:

$config->get('db/connection/default/host');     // path-based
$config->set('feature/flag/x', true);
$config->hasFeatureX();                         // magic getter (snake_case)
$config->withFeatureX(true);                    // immutable variant

Enums

use Flyokai\DataMate\StringEnumTrait;

enum Status: string
{
    use StringEnumTrait;

    case Active   = 'active';
    case Disabled = 'disabled';
}

Status::allowedValues();   // ['active', 'disabled']
Status::fromName('Active');
Status::tryFromName('xxx');
Status::validate('active');
Status::normalize('ACTIVE');

LCStringEnumTrait lower-cases input before lookup; UCStringEnumTrait upper-cases.

Collections (ItemJar)

use Flyokai\DataMate\ItemJar;
use Flyokai\DataMate\JarTrait;

final class UserJar implements ItemJar
{
    use JarTrait;
}

$jar = new UserJar();
$jar->add($user1);
$jar->get($user1->id());
foreach ($jar as $u) { /* … */ }

API surface

Class / Trait Purpose
Dto Core interface
DtoTrait Reflection-based Valinor implementation
Solid / Draft / GreyData State markers
HasId / HasAltId / HasIdDto Identity
EnumTrait, StringEnumTrait, IntEnumTrait, LCStringEnumTrait, UCStringEnumTrait Enum helpers
ItemJar, JarTrait Type-safe collections
ConfigJar, DdlTable, DdlTableJar Built-in DTOs
Assertions assertNotNull, assertPositiveInt, assertPositiveFloat
ValidationException, InvalidEntity Errors
dtoMapper(...) Cached configured Valinor TreeMapper

Gotchas

  • with() is Valinor-flexible (accepts loose types, slower). cloneWith() is constructor-strict (faster).
  • toDbRow() JSON-encodes arrays — be aware when reading rows back.
  • A constructor parameter that was never supplied is "undefined" and is skipped in toArray(). An explicit null is included.
  • GreyData does not track undefined — every key in $data is preserved.
  • __sleep() only serializes constructor params — implement __wakeup() to reinitialize derived state.
  • Reflection caches are global static and never cleared (intentional, for performance).
  • Alt-ID keys are strings; composite keys are arrays of strings, normalised via sorting.

See also

License

MIT

flyokai/data-mate 适用场景与选型建议

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-12-04