承接 maartenpaauw/laravel-specification-pattern 相关项目开发

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

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

maartenpaauw/laravel-specification-pattern

最新稳定版本:v3.0.0

Composer 安装命令:

composer require maartenpaauw/laravel-specification-pattern

包简介

Filter an Illuminate collection with specifications

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Codecov Total Downloads

Filter an Illuminate collection with specifications.

Support me

Pennant for Filament

You can support me by buying Pennant feature flags for Filament.

Installation

You can install the package via composer:

composer require maartenpaauw/laravel-specification-pattern

You can publish the config file with:

php artisan vendor:publish --tag="laravel-specification-pattern-config"

This is the contents of the published config file:

return [
    'collection-method' => 'matching',
];

Usage

Here's how you can create a specification:

php artisan make:specification AdultSpecification

This will generate a specification class within the App\Specifications namespace.

<?php

namespace App\Specifications;

use Maartenpaauw\Specifications\Specification;

/**
 * @implements  Specification<mixed>
 */
class AdultSpecification implements Specification
{
    /**
     * {@inheritDoc}
     */
    public function isSatisfiedBy(mixed $candidate): bool
    {
        return true;
    }
}

Imagine we have the following class which represents a person with a given age.

class Person {
    public function __construct(public int $age)
    {}
}

Let's apply the business logic:

<?php

namespace App\Specifications;

use Maartenpaauw\Specifications\Specification;

/**
- * @implements  Specification<mixed>
+ * @implements  Specification<Person>
 */
class AdultSpecification implements Specification
{
    /**
     * {@inheritDoc}
     */
    public function isSatisfiedBy(mixed $candidate): bool
    {
-        return true;
+        return $candidate->age >= 18;
    }
}

After applying the bussiness logic we can use the specification by directly calling the isSatisfiedBy method or indirectly be filtering an eloquent collection by calling the matching method.

Direct

$specification = new AdultSpecification();

// ...

$specification->isSatisfiedBy(new Person(16)); // false
$specification->isSatisfiedBy(new Person(32)); // true

Indirect

$persons = collect([
    new Person(10),
    new Person(17),
    new Person(18),
    new Person(32),
]);

// ...

// Returns a collection with persons matching the specification
$persons = $persons->matching(new AdultSpecification());

// ...

$persons->count(); // 2

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

  • Stars: 13
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知