定制 nickbeen/sequential-rank 二次开发

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

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

nickbeen/sequential-rank

Composer 安装命令:

composer require nickbeen/sequential-rank

包简介

Simple ranking system for ordering results by sequence

README 文档

README

Latest version Build status Total downloads PHP Version License

Simple ranking system for ordering results by sequence. Usually you'll need to sort your results alphabetically or chronologically (or even manually), but what if you need to order specific fields based on their exact order. Doable when working with justs integers or a handful of possible sequences, but can get quite complicated with dozens of possible sequences. Be sure to check the example if the concept sounds too vague.

Sequential Ranking is a simple ranking system that relies on ordering instructions provided by you. While number based ranking systems require updating many -if not all- models, ranking systems such as Sequential Rank only need to update the re-ordered model.

Requirements

  • PHP 8.1 or higher

Installation

Install the library into your project with Composer.

composer require nickbeen/sequential-rank

Usage

The library requires the data to be injected into the constructor as an array. You can provide instructions for ordening with either an array or an enum. While get() returns the sequential ranks, you can also just call orderBy() to get your original array in the correct order.

Order by array

The most common way to provide instructions for ordering is to use an associative array. When working with databases, constructing an array is the most straight-forward choice.

$order = [
    'red' => 1,
    'blue' => 2,
    'yellow' => 3,
];

$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy($order);
$result = $seqRank->get();
$result = [
    '1-2-3', // red, blue, yellow
    '3-2-1', // yellow, blue, red
];

Order by enum

Another way to provide instructions for ordering is to use an enumeration. Enums allows you to bundle both the values and the provided order into one file. Useful when you want your application instead of your database to keep tabs of the desired order.

Your enum must provide an order() method containing the order you want. This implementation ensures your data and order stay decoupled to remove any friction when adding or updating the model.

enum Colors: string {
    case RED = 'red';
    case BLUE = 'blue';
    case YELLOW = 'yellow';
    
    public function order(): int {
        return match ($this) {
            self::RED => 1,
            self::BLUE => 2,
            self::YELLOW => 3,
        }
    }
}
$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy(Colors::class);
$result = $seqRank->get();
$result = [
    '1-2-3', // red, blue, yellow
    '3-2-1', // yellow, blue, red
];

Order without provided order

If no order is provided, the results will be ordered by natural sort. In short this means strings will be ordered in alphabetical order while multi-digit numbers are treated atomically. This is only useful when your values and display order are identical.

$array = [
    ['yellow', 'blue', 'red'],
    ['red', 'blue', 'yellow'],
];

$seqRank = new SequentialRank($array);
$seqRank->orderBy(null)
$result = $seqRank->get();
$result = [
    'red-blue-yellow',
    'yellow-blue-red',
];

Example

We need to document a list of attacks of a video game. Each attack has its own input consisting of a sequence of buttons. To keep things readable, we need to display the list of attacks in a specific order. First, we need to document which buttons could exist in the list of attacks, and while we're at it, decide on a display order for these buttons.

enum Buttons: string {
    case FORWARD = 'forward';
    case DOWN = 'down';
    case BACK = 'back';
    case UP = 'up';
    case X = 'x';
    case Y = 'y';
    case A = 'a';
    case B = 'b';
    
    public function order(): int {
        return match ($this) {
            self::FORWARD => 10,
            self::DOWN => 20,
            self::BACK => 30,
            self::UP => 40,
            self::X => 50,
            self::Y => 60,
            self::A => 70,
            self::B => 80
        }
    }
}

Notice how we increased the order by 10 for each button. This allows us to add new buttons in the future without needing to reorganise and re-order the whole list of attacks. If you expect to frequently add new fields, you should use bigger gaps. Since the whole ranking system is driven by natural sorting, you can also use more complicated values to e.g. categorize values like a-10 or d1-a4.

Anyway, now let's think of a list of attacks containing any of these 8 buttons.

$attacks = [
    ['down', 'forward', 'y'],
    ['forward', 'down', 'y'],
    ['up', 'x'],
    ['x', 'y', 'a'],
    ['forward', 'forward', 'b'],
    ['back', 'a', 'y'],
    ['forward', 'back', 'x'],
    ['back', 'x', 'down', 'y', 'a'],
];

Let's start using Sequential Rank to reorder the list of attacks to be more readable.

$seqRank = new SequentialRank($attacks);
$seqRank->orderBy(Buttons::class);
$result = $seqRank->get();

We're calling get() to get the actual Sequential Ranks returned to us.

$result = [
    '10-10-80', // forward, forward, b
    '10-20-60', // forward, down, y
    '10-30-50', // forward, back, x
    '20-10-60', // down, forward, y
    '30-50-20-60-70', // back, x, down, y ,a
    '30-70-60', // back, a, y
    '40-50', // up, x
    '50-60-70', // x, y, a
];

When working with a database, you can save the Sequential Rank with the attack data and instruct your database to order by Sequential Rank to get what we want.

SELECT id, buttons, sequential_rank
FROM attacks
ORDER BY sequential_rank
id buttons sequential_rank
5 forward, forward, b 10-10-80
2 forward, down, y 10-20-60
7 forward, back, x 10-30-50
1 down, forward, y 20-10-60
8 back, x, down, y ,a 30-50-20-60-70
6 back, a, y 30-70-60
3 up, x 40-50
4 x, y, a 50-60-70

FAQ

What are the pitfalls of Sequential Rank?

A poorly constructed order (e.g. 1,2,3,4) can force you to reorganize your order and recalculate the Sequential Ranks of all your models. One could debate if a string-based field is less performant than an integer based id, but putting an index on the Sequential Rank in your database will definitely help. Lastly, Sequential Rank is not battle-tested.

If you need something more robust, find a package that incorporates Lexorank, the ranking system that drives the drag 'n drop ordering in JIRA. Lexorank offers better tools for easy reordering, but however require you to calculate the exact positions due to its agnostic state.

How should I store Sequential Ranks in my database?

I recommend using a VARCHAR(255) column unless you're absolutely positively able to predict the future changes to the order and structure of your data.

License

This library is licensed under the MIT License (MIT). See the LICENSE for more details.

nickbeen/sequential-rank 适用场景与选型建议

nickbeen/sequential-rank 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 226 次下载、GitHub Stars 达 1, 最近一次更新时间为 2022 年 08 月 12 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 nickbeen/sequential-rank 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-08-12