Switch to self implemented IComparable and IComparator

master 3.2.0
Julien Rosset 2 years ago
parent 593e118ba1
commit 696e4fa458

@ -15,8 +15,7 @@
"minimum-stability": "stable", "minimum-stability": "stable",
"require": { "require": {
"php": "^8.1", "php": "^8.1"
"sgh/comparable": "^1.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

@ -0,0 +1,23 @@
<?php
namespace jrosset\Collections;
/**
* Interface for comparable objects
*
* @template TObject of object
*/
interface IComparable {
/**
* Compare to another instance
*
* @param TObject $other The other instance
*
* @return int <br>&lt; 0 if current instance is classed before the other instance
* <br>= 0 if current instance is classed equals the other instance
* <br>&gt; 0 if current instance is classed after the other instance
*
* @noinspection PhpMissingParamTypeInspection
*/
public function compareTo ($other): int;
}

@ -0,0 +1,24 @@
<?php
namespace jrosset\Collections;
/**
* Interface for classes comparing external objects
*
* @template TObject of object
*/
interface IComparator {
/**
* Compare two objects
*
* @param TObject $object1 The first object
* @param TObject $object2 The second object
*
* @return int <br>&lt; 0 if the first object is classed before the second object
* <br>= 0 if the first object is classed equals the second object
* <br>&gt; 0 if the first object is classed after the second object
*
* @noinspection PhpMissingParamTypeInspection
*/
public function compare ($object1, $object2): int;
}

@ -7,8 +7,6 @@ use Closure;
use Countable; use Countable;
use IteratorAggregate; use IteratorAggregate;
use JsonSerializable; use JsonSerializable;
use SGH\Comparable\Comparable;
use SGH\Comparable\Comparator;
use Throwable; use Throwable;
/** /**
@ -131,30 +129,32 @@ interface IImmutableCollection extends IteratorAggregate, JsonSerializable, Coun
/** /**
* Sort the elements (by value) * Sort the elements (by value)
* *
* @param null|Comparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see Comparable}. * @param null|IComparator|Closure(TValue, TValue):int $sorter The sorting method ; Null if values are object implementing {@see IComparable}.
* <br>Return : * <br>Return :
* <br>&lt; 0 if value1 is before value2 * <br>&lt; 0 if value1 is before value2
* <br>= 0 if value1 equals value2 * <br>= 0 if value1 equals value2
* <br>&gt; 0 if value1 is after value2 * <br>&gt; 0 if value1 is after value2
* *
* @return self The sorted collection * @return static The sorted collection
* @psalm-return static<TKey, TValue>
* *
* @throws Throwable If an error occurs * @throws Throwable If an error occurs
*/ */
public function sort (Closure|Comparator|null $sorter = null): self; public function sort (Closure|IComparator|null $sorter = null): self;
/** /**
* Sort the elements by key * Sort the elements by key
* *
* @param Comparator|Closure(TKey, TKey):int $sorter The sorting method. Return : * @param IComparator|Closure(TKey, TKey):int $sorter The sorting method. Return :
* <br>&lt; 0 if key1 is before key2 * <br>&lt; 0 if key1 is before key2
* <br>= 0 if key1 equals key2 * <br>= 0 if key1 equals key2
* <br>&gt; 0 if key1 is after key2 * <br>&gt; 0 if key1 is after key2
* *
* @return self The sorted collection * @return static The sorted collection
* @psalm-return static<TKey, TValue>
* *
* @throws Throwable If an error occurs * @throws Throwable If an error occurs
*/ */
public function sortByKey (Closure|Comparator $sorter): self; public function sortByKey (Closure|IComparator $sorter): self;
/** /**
* The list of all keys * The list of all keys

@ -5,8 +5,6 @@ namespace jrosset\Collections;
use ArrayIterator; use ArrayIterator;
use Closure; use Closure;
use InvalidArgumentException; use InvalidArgumentException;
use SGH\Comparable\Comparable;
use SGH\Comparable\Comparator;
use Traversable; use Traversable;
/** /**
@ -305,13 +303,13 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sort (Closure|Comparator|null $sorter = null): static { public function sort (Closure|IComparator|null $sorter = null): static {
if ($sorter === null) { if ($sorter === null) {
$sorter = function (Comparable $value1, Comparable $value2): int { $sorter = function (IComparable $value1, IComparable $value2): int {
return $value1->compareTo($value2); return $value1->compareTo($value2);
}; };
} }
elseif ($sorter instanceof Comparator) { elseif ($sorter instanceof IComparator) {
$sorter = function ($value1, $value2) use ($sorter): int { $sorter = function ($value1, $value2) use ($sorter): int {
return $sorter->compare($value1, $value2); return $sorter->compare($value1, $value2);
}; };
@ -325,8 +323,8 @@ class ImmutableCollection implements IImmutableCollection {
/** /**
* @inheritDoc * @inheritDoc
*/ */
public function sortByKey (Closure|Comparator $sorter): static { public function sortByKey (Closure|IComparator $sorter): static {
if ($sorter instanceof Comparator) { if ($sorter instanceof IComparator) {
$sorter = function ($value1, $value2) use ($sorter): int { $sorter = function ($value1, $value2) use ($sorter): int {
return $sorter->compare($value1, $value2); return $sorter->compare($value1, $value2);
}; };

@ -1,11 +1,19 @@
<?php <?php
/** @noinspection PhpUnhandledExceptionInspection */
/** @noinspection PhpIllegalPsrClassPathInspection */ /** @noinspection PhpIllegalPsrClassPathInspection */
use jrosset\Collections\Collection; use jrosset\Collections\Collection;
use jrosset\Collections\IComparable;
use jrosset\Collections\IComparator;
require_once __DIR__ . '/../vendor/autoload.php'; require_once __DIR__ . '/../vendor/autoload.php';
class Measure { /**
* Class Measure
*
* @implements IComparable<static>
*/
class Measure implements IComparable {
private int $value; private int $value;
private string $unit; private string $unit;
@ -14,9 +22,33 @@ class Measure {
$this->unit = $unit; $this->unit = $unit;
} }
public function getValue (): int {
return $this->value;
}
public function asString (): string { public function asString (): string {
return $this->value . $this->unit; return $this->value . $this->unit;
} }
/**
* @inheritDoc
*/
public function compareTo ($other): int {
return $this->value <=> $other->value;
}
}
/**
* Class MeasureReverseComparator
*
* @implements IComparator<Measure>
*/
class MeasureReverseComparator implements IComparator {
/**
* @inheritDoc
*/
public function compare ($object1, $object2): int {
return -($object1->getValue() <=> $object2->getValue());
}
} }
/** /**
@ -34,6 +66,11 @@ function getMeasures (): Collection {
} }
echo 'Measures :' . PHP_EOL; echo 'Measures :' . PHP_EOL;
foreach (getMeasures() as $no => $measure) { foreach (getMeasures()->sort() as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
}
echo 'Measures (reversed) :' . PHP_EOL;
foreach (getMeasures()->sort(new MeasureReverseComparator()) as $no => $measure) {
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL; echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
} }
Loading…
Cancel
Save