定制 boulzy/specification 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-11-01