phrity/comparison
Composer 安装命令:
composer require phrity/comparison
包简介
Interfaces and helper trait for comparing objects. Comparator for sort and filter applications.
README 文档
README
Comparison
Interfaces and helper trait for comparing objects. Comparator utility class for sort and filter applications.
Current version supports PHP ^7.1|^8.0.
Installation
Install with Composer;
composer require phrity/comparison
The Equalable interface
Interface synopsis
interface Phrity\Comparison\Equalable { /* Abstract methods */ abstract public equals(mixed $compare_with) : bool }
Examples
// $a must implement Equalable, $b can be anything $a->equals($b); // True if $a is equal to $b
The Comparable interface
Extends Equalable interface.
Interface synopsis
interface Phrity\Comparison\Comparable extends Phrity\Comparison\Equalable { /* Abstract methods */ abstract public greaterThan(mixed $compare_with) : bool abstract public greaterThanOrEqual(mixed $compare_with) : bool abstract public lessThan(mixed $compare_with) : bool abstract public lessThanOrEqual(mixed $compare_with) : bool abstract public compare(mixed $compare_with) : int /* Inherited from Equalable */ abstract public equals(mixed $compare_with) : bool }
Examples
// $a must implement Comparable, $b can be anything $a->greaterThan($b); // True if $a is greater than $b $a->greaterThanOrEqual($b); // True if $a is greater than or equal to $b $a->lessThan($b); // True if $a is less than $b $a->lessThanOrEqual($b); // True if $a is less than or equal to $b // 0 if $a is equal to $b // -1 if $a is less than $b // 1 if $a is greater than $b $a->compare($b);
The ComparisonTrait trait
A class using this trait only has to implement the compare() method. Enables all other methods in Equalable and Comparable intefaces.
Trait synopsis
trait Phrity\Comparison\ComparisonTrait implements Phrity\Comparison\Comparable { /* Methods */ public equals(mixed $compare_with) : bool public greaterThan(mixed $compare_with) : bool public greaterThanOrEqual(mixed $compare_with) : bool public lessThan(mixed $compare_with) : bool public lessThanOrEqual(mixed $compare_with) : bool /* Inherited from Comparable */ abstract public compare(mixed $compare_with) : int }
The Comparator class
Utility class to sort and filter array objects that implement the Comparable interface.
Class synopsis
class Phrity\Comparison\Comparator { /* Methods */ public __construct(array $comparables = null) public sort(array $comparables = null) : array public rsort(array $comparables = null) : array public equals(Comparable $condition, array $comparables = null) : array public greaterThan(Comparable $condition, array $comparables = null) : array public greaterThanOrEqual(Comparable $condition, array $comparables = null) : array public lessThan(Comparable $condition, array $comparables = null) : array public lessThanOrEqual(Comparable $condition, array $comparables = null) : array public min(array $comparables = null) : Comparable public max(array $comparables = null) : Comparable }
Examples
$comparables = [$v2, $v1, $v4, $v3]; $condition = $v3; $comparator = new Comparator(); // Sort and reverse sort array of Comparable $comparator->sort($comparables); // [$v1, $v2, $v3, $v4] $comparator->rsort($comparables); // [$v4, $v3, $v2, $v1] // Filter array of Comparable using a Comparable as condition $comparator->equals($condition, $comparables); // [$v3] $comparator->greaterThan($condition, $comparables); // [$v4] $comparator->greaterThanOrEqual($condition, $comparables); // [$v4, $v3] $comparator->lessThan($condition, $comparables); // [$v2, $v1] $comparator->lessThanOrEqual($condition, $comparables); // [$v2, $v1, $v3] // Select min/max instance $comparator->min($comparables); // $v1 $comparator->max($comparables); // $v4 // Can also "store" comparables for re-use in multiple operations $comparables = [$v2, $v1, $v4, $v3]; $comparator = new Comparator($comparables); $comparator->sort(); // [$v1, $v2, $v3, $v4] $comparator->equals($condition); // [$v3] $comparator->min(); // $v1
The IncomparableException class
Must be thrown if comparison methods receive input they can not compare with.
Class synopsis
class Phrity\Comparison\IncomparableException extends InvalidArgumentException { /* Inherited from InvalidArgumentException */ public __construct([string $message = '' [, int $code = 0 [, Throwable $previous = null]]]) public getMessage() : string public getPrevious() : Throwable public getCode() : mixed public getFile() : string public getLine() : int public getTrace() : array public getTraceAsString() : string public __toString() : string }
Versions
| Version | PHP | |
|---|---|---|
1.4 |
^8.1 |
|
1.3 |
^7.1|^8.0 |
|
1.2 |
^7.1 |
The Comparator supports stored content for multiple operations |
1.1 |
>=5.6 |
The Comparator class for sort and filter |
1.0 |
>=5.6 |
Equalable and Comparable interface, ComparisonTrait trait |
phrity/comparison 适用场景与选型建议
phrity/comparison 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 279.19k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 12 月 22 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「filter」 「sort」 「comparison」 「comparable」 「comparator」 「equalable」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phrity/comparison 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phrity/comparison 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phrity/comparison 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Interfaces and functions for type-safe, consistent value comparison
DataTables is a plug-in for the jQuery Javascript library. It is a highly flexible tool, based upon the foundations of progressive enhancement, which will add advanced interaction controls to any HTML table.
Syntactic sugar for PHP's sorting
Easily provide front-end sorting controls for SilverStripe lists
Plug-ins for DataTables
Nova searchable filter for belongsTo relationships.
统计信息
- 总下载量: 279.19k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 28
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-12-22
