diffhead/php-data-enrichment-kit
Composer 安装命令:
composer require diffhead/php-data-enrichment-kit
包简介
Data enrichment library. A suitable component for microservice architectures.
关键字:
README 文档
README
A collection of components for building microservice-based applications. This library provides tools for enriching data during communication between system components.
Installation
# Install the package
composer require diffhead/php-data-enrichment-kit
# Run library tests
composer test
Usage
To use this library, you first need to create repositories
that implement the Repository interface:
namespace Diffhead\PHP\DataEnrichmentKit\Interface;
interface Repository
{
/**
* @param string $field
* @param array $values
*
* @return iterable<int,\Diffhead\PHP\DataEnrichmentKit\Interface\Entity>
*/
public function getByFieldValues(string $field, array $values): iterable;
}
Each entity must implement the Entity interface:
namespace Diffhead\PHP\DataEnrichmentKit\Interface;
interface Entity extends \ArrayAccess, \JsonSerializable {}
Creating Enrichment Requests and Attaching to HTTP Messages
use Diffhead\PHP\DataEnrichmentKit\Builder;
use Diffhead\PHP\DataEnrichmentKit\Message;
use Diffhead\PHP\DataEnrichmentKit\Header;
use Diffhead\PHP\DataEnrichmentKit\Service\Serializer;
use Diffhead\PHP\DataEnrichmentKit\Service\Parser;
use Diffhead\PHP\DataEnrichmentKit\Storage\Requests;
$builder = Builder::withTarget('user', 'id');
$builder
->item('data.posts.*.creator_id', 'creator')
->item('data.posts.*.moderator_id', 'moderator');
$requests = new Requests([
$builder->build()
]);
$message = new Message(new Serializer(), new Parser());
/**
* @var \Psr\Http\Message\MessageInterface $psrMessage
*
* Message contains the following body data:
*
* {"data":{"posts":[{"id":1,"title":"String","creator_id":1,"moderator_id":3}]}}
*
* This call will attach enrichment requests to a message
*/
$message->setRequests($psrMessage, Header::XEnrichmentRequest, $requests);
Receiving Data and Performing Enrichment
use Diffhead\PHP\DataEnrichmentKit\Enricher;
use Diffhead\PHP\DataEnrichmentKit\Message;
use Diffhead\PHP\DataEnrichmentKit\Header;
use Diffhead\PHP\DataEnrichmentKit\Service\Enrichment;
use Diffhead\PHP\DataEnrichmentKit\Service\Serializer;
use Diffhead\PHP\DataEnrichmentKit\Service\Parser;
use Diffhead\PHP\DataEnrichmentKit\Storage\Repositories;
use Diffhead\PHP\DataEnrichmentKit\Interface\Repository;
/**
* Repository implementation example
*/
$repository = new class() implements Repository
{
private array $items = [
['id' => 1, 'name' => 'Antony'],
['id' => 3, 'name' => 'Martin']
];
public function getByFieldValues(string $field, array $values): iterable
{
return array_filter($this->items, fn(array $item) => in_array($item[$field], $values));
}
};
$repositories = new Repositories([
'user' => $repository
]);
/**
* Initialize services
*/
$enricher = new Enricher(new Enrichment($repositories));
$message = new Message(new Serializer(), new Parser());
/**
* Getting enrichment requests from psr message
*/
$requests = $message->getRequests($psrMessage, Header::XEnrichmentRequest);
/**
* Getting array payload from message
*/
$payload = $message->getPayload($psrMessage);
/**
* Enrich the payload using the registered repositories
* Expected enriched structure:
*
* [
* "data" => [
* "posts" => [
* [
* "id" => 1,
* "title" => "String",
* "creator_id" => 1,
* "moderator_id" => 3,
* "creator" => ["id" => 1, "name" => "Antony"],
* "moderator" => ["id" => 3, "name" => "Martin"]
* ]
* ]
* ]
* ]
*/
$enriched = $enricher->enrich($payload, $requests);
/**
* Set new payload to psr message
*/
$psrMessage = $message->setPayload($psrMessage, $enriched);
Anatomy
This library consists of the following high-level service components:
- Enricher – a self-explanatory service that enriches data.
- Message – a PSR-compatible component for interacting with
HTTP requests and responses using
MessageInterface. - Builder – helps build enrichment request objects.
Lower-level components for building custom enrichment algorithms:
- Enrichment – implements the
Enrichmentinterface and contains the enrichment logic. - Parser – parses raw and returns
Requestobjects. - Serializer – serializes
Requestobjects to strings.
Value objects:
- Item – a unit describing an enrichment reference and alias.
- ItemsBag – stores multiple
Iteminstances. - Request – represents a single enrichment request.
- Target – specifies the entity to enrich.
Storage objects:
- Requests – contains enrichment requests and implements
IteratorAggregate. - Repositories – stores a map of entity repositories.
Customize
You can extend the library by implementing your own:
Data enrichment logic
namespace Diffhead\PHP\DataEnrichmentKit\Interface;
use Diffhead\PHP\DataEnrichmentKit\Storage\Requests;
interface Enrichment
{
public function enrich(array $data, Requests $requests): array;
}
Request parsing
namespace Diffhead\PHP\DataEnrichmentKit\Interface;
use Diffhead\PHP\DataEnrichmentKit\Object\Request;
interface Parser
{
public function parse(string $value): Request;
}
Request serialization
namespace Diffhead\PHP\DataEnrichmentKit\Interface;
use Diffhead\PHP\DataEnrichmentKit\Object\Request;
interface Serializer
{
public function toString(Request $request): string;
}
Notes
Designed primarily for HTTP, but low-level components allow you to quickly implement high-level integrations for any data source.
Supports PSR-compliant messages and can be integrated into frameworks like Laravel with custom adapters.
diffhead/php-data-enrichment-kit 适用场景与选型建议
diffhead/php-data-enrichment-kit 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 222 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「parser」 「message」 「serializer」 「pipeline」 「processing」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 diffhead/php-data-enrichment-kit 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 diffhead/php-data-enrichment-kit 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 diffhead/php-data-enrichment-kit 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Easy to use SDK with grabber for multiple platforms at once like YouTube, Dailymotion, Facebook and more.
PHP AMQP Binding Library
An MT940 bank statement parser for PHP
Laravel integration for the jsonapi.org parser
A modern HTML DOM parser for PHP using DOMDocument and Symfony CssSelector.
JPOPHP (JSON Parser Object PHP) is a library for parsing the data in JSON format.
统计信息
- 总下载量: 222
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 25
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-11-26