肉磨机 / 可比较
该软件包已被废弃,不再维护。未建议替代软件包。
对象自定义比较检查的接口。
v0.1.0
2015-09-27 18:37 UTC
This package is not auto-updated.
Last update: 2020-01-16 23:26:01 UTC
README
Comparable
实现类自定义比较逻辑的接口,而不是反复编写内联代码。
提供接口本身,建立实现类必须拥有的比较方法契约。此外,还提供了一个特定的异常,可以用来通知调用者无法比较传入的值。最后但同样重要的是,还包含了一个虚拟类,可以在测试中使用,作为双倍、存根或模拟的替代品。
安装
打开终端,进入您的项目目录,并执行以下命令以将此软件包添加到依赖项
$ composer require fleshgrinder/comparable
此命令要求您全局安装了Composer,如Composer文档中的安装章节所述。
使用
只需实现接口和必需的compareTo
方法。
class YourClass implements Comparable { const TOLERANCE = 0.0001; protected $value; public function __construct($value) { $this->value = (float) $value; } /** * @inheritDoc */ public function compareTo($other) { if (($other instanceof $this) === false) { throw new UncomparableException(); } $diff = $other->value() - $this->value; if ($diff > static::TOLERANCE) { return 1; } if ($diff < -static::TOLERANCE) { return -1; } return 0; } }