https://github.com/ensostudio/comparator
PHP package to comparing values
https://github.com/ensostudio/comparator
comparator compare diff php
Last synced: 3 months ago
JSON representation
PHP package to comparing values
- Host: GitHub
- URL: https://github.com/ensostudio/comparator
- Owner: ensostudio
- License: bsd-3-clause
- Created: 2022-03-31T18:52:37.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2022-08-27T13:08:50.000Z (over 3 years ago)
- Last Synced: 2025-05-03T09:03:56.105Z (8 months ago)
- Topics: comparator, compare, diff, php
- Language: PHP
- Homepage:
- Size: 17.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
The flexible comparation of
- index/assoc arrays
- objects/closures
- floats/NANs
- binary/text strings
- stream resources
### Installation
Via [Composer](https://getcomposer.org):
~~~bash
composer require ensostudio/comparator
~~~
### Usage
~~~php
use EnsoStudio\Comparator\Comparator;
$comparator = Comparator(Comparator::EQUAL_ARRAY | Comparator::EQUAL_FLOAT);
if ($comparator->compare($value, $value2)) {
echo 'same values';
}
~~~
~~~php
$comparator->setFlags(Comparator::EQUAL_FLOAT);
var_dump(3 - 2.4 == 0.6, $comparator->compare(3 - 2.4, 0.6));
// false, true
$comparator->setFlags(Comparator::EQUAL_STRING);
var_dump('foo' == 'FOO', $comparator->compare('foo', 'FOO'));
// false, true
// Case-issensetive comparation supports only for English:
var_dump($comparator->compare('я', 'Я'));
// false
$comparator->setFlags(Comparator::EQUAL_CLOSURE);
$createClosure = function () {
return function ($value) {
return $value * 2;
};
};
var_dump($createClosure() == $createClosure(), $comparator->compare($createClosure(), $createClosure()));
// false, true
$comparator->setFlags(Comparator::EQUAL_ARRAY | Comparator::EQUAL_FLOAT);
var_dump($comparator->compare(
['float' => 2 - 1.6, 'int' => 3],
['int' => 3, 'float' => 0.4]
));
// true
~~~
### Public API
~~~php
namespace EnsoStudio\Comparator;
class Comparator
{
public function __construct(int $flags);
public function setFlags(int $flags);
public function getFlags(): int;
public function hasFlag(int $flag): bool;
public function getType(mixed $value): string;
public function canCompare(string $type, string $type2): bool;
public function compare(mixed $value, mixed $value): bool;
}
~~~