PHP implementation of Domain Notification Pattern based in Flunt (.NET) developed by @andrebaltieri
Flunt is a fluent way to use Notification Pattern with your entities, concentrating every change you made and easy accessing it when you need.
check our Wiki for more details and samples of how to use Flunt in your applications.
Package | Version |
---|---|
PHP |
>=8.1 |
This package is available through 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');
If you are building an application with clean architecture approaches you can consider use this project, cause it's totally free of external dependencies, feel free to implement a Port/Adapter linking the dependencie with your code base, this one is for those who are more conceptual =)