vdhicts/conditions

处理条件的包

v1.2.0 2024-03-17 06:36 UTC

This package is auto-updated.

Last update: 2024-09-17 07:42:34 UTC


README

此包提供了一种处理应用程序中操作条件的方法。例如,检查记录是否具有所需的状态(以及哪些条件被视为有效和/或无效),或者某个转换是否可能。有关更多示例,请参阅使用部分。

要求

此包需要PHP 8.1+。

安装

您可以通过composer安装此包

composer require vdhicts/conditions

使用

术语

  • 条件:条件是一个可以满足或不满足的单个检查。条件可以具有级别、消息和附加数据。
  • 条件集合:条件的集合。
  • 条件级别:条件级别是一个可以用来指示条件严重程度的级别。对于显示或过滤条件很有用。不影响条件的满足,因此未满足的信息级别条件仍然是未满足的条件。
  • 条件转换器:条件转换器用于将条件转换为其他表示或从其他表示创建条件。

条件

首先创建一个条件。条件至少有一个名称。当未提供满足参数时,条件被认为是满足的。

use Vdhicts\Conditions\Condition;
use Vdhicts\Conditions\Enums\ConditionLevel;

// Basic variant
$condition = new Condition(
    name: 'Contact has email address',
    fulfilled: $contact->email_address !== null,
);

// Extended variant
$condition = new Condition(
    name: 'Contact has email address',
    fulfilled: $contact->email_address !== null,
    level: ConditionLevel::Error,
    message: 'The contact needs to have an e-mail address to receive the newsletter.',
    data: [
        'contact_id' => $contact->id,
        'newsletter_id' => $newsletter->id,
    ]   
);

// Fluent variant
$condition = (new Condition())
    ->setName('Contact has email address')
    ->setFulfilled($contact->email_address !== null)
    ->setLevel(ConditionLevel::Error)
    ->setMessage('The contact needs to have an e-mail address to receive the newsletter.')
    ->setData([
        'contact_id' => $contact->id,
        'newsletter_id' => $newsletter->id,
    ]);

条件集合

当您有多个条件时,可以将它们添加到条件集合中。条件集合被认为是满足的,当其中所有条件都满足或没有提供任何条件时。

use Vdhicts\Conditions\ConditionCollection;

// Basic variant
$conditionCollection = new ConditionCollection(collect([
    new Condition('Contact has e-mail address', $contact->email_address !== null),
    new Condition('Contact has name', $contact->name !== null),
]));

// Fluent variant
$conditionCollection = (new ConditionCollection())
    ->add(new Condition('Contact has e-mail address', $contact->email_address !== null))
    ->add(new Condition('Contact has name', $contact->name !== null));

可以检查集合是否满足。

$conditionCollection->isFulfilled(); // true or false
$conditionCollection->isNotFulfilled(); // true or false

为了方便处理条件,还有一些方法可以根据某些要求检索条件。

$conditionCollection->get(); // Returns a collection of all conditions

$conditionCollection->getFulfilledConditions(); // Returns a collection of fulfilled conditions
$conditionCollection->getNotFulfilledConditions(); // Returns a collection of not fulfilled conditions

$conditionCollection->only([ConditionLevel::Error, ConditionLevel::Warning]); // Returns a condition collection of conditions with the provided levels
$conditionCollection->except([ConditionLevel::Info]); // Returns a condition collection of conditions without the provided levels

也可以将多个集合合并为一个。

$conditionCollection->merge($otherConditionCollection);

转换器

转换器可用于将条件转换为其他表示或从其他表示创建条件。

use Vdhicts\Conditions\Condition;
use Vdhicts\Conditions\ConditionTransformer;

// Transform a condition to an array
$array = ConditionTransformer::toArray(new Condition(
    name: 'Contact has email address',
    fulfilled: $contact->email_address !== null,
));

// Transform an array to a condition
$condition = ConditionTransformer::fromArray([
    'name' => 'Contact has email address',
    'fulfilled' => $contact->email_address !== null,
]);

贡献

发现了一个错误或想添加一个新功能?太好了!还有许多其他方式可以做出有意义的贡献,例如审查悬而未决的pull请求和编写文档。即使是为发现的错误打开一个问题也是受欢迎的。

安全性

如果您在此或Vdhicts的其他包中发现任何与安全性相关的问题,请通过电子邮件security@vdhicts.nl而不是使用问题跟踪器。

许可

此包是开源软件,受MIT许可许可。

关于Vdhicts

Vdhicts基于Laravel框架开发和实施面向企业的IT解决方案。