vaened/dictionary-flow

一个用于在键值数据字典中进行全面评估的库,支持精确的条件定义和基于数据的决策。

v2.0 2024-09-10 16:58 UTC

This package is auto-updated.

Last update: 2024-09-10 18:27:00 UTC


README

Tests Software License

字典流库旨在简化基于键值数据字典的决策和动作处理。它通过减少重复的条件和检查需求,提供了一种更结构化和灵活的方法来管理参数和执行动作。

使用此工具,您可以轻松定义处理数据和执行相应动作的规则,减少对多个if语句和手动验证的依赖。这提高了代码的可读性和可维护性,使其更容易与处理动态数据的系统集成。

安装

您可以使用composer安装此库。

composer require vaened/dictionary-flow

用法

$mediator = new Mediator($parameters);

// Utilize reflection to dynamically evaluate the data dictionary
// based on the specified function signature.
$mediator->on(
    Matches::signature(
        fn(array $skills) => /* Perform appropriate action for skills */
    )
);

// Manually check if the 'birthdate' key has a value and process
// it accordingly.
$mediator->on(
    Has::value(
        Input::date('birthdate'),
        fn(DateTimeInterface $birthdate) => /* Perform relevant action based on birthdate */
    )
);

初始化

要开始使用此库,请按照以下步骤设置基本组件

// Define your input data as an associative array.
$dictionary = [
    'name' => 'You',
    'birthdate' => '1996-01-01',
    'married' => false,
    'skills' => ['PHP', 'Js', 'Python']
];

// Create a Parameters instance from the defined dictionary.
$parameters = Parameters::from($dictionary);

// Initialize the Mediator using the prepared Parameters instance.
$mediator = new Mediator($parameters);

特性

代码简化:用清晰声明性结构替换多个if检查,以管理条件和动作。

灵活评估:通过简单可扩展的接口,允许定义针对各种数据类型和场景的规则和动作。

结构化数据处理:根据键值字典促进参数处理,更有效地组织逻辑。

无缝集成:易于集成到应用程序中,实现快速简便的配置。

可扩展性:允许开发者为满足特定需求创建自己的规则和条件实现。

许可

此库受MIT许可证的许可。有关更多信息,请参阅许可文件