fleshgrinder / constraint-violations
此包已被废弃且不再维护。未建议替代包。
此库提供了一个通知模式的实现,并增加了额外的功能以保持代码简洁并使其在领域驱动设计(DDD)及其值对象的应用中更加有用。
0.1.0
2016-04-03 19:29 UTC
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 约束违反
此库提供了一个通知模式的实现,并增加了额外的功能以保持代码简洁并使其在领域驱动设计(DDD)及其值对象的应用中更加有用。
安装
打开终端,进入您的项目目录,并执行以下命令以添加此包到您的依赖项
$ composer require fleshgrinder/constraint-violations
此命令需要您全局安装了Composer,如Composer文档中的安装章节所述。
用法
最基本的使用方式如下所示
$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 ...
请查看维基以获取更多示例。
链接
- Martin Folwer: “在验证中用通知代替抛出异常”,在 martinfowler.com,2014年12月9日。