承接 yohang/sorter 相关项目开发

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

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

yohang/sorter

最新稳定版本:1.1.1

Composer 安装命令:

composer require yohang/sorter

包简介

A library to dynamically sort collections / queries

README 文档

README

Sorter is a PHP column sorting library that allows you to apply sorts of any kind of data source.

tests Mutation testing badge Coverage Status

Features

  • Sorts any kind of data source (array, Doctrine ORM, and plain SQL built-in)
  • Sorts by multiple columns
  • Factorize sorting logic into definitions classes
  • Process HTTP request
  • Symfony Bundle
  • Twig extension

Installation

 $ composer require tilleuls/sorter

Optionnal : enable symfony bundle

<?php

return [
    // ...
    Sorter\Extension\Symfony\Bundle\SorterBundle::class => ['all' => true],
];

Usage

Sorter provides a SorterFactory class that allows you to sort your data source.

The factory builds a sorter instance and requires an applier to apply the sort to the data source.

The classic way to use it is the following :

  1. Create a SorterFactory instance (if you are not using Symfony)
  2. Create a sorter instance using the factory
  3. Define the sorting columns, the default sort, and eventually the query prefix

Basic sorting

// Create the sorter factory (useless with Symfony)
$factory = new SorterFactory([new DoctrineORMApplier()]);

// Create your sorter instance and make your definition
$sorter = $factory->createSorter()
    ->add('title', 'p.title')
    ->add('date', 'p.date')
    ->addDefault('date', Sort::ASC);

// Handle takes an array of data and transform it to a Sort object
$sorter->handle([]);

// Apply the sort to the data
$data = $sorter->sort($data);

The Sorter\Sorter class providers the following methods to define your sorts :

  • $sorter->add(string $field, string $path) : Adds a new column to the sorter. $field is the name of the column, and $path is the path to the column in your data source. The path can be a SQL expression, a property name, or even an array key index.
  • $sorter->addDefault(string $field, string $direction) / $sorter->removeDefault(string $field) : Adds a default sort to the sorter. $field is the name of the column, and $direction is the direction of the sort (Sort::ASC or Sort::DESC).
  • $sorter->setPrefix(string $prefix) : Sets the prefix to be used in the query string. This is useful if you want to use several sorters in the same page.

Symfony usage

With Symfony, the SorterFactory is available as a service.

class IndexController
{
    public function __construct(
        private SorterFactory $factory,
        private PostRepository $repository,
        private Environment $twig,
    ) {
    }
    
    public function index(Request $request)
    {
        $sorter = $this->factory->createSorter()
            ->add('title', 'p.title')
            ->add('date', 'p.date')
            ->addDefault('date', Sort::ASC);
    
        $sorter->handleRequest($request);
        $qb = $sorter->sort($this->repository->createQueryBuilder('p'));
    
        return new Response(
            $this->twig->render(
                'array-sort.html.twig',
                [
                    'sorter' => $sorter,
                    'data' => $qb->getQuery()->getResult(),
                ],
            ),
        );
    }
}

Definition class

You can factorize your sorting logic into a definition class. Definition classes are useful if you want to reuse the same sorting logic in several places.

use Sorter\Definition;
use Sorter\Sorter;

class PostSortDefinition implements Definition
{
    public function buildSorter(Sorter $sorter): void
    {
        $sorter
            ->add('title', 'p.title')
            ->add('date', 'p.date')
            ->addDefault('date', Sort::ASC);
    }
}
class IndexController
{
    public function __construct(
        private SorterFactory $factory,
        private PostRepository $repository,
        private Environment $twig,
    ) {
    }
    
    public function index(Request $request)
    {
        $sorter = $this->factory->createSorter(new PostSortDefinition());
        $sorter->handleRequest($request);
        $qb = $sorter->sort($this->repository->createQueryBuilder('p'));
    
        return new Response(
            $this->twig->render(
                'array-sort.html.twig',
                [
                    'sorter' => $sorter,
                    'data' => $qb->getQuery()->getResult(),
                ],
            ),
        );
    }
}

Twig extension

You can use the SorterExtension to render the sorting links in your Twig templates.

The twig extension provides the following functions:

  • sorter_link : Renders a ready to use and request aware sorting link.
  • sorter_url : Renders a URL for the sorting link. This is useful if you want to use your own HTML.
  • sorter_direction : Returns the direction of the sort for a given column. This is useful if you want to use your own HTML.

Example

<table>
  <thead>
    <tr>
      <th scope="col" data-col="title">
        {{ sorter_link(sorter, 'title', 'Title') }}
      </th>
      <th scope="col" data-col="a">
        {{ sorter_link(sorter, 'a', 'A') }}
      </th>
      <th scope="col" data-col="b">
        <a href="{{ sorter_url(sorter, 'b') }}" class="SortLink SortLink--{{ sorter_direction(sorter, 'b') }}">B</a>
      </th>
      <th scope="col" data-col="c">
        <a href="{{ sorter_url(sorter, 'c') }}" class="SortLink SortLink--{{ sorter_direction(sorter, 'c') }}">
          C
        </a>
      </th>
    </tr>
  </thead>
  <tbody>
    {% for row in data %}
      <tr>
        <th scope="row">{{ row.title }}</th>
        <td>{{ row.a }}</td>
        <td>{{ row.b }}</td>
        <td>{{ row.c }}</td>
      </tr>
    {% endfor %}
  </tbody>
</table>

Object sorting with ArrayApplier

By default, ArrayApplier use the PHP spaceship operator <=> to compare values. With some objects, you may want to use a custom comparison function.

You can use the Comparable interface to define a custom comparison function for your objects.

final class CustomObject implements Comparable
{
    public function __construct(
        public string $value1,
        public string $value2,
    ) {
    }

    public function compare(Comparable $other): int
    {
        return $this->value2 <=> $other->value2;
    }
}

```php

yohang/sorter 适用场景与选型建议

yohang/sorter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 126 次下载、GitHub Stars 达 4, 最近一次更新时间为 2025 年 03 月 24 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 yohang/sorter 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-24