2017 © Pedro Peláez
 

library comparable

Type-safe comparison for PHP.

image

fleshgrinder/comparable

Type-safe comparison for PHP.

  • Monday, March 27, 2017
  • by Fleshgrinder
  • Repository
  • 1 Watchers
  • 0 Stars
  • 32 Installations
  • PHP
  • 1 Dependents
  • 2 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 0 % Grown

The README.md

Packagist Packagist License, (*1)

Comparable

Interface to implement custom comparison logic for classes instead of writing them inline over and over again., (*2)

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., (*3)

Installation

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

$ composer require fleshgrinder/comparable

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

Usage

Simply implement the interface and the required compareTo method., (*6)

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, (*7)

The Versions

27/03 2017

dev-develop

dev-develop

Type-safe comparison for PHP.

  Sources   Download

Unlicense

The Requires

 

The Development Requires

27/09 2015

dev-master

9999999-dev

Interface for custom compare checks of objects.

  Sources   Download

MIT

27/09 2015

v0.1.0

0.1.0.0

Interface for custom compare checks of objects.

  Sources   Download

MIT

23/09 2015

v0.0.0

0.0.0.0

Interface for custom compare checks of objects.

  Sources   Download

MIT