承接 vjik/specification 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

vjik/specification

最新稳定版本:1.0.0

Composer 安装命令:

composer require vjik/specification

包简介

Implementation of Specification pattern

README 文档

README

Latest Stable Version Total Downloads Build status Coverage Status Mutation testing badge type-coverage static analysis psalm-level

The package provides PHP implementation of "Specification" software design pattern:

  • interface SpecificationInterface and abstract BaseSpecification to create user specifications;
  • composite specifications AndSpecification and OrSpecification;
  • negation specification NotSpecification.

Requirements

  • PHP 8.2 or higher.

Installation

The package could be installed with composer:

composer require vjik/specification

General usage

You can create your own specifications by inheriting from the BaseSpecification class:

use Vjik\Specification\BaseSpecification;
    
/**
 * @template T as User
 * @template-extends BaseSpecification<T>
 */
final class UserIsAdultSpecification extends BaseSpecification
{
    /**
     * @param T $value
     */
    public function isSatisfiedBy(mixed $value): bool
    {
        return $value->age >= 18;
    }
}

$specification = new UserIsAdultSpecification();

// true or false
$specification->isSatisfiedBy($user); 

// throws an exception if user is not adult
$specification->satisfiedBy($user); 

We recommend use static analysis tools like Psalm and PHPStan to improve code quality.

Built-in specifications

You can combine specifications using composite specifications:

use Vjik\Specification\AndSpecification;
use Vjik\Specification\OrSpecification;

// User is adult AND active
$isActiveAdultUserSpecification = new AndSpecification([
    new UserIsAdultSpecification(),
    new UserIsActiveSpecification(),
]);

// User is adult OR user with parents
$userHasAccessSpecification = new OrSpecification([
    new UserIsAdultSpecification(),
    new UserWithParentsSpecification(),
]);

Also, you can use negation specification:

use Vjik\Specification\NotSpecification;

// User is not adult
$userIsNotAdultSpecification = new NotSpecification(
    new UserIsAdultSpecification()
);

Static analysis compatible

Static analysis tools like Psalm and PHPStan helps you to avoid mistakes. For example, Psalm issues:

$userIsAdultSpecification = new UserIsAdultSpecification();

// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::isSatisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->isSatisfiedBy('test');

// ERROR: InvalidArgument Argument 1 of UserIsAdultSpecification::satisfiedBy expects User, but 'test' provided
$userIsAdultSpecification->satisfiedBy('test');

// ERROR: InvalidArgument Incompatible types found for T (must have only one of User, Post)
$isActiveAdultUserSpecification = new AndSpecification([
    new UserIsAdultSpecification(),
    new PostIsActiveSpecificaion(),
]);

Documentation

If you have any questions or problems with this package, use author telegram chat for communication.

License

The vjik/specification is free software. It is released under the terms of the BSD License. Please see LICENSE for more information.

统计信息

  • 总下载量: 1
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2024-11-21