scheb / comparator
Compares to values for equality
Requires
- php: ^7.1.3
Requires (Dev)
- phpunit/phpunit: ^7.2
This package is auto-updated.
Last update: 2022-01-11 13:25:31 UTC
README
This library compares two values for equality.
If you need to add your own rules for comparing specific values, you can extend the library with your own comparison strategies.
Features
- Simple equality comparison (
==
or===
) - Strategy interface for your own comparison rules
Installation
composer require scheb/comparator
How to use
$comparator = new \Scheb\Comparator\Comparator(true); // Type-sensive equal (===) $comparator->isEqual(0, "0"); // Returns false $comparator->isEqual(0, ""); // Returns false $comparator->isEqual(0, 0); // Returns true $comparator->isEqual(0, "foo"); // Returns false $comparator = new \Scheb\Comparator\Comparator(false); // Type-insensive equal (==) $comparator->isEqual(0, "0"); // Returns true $comparator->isEqual(0, ""); // Returns true $comparator->isEqual(0, 0); // Returns true $comparator->isEqual(0, "foo"); // Returns false
How to extend
To add your own comparison strategy, implement Scheb\Comparator\ValueComparisonStrategyInterface
.
Then, add an instance of that class via the constructor argument $customComparisonStrategies
of
Scheb\Comparator\Comparator
.
Custom comparison strategies take preference over default ones.
Contribute
You're welcome to contribute to this library by creating a pull requests or feature request in the issues section. For pull requests, please follow these guidelines:
- Symfony code style
- PHP7.1 type hints for everything (including: return types,
void
, nullable types) - Please add/update test cases
- Test methods should be named
[method]_[scenario]_[expected result]
To run the test suite install the dependencies with composer install
and then execute bin/phpunit
.
License
This bundle is available under the MIT license.