fleshgrinder / constraint-violations
This library provides an implementation of the Notification Pattern with additional functionality to keep code DRY and to be more useful in the context of Domain-Driven Design (DDD) and its value objects.
Requires
- php: >=7.0.0
Requires (Dev)
- codeclimate/php-test-reporter: ^0.3.0
- phpunit/phpunit: ^5.2
This package is not auto-updated.
Last update: 2020-01-24 16:07:58 UTC
README
PHP Constraint Violations
This library provides an implementation of the Notification Pattern with additional functionality to keep code DRY and to be more useful in the context of Domain-Driven Design (DDD) and its value objects.
Installation
Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:
$ composer require fleshgrinder/constraint-violations
This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.
Usage
Most basic usage is as illustrated in the following example:
$input = 'some data'; $errors = new ViolationCollector(); $warnings = new ViolationCollector(); if (some_validation($input) === false) { $errors->addViolation('some message'); } if (some_other_validation($input) === false) { $warnings->addViolation('some other message'); } // More validations ... if ($warnings->hasViolations) { echo "WARNINGS\n"; foreach ($warnings->getMessages() as $message) { echo "{$message}\n"; } } if ($errors->hasViolations) { echo "ERRORS\n"; foreach ($warnings->getMessages() as $message) { echo "{$message}\n"; } exit(1); } // Continue ...
Please have a look at the wiki for more examples.
Weblinks
- Martin Folwer: “Replacing Throwing Exceptions with Notification in Validations”, in martinfowler.com, December 9, 2014.