boulzy/specification
最新稳定版本:1.2.1
Composer 安装命令:
composer require boulzy/specification
包简介
PHP implementation of the Specification pattern.
关键字:
README 文档
README
PHP library implementing the Specification pattern.
Installation
The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:
$ composer require boulzy/specification
Usage
The design pattern "Specification" describes business rules. Specifications can be chained or combined to apply more complex business rules.
A Specification must implement the interface Boulzy\Specification\Specification, though it's recommanded to
extend the abstract class Boulzy\Specification\CompositeSpecification that already implements the combination of
specifications.
<?php
class TrueSpecification extends Boulzy\Specification\CompositeSpecification
{
public function isSatisfiedBy($candidate) : bool
{
return true === $candidate;
}
}
class FalseSpecification extends Boulzy\Specification\CompositeSpecification
{
public function isSatisfiedBy($candidate) : bool
{
return false === $candidate;
}
}
$trueSpecification = new TrueSpecification();
$falseSpecification = new FalseSpecification();
$isSatisfied = $trueSpecification->isSatisfiedBy(true); // true
$isSatisfied = $trueSpecification->isSatisfiedBy(false); // false
$isSatisfied = $falseSpecification->isSatisfiedBy(true); // false
$isSatisfied = $falseSpecification->isSatisfiedBy(false); // true
$isSatisfied = $trueSpecification->not()->isSatisfiedBy(true); // false
$isSatisfied = $trueSpecification->not()->isSatisfiedBy(false); // true
$isSatisfied = $trueSpecification->and($falseSpecification)->isSatisfiedBy(true); // false
$isSatisfied = $trueSpecification->and($falseSpecification)->isSatisfiedBy(false); // false
$isSatisfied = $trueSpecification->andNot($falseSpecification)->isSatisfiedBy(true); // true
$isSatisfied = $trueSpecification->or($falseSpecification)->isSatisfiedBy(true); // true
See the unit tests for simple examples.
License
This project is licensed under the MIT License.
统计信息
- 总下载量: 102
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-11-01