You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
1.8 KiB
PHP
79 lines
1.8 KiB
PHP
<?php
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
/** @noinspection PhpIllegalPsrClassPathInspection */
|
|
|
|
use jrosset\Collections\Collection;
|
|
use jrosset\Collections\IComparable;
|
|
use jrosset\Collections\IComparator;
|
|
|
|
require_once __DIR__ . '/../vendor/autoload.php';
|
|
|
|
/**
|
|
* Class Measure
|
|
*
|
|
* @implements IComparable<static>
|
|
*/
|
|
class Measure implements IComparable {
|
|
private int $value;
|
|
private string $unit;
|
|
|
|
public function __construct (int $value, string $unit = 's') {
|
|
$this->value = $value;
|
|
$this->unit = $unit;
|
|
}
|
|
|
|
public function getValue (): int {
|
|
return $this->value;
|
|
}
|
|
|
|
public function asString (): string {
|
|
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());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return Collection<int, Measure>
|
|
*/
|
|
function getMeasures (): Collection {
|
|
$mesures_avant = new Collection();
|
|
$mesures_avant->add(new Measure(38), new Measure(29));
|
|
|
|
$measures = new Collection();
|
|
$measures->add(new Measure(27), new Measure(34));
|
|
$measures->prependCollection($mesures_avant);
|
|
|
|
return $measures;
|
|
}
|
|
|
|
$measures = getMeasures();
|
|
$measures->sortSelf();
|
|
|
|
echo 'Measures :' . PHP_EOL;
|
|
foreach ($measures as $no => $measure) {
|
|
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
|
|
}
|
|
|
|
echo 'Measures (reversed) :' . PHP_EOL;
|
|
foreach ($measures->sort(new MeasureReverseComparator()) as $no => $measure) {
|
|
echo "\t#" . $no . ' = ' . $measure->asString() . PHP_EOL;
|
|
} |