reformo / backendbase-specification
PHP中使用规范模式(Specification pattern)和通知模式(Notification pattern)的基本类。
1.0
2024-07-25 12:22 UTC
Requires
- php: ^8.3
- malukenho/mcbumpface: ^1.2.0
Requires (Dev)
- doctrine/coding-standard: ^12.0.0
- phpstan/phpstan: ^1.11
- phpunit/phpunit: ^11.2.8
This package is auto-updated.
Last update: 2024-08-25 19:02:16 UTC
README
PHP中规范模式的基本类。除了典型的and
、or
和not
规范外,还提出了anyOf
、oneOf
、noneOf
规范。
此包基于carlosbuenosvinos/ddd中的实现。
安装
$ composer require reformo/backendbase-specification
使用示例
<?php use Backendbase\Specification\Specification; use Backendbase\Specification\OneOfSpecification; class Order { public function isPaid() { return true; } public function isShipped() { return false; } public function isCancelled() { return false; } } class UnshippedOrderSpecification extends Specification { public function isSatisfiedBy($order): bool { return !$order->isShipped(); } } class PaidOrderSpecification extends Specification { public function isSatisfiedBy($order): bool { return $order->isPaid(); } } class CancelledOrderSpecification extends Specification { public function isSatisfiedBy($order): bool { return $order->isCancelled(); } } $paid = new PaidOrderSpecification; $unshipped = new UnshippedOrderSpecification; $cancelled = new CancelledOrderSpecification; $paid->and($unshipped)->isSatisfiedBy(new Order); // => true (new OneOfSpecification($paid, $unshipped, $cancelled))->isSatisfiedBy(new Order); // => true