承接 hibit-dev/criteria 相关项目开发

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

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

hibit-dev/criteria

Composer 安装命令:

composer require hibit-dev/criteria

包简介

A comprehensive package for managing criteria pattern in PHP projects, streamlining data filtering, sorting, and pagination with ease.

README 文档

README

A comprehensive package for managing criteria pattern in PHP projects, streamlining data filtering, sorting, and pagination with ease.

GitHub Actions Workflow Status GitHub License Packagist Version Packagist Downloads Tests Coverage

Clean query building using Criteria

Criteria is a framework-agnostic PHP package that simplifies the use of the criteria pattern for filtering, sorting, and paginating data. It helps separate query logic from repositories, making the codebase easier to maintain and extend over time. By using Criteria, developers can handle complex querying needs without spreading filter logic across different parts of the application.

Installation

Install Criteria using composer require:

composer require hibit-dev/criteria

Generating criteria

A specific criteria must be created for each use case. It extends the shared domain logic found in the abstract Criteria implementation, which standardizes how filtering, sorting, and pagination are applied. This ensures that each query follows a consistent pattern while allowing flexibility for different filtering needs.

In this example, UserSearchCriteria is designed to handle filtering by name and email, while accepting CriteriaPagination and CriteriaSort to manage pagination and sorting. The static create() method builds a fully constructed criteria object.

use Hibit\Criteria;
use Hibit\CriteriaPagination;
use Hibit\CriteriaSort;
 
final readonly class UserSearchCriteria extends Criteria
{
    public ?string $name;
    public ?string $email;
 
    public static function create(
        CriteriaPagination $pagination,
        CriteriaSort $sort,
        ?string $name = null,
        ?string $email = null,
    ): UserSearchCriteria {
        $criteria = new self($pagination, $sort);
 
        $criteria->name = $name;
        $criteria->email = $email;
 
        return $criteria;
    }
}

Marking the class as readonly ensures that the criteria object cannot be modified after creation, maintaining consistency and preventing unwanted changes during processing.

Pagination

The CriteriaPagination class is responsible for managing pagination. The create() method is used to easily construct a pagination object, where you can specify the limit and offset values (defaulting to 10 and 0 if not specified).

// Default Pagination: limit=10, offset=0
CriteriaPagination::create()

// Pagination: limit=10 (default), offset=10
CriteriaPagination::create(null, 10)

// Pagination: limit=20, offset=0 (default)
CriteriaPagination::create(20)

// Pagination: limit=20, offset=20
CriteriaPagination::create(20, 20)

The first value represents the limit (items per page), and the second value represents the offset (the starting point for the results). This approach ensures flexibility and ease of use when handling pagination in queries.

Sorting

The CriteriaSort class manages the sorting of query results. It works with the create() method to specify the field by which to sort the results, as well as the sorting direction (ascending or descending).

// Sorting: created_at DESC (default)
CriteriaSort::create('created_at')

// Sorting: created_at ASC
CriteriaSort::create('created_at', CriteriaSortDirection::ASC)
 
// Sorting: name DESC
CriteriaSort::create('name', CriteriaSortDirection::DESC)

This class helps maintain a consistent sorting approach throughout the application.

Integrating Criteria

Once a custom criteria class is defined, it can be passed into the repository to filter, sort, and paginate the data accordingly. The repository is where the actual query-building happens, using the criteria object to structure the database query. This keeps the filtering, sorting, and pagination logic separate from the rest of the application, making the code cleaner and easier to maintain.

use Hibit\CriteriaPagination;
use Hibit\CriteriaSort;
use Hibit\CriteriaSortDirection;
 
class UserRepository
{
    public function searchByCriteria(UserSearchCriteria $criteria): array
    {
        // Start query builder to apply filtering
 
        if ($criteria->name !== null) {
            // Apply name filter: $criteria->name
        }
 
        if ($criteria->email !== null) {
            // Apply email filter: $criteria->email
        }
 
        if ($criteria->pagination !== null) {
            // Apply limit: $criteria->pagination->limit
            // Apply offset: $criteria->pagination->offset
        }
 
        if ($criteria->sort) {
            // Apply sorting field: $criteria->sort->field->value()
            // Apply sorting direction: $criteria->sort->direction->value()
        }
 
        // Execute the query and return the results
    }
}

Note that all values can be nullable when constructing the query within the repository.

Using Criteria

To use the criteria in your repository, create an instance of the criteria class with the necessary filters, pagination, and sorting. Then, pass the criteria object to the repository method, which will apply it to the query.

use Hibit\CriteriaPagination;
use Hibit\CriteriaSort;
use Hibit\CriteriaSortDirection;
 
class CriteriaTestClass
{
    public function __invoke(UserRepository $userRepository): array
    {
        return $userRepository->searchByCriteria(
            UserSearchCriteria::create(
                CriteriaPagination::create(), // Default pagination
                CriteriaSort::create('created_at', CriteriaSortDirection::DESC),
                'John'
            )
        );
    }
}

This approach ensures that filtering, sorting, and pagination are easily maintained and reused throughout the application.

Documentation

Discover a world of knowledge hosted on HiBit website. Serving as your informational hub, this resource offers clear instructions and valuable insights to explore a spectrum of articles, tutorials, stories, news, and beyond.

You'll find detailed instructions and comprehensive documentation about this repository on:

Security

If you discover any security related issues, please email security@hibit.dev instead of using the issue tracker.

About HiBit

HiBit isn't just a blog; it's your go-to space for everything related to development, IT, and the wonders of electronics. Designed for developers, IT enthusiasts, and electronics hobby lovers, HiBit is a dynamic hub that keeps you in the loop with fresh and engaging content.

Explore a collection of articles, tutorials, and insights, encouraging a lively community where reading, commenting, discussing, and sharing experiences is not just promoted but celebrated.

License

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

hibit-dev/criteria 适用场景与选型建议

hibit-dev/criteria 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 108 次下载、GitHub Stars 达 8, 最近一次更新时间为 2023 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「repository」 「pagination」 「laravel」 「filters」 「sorting」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 hibit-dev/criteria 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hibit-dev/criteria 我们能提供哪些服务?
定制开发 / 二次开发

基于 hibit-dev/criteria 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-29