PHPackages                             php-ddd/notification - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Mail &amp; Notifications](/categories/mail)
4. /
5. php-ddd/notification

AbandonedArchivedLibrary[Mail &amp; Notifications](/categories/mail)

php-ddd/notification
====================

PHP implementation of Fowler's Notification pattern

v1.0.2(11y ago)1527.8k↑21.4%1MITPHPPHP &gt;=5.3.3

Since Dec 27Pushed 11y ago4 watchersCompare

[ Source](https://github.com/php-ddd/notification)[ Packagist](https://packagist.org/packages/php-ddd/notification)[ RSS](/packages/php-ddd-notification/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (2)Versions (4)Used By (0)

[![Build Status](https://camo.githubusercontent.com/a32ba6e6f74a40c325c988d0192e9c4aad0cd42df346db1a3d24085992739f07/68747470733a2f2f7472617669732d63692e6f72672f7068702d6464642f6e6f74696669636174696f6e2e737667)](https://travis-ci.org/php-ddd/command)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/dcf39d079a5a7d1cce0fb93e0e2969ce92e6fa7f0f5fd83b0c8aee11bb331334/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7068702d6464642f6e6f74696669636174696f6e2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/php-ddd/notification/?branch=master)[![Code Coverage](https://camo.githubusercontent.com/2bc9d18e98d0dcc29211a16c12fcc128fc1ae48cf735fc189b6e9cddb88b8de2/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f7068702d6464642f6e6f74696669636174696f6e2f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/php-ddd/notification/?branch=master)[![SensioLabsInsight](https://camo.githubusercontent.com/4f52175c99a26bf69855a13f1d9815670f17ea184f016208ab42ff275ac7f979/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f38353266653630382d313630352d343831622d383861352d6233633631333730313961392f6d696e692e706e67)](https://insight.sensiolabs.com/projects/852fe608-1605-481b-88a5-b3c6137019a9)Notification
===========================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#notification)

PHP 5.3+ library to handle to replace throwing exceptions with notification in validations.

> "If you're validating some data, you usually shouldn't be using exceptions to signal validation failures." -- [Martin Fowler](http://martinfowler.com/articles/replaceThrowWithNotification.html)

Installation
------------

[](#installation)

This is installable via [Composer](https://getcomposer.org/) as [php-ddd/notification](https://packagist.org/packages/php-ddd/notification):

```
$ composer require php-ddd/notification

```

Usage
-----

[](#usage)

Suppose we have a code like this:

```
use Exception;

class PurchaseOrder
{
    /**
     * @var Items[]
     */
    private $items = array();

    /**
     * @var ShippingInformation
     */
    private $shippingInformation;

    /**
     * Check if we can validate the purchase order
     *
     * @throw Exception an Exception containing the reason why we can't validate the Order
     */
    public function canValidate()
    {
        if (empty($this->items)) {
            throw new Exception('There is nothing to purchase.');
        }

        foreach($this->items as $item) {
            if ($item->isProductExpires()) {
                throw new Exception(sprintf('The item %s is no longer available.', (string)$item));
            }
        }

        if (!$this->shippingInformation->isComplete()) {
            throw new Exception('You should first complete your shipping information');
        }
    }
}
```

We want to avoid using Exception because the goal of this method is to know if we can validate the Purchase order, not to validate it.
This kind of method should return a boolean value saying whether we can validate or not.
But returning a boolean without throwing exception means we loose information about why we can't purchase this order.
We can having both information by introducing the Notification pattern.

```
use PhpDDD\Notification;

class PurchaseOrder
{
    /**
     * @var Items[]
     */
    private $items = array();

    /**
     * @var ShippingInformation
     */
    private $shippingInformation;

    /**
     * Check if we can validate the purchase order
     *
     * @param Notification $notification
     *
     * @return bool whether we can validate this order or not
     */
    public function canValidate(Notification $notification)
    {
        if (empty($this->items)) {
            $notification->addError('There is nothing to purchase.');
        }

        foreach($this->items as $item) {
            if ($item->isProductExpires()) {
                $notification->addError(sprintf('The item %s is no longer available.', (string)$item));
            }
        }

        if (!$this->shippingInformation->isComplete()) {
            $notification->addError('You should first complete your shipping information');
        }

        return $notification->hasError();
    }
}

// Elsewhere
use Exception;

$order = new PurchaseOrder();
// ...
$notification = new Notification();
if (!$order->canValidate($notification)) {
    throw new Exception($notification->firstMessage());
}
```

Bear in mind that this changes the behavior of your code. You should read [Martin Fowler's blog article](http://martinfowler.com/articles/replaceThrowWithNotification.html) to see how you can do this without breaking your code.

Sometimes, you don't want to know why we cannot validate. So, creating the notification object is irrelevant for your context.
In this case, simply update your code like so:

```
use PhpDDD\Notification;

class PurchaseOrder
{
    /**
     * @var Items[]
     */
    private $items = array();

    /**
     * @var ShippingInformation
     */
    private $shippingInformation;

    /**
     * Check if we can validate the purchase order
     *
     * @param Notification|null $notification
     *
     * @return bool whether we can validate this order or not
     */
    public function canValidate(Notification $notification = null)
    {
        if (null === $notification) {
            $notification = new Notification();
        }

        if (empty($this->items)) {
            $notification->addError('There is nothing to purchase.');
        }

        foreach($this->items as $item) {
            if ($item->isProductExpires()) {
                $notification->addError(sprintf('The item %s is no longer available.', (string)$item));
            }
        }

        if (!$this->shippingInformation->isComplete()) {
            $notification->addError('You should first complete your shipping information');
        }

        return $notification->hasError();
    }
}

// Elsewhere
$order = new PurchaseOrder();
// ...
if (!$order->canValidate()) {
    return;
}
```

###  Health Score

34

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity35

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity60

Established project with proven stability

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

3

Last Release

4160d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/5cc2f661073d68100db53e02e0892a69076f0b44bc0ced487b043c6aff28ad0c?d=identicon)[JulienDufresne](/maintainers/JulienDufresne)

---

Tags

Value ObjectnotificationDomain Driven DesigndddGeneric Sub-domain

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/php-ddd-notification/health.svg)

```
[![Health](https://phpackages.com/badges/php-ddd-notification/health.svg)](https://phpackages.com/packages/php-ddd-notification)
```

###  Alternatives

[symfony/notifier

Sends notifications via one or more channels (email, SMS, ...)

80640.3M290](/packages/symfony-notifier)[jolicode/jolinotif

Send desktop notifications on Windows, Linux, MacOS.

1.4k11.6M41](/packages/jolicode-jolinotif)[laravel-notification-channels/telegram

Telegram Notifications Channel for Laravel

1.1k3.4M35](/packages/laravel-notification-channels-telegram)[laravel-notification-channels/twilio

Provides Twilio notification channel for Laravel

2587.7M12](/packages/laravel-notification-channels-twilio)[witty/laravel-push-notification

Laravel 5 Package for sending push notifications to Android and iOS devices

245.0k](/packages/witty-laravel-push-notification)[pyrech/composer-notifier

Display desktop notifications when composer finishes to install / update

411.7k](/packages/pyrech-composer-notifier)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
