bloise/flunt-php

1.0.0 2022-05-28 18:34 UTC

This package is auto-updated.

Last update: 2024-09-29 06:03:19 UTC


README

PHP 实现基于 Flunt (.NET) 的领域通知模式,由 @andrebaltieri 开发

Flunt

Flunt 是一种流畅的方式,使用通知模式与实体交互,集中记录所有更改,并在需要时轻松访问。

查看我们的 Wiki 以获取更多详细信息和使用 Flunt 在应用程序中使用的示例。

依赖关系

安装

此包通过 Packagist 提供

Composer

composer require bloise/flunt

如何使用

final class CPF extends Notifiable
{
  ...
}

$cpf = new CPF('012.345.678-90');
$cpf->addNotification("CPF", "Invalid document");

if($cpf->isValid())
  ...

指定的合约

final class CPF extends Notifiable
{
    private readonly string $document;

    public function __construct(string $document)
    {
        $this->setDocument($document);
        $this->addNotifications(
            (new CPFContract)
                ->hasMinLen($this->document, 'CPF', 'Invalid length')
                ->validFormat($this->document, 'CPF', 'Invalid format')
                ->validDocument($this->document, 'CPF', 'Invalid document')
        );
    }
}

final class CPFContract extends Contract
{
    public function hasMinLen(string $cpf, string $property, string $message): self {
        if (strlen($cpf) != 11) {
            $this->notifications[$property][] = $message;
        }
    }

    public function validFormat(string $cpf, string $property, string $message): self {...}

    public function validDocument(string $cpf, string $property, string $message): self {...}
}

流畅方法

$cpf->isValid();
$cpf->getMessages();
$cpf->filterByMessage('Invalid length');

为现代架构方法设计

如果你正在使用干净的架构方法构建应用程序,你可以考虑使用此项目,因为它完全无外部依赖,你可以自由地实现端口/适配器,将依赖关系与代码库连接起来,这个是为那些更注重概念的人 =)