fleshgrinder/comparable 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

fleshgrinder/comparable

最新稳定版本:v0.1.0

Composer 安装命令:

composer require fleshgrinder/comparable

包简介

Interface for custom compare checks of objects.

README 文档

README

Packagist Packagist License

Comparable

Interface to implement custom comparison logic for classes instead of writing them inline over and over again.

Provided is the interface itself that establishes the contract that implementing classes have to have the compare method. Furthermore a specific exception is provided that can be used to notify callers that a comparison of the passed value is not possible. Last but not least a dummy class that can be used in tests as a substitute for doubles, stubs, or mocks is included as well.

Installation

Open a terminal, enter your project directory and execute the following command to add this package to your dependencies:

$ composer require fleshgrinder/comparable

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Usage

Simply implement the interface and the required compareTo method.

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;
    }

}

License

MIT License

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-23