jimbo2150/php-comparable
Composer 安装命令:
composer require jimbo2150/php-comparable
包简介
PHP library to allow for comparison of two objects with a comparison operator.
README 文档
README
PHP Comparable Objects Trait & Custom Comparison Convenience Method
A PHP library to allow for comparison of two objects with a comparison operator.
This library allows you to compare two objects using a value within the objects rather than the entirety of the object (the default for PHP).
Installation
This library is available on Packagist using Composer. Run this command within your composer project directory:
$ composer require jimbo2150/php-comparable
Usage (private value with convenience trait)
The comparable trait (ComparableTrait) requires a protected method, getComparableValue(), which does not publically expose the value to be compared. The convenience methods then use reflection to access the other object's value:
use Jimbo2150\PhpComparable\Trait\ComparableTrait; // This class will compare the Person object's $age property. class Person { use ComparableTrait; public function __construct(private string $name, private int $age) { assert($age > 0); } public function getComparableValue(): mixed { return $this->age; } } $john = new Person('John', 29); $karen = new Person('Karen', 24); $john->compareTo($karen); // False, compares by equals (==) by default $karen->compareTo($john, Operator::from('<')); // True, comparing with less than
Usage (private value with custom comparison)
You can also implement your own custom compare method by creating your own comparison method that utilizes the customCompareTo(callable $callback, ComparableTrait $compareTo, Operator $operator) convenience method and provide a callback (callable or Closure) that, when called, is passed the current object's comparison value, the comparison value of the object your are comparing to, and the operator as parameter values:
use Jimbo2150\PhpComparable\Trait\ComparableTrait; class Score { use ComparableTrait; public const MIN_REQUIRED_SCORE = 1; public function __construct(private float $score) { } public function compareDiff(object $other): bool|int { $operator = Operator::GREATER_THAN_OR_EQUAL; $diffFunction = fn ( mixed $leftValue, mixed $rightValue, Operator $operator, ): bool|int => $operator->compare( $leftValue - $rightValue, self::MIN_REQUIRED_SCORE ); return $this->customCompareTo( $diffFunction, $other, $operator ); } public function getComparableValue(): mixed { return $this->score; } } $score1 = new Score(2.4); $score2 = new Score(0.7); $score3 = new Score(0.2); $score1->compareDiff($score2); // True, score is >= 1 $score2->compareDiff($score3); // False, score is less than 1
jimbo2150/php-comparable 适用场景与选型建议
jimbo2150/php-comparable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 8 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 02 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 jimbo2150/php-comparable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 jimbo2150/php-comparable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 8
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2025-02-17