phrity / comparison
比较对象的接口和辅助特性。用于排序和过滤应用的比较器。
1.3.0
2020-12-16 21:35 UTC
Requires
- php: ^7.1|^8.0
Requires (Dev)
- php-coveralls/php-coveralls: ^2.0
- phpunit/phpunit: ^7.0|^8.0|^9.0
- squizlabs/php_codesniffer: ^3.5
README
比较
比较对象的接口和辅助特性。排序和过滤应用的比较器工具类。
当前版本支持 PHP ^7.1|^8.0
。
安装
使用 Composer 安装;
composer require phrity/comparison
The Equalable 接口
接口摘要
interface Phrity\Comparison\Equalable { /* Abstract methods */ abstract public equals(mixed $compare_with) : bool }
示例
// $a must implement Equalable, $b can be anything $a->equals($b); // True if $a is equal to $b
The Comparable 接口
扩展 Equalable
接口。
接口摘要
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 }
示例
// $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 特性
使用此特性的类只需实现 compare()
方法。启用 Equalable
和 Comparable
接口中的所有其他方法。
特性摘要
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 类
排序和过滤实现 Comparable
接口的数组对象的实用工具类。
类摘要
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 }
示例
$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 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 }