1. Go to this page and download the library: Download r34117y/sorted-linked-list library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
r34117y / sorted-linked-list example snippets
use Agrzelec\SortedLinkedList\SortedLinkedListFactory;
use Agrzelec\SortedLinkedList\Exception\InvalidValueTypeException;
use Agrzelec\SortedLinkedList\SortedLinkedListFactory;
use Agrzelec\SortedLinkedList\Strategy\SortedListStrategy;
/**
* @implements SortedListStrategy<DateTimeImmutable>
*/
final class DateTimeImmutableStrategy implements SortedListStrategy
{
public function normalize(mixed $value): DateTimeImmutable
{
if (!$value instanceof DateTimeImmutable) {
throw new InvalidValueTypeException(sprintf(
'Expected value of type %s, got %s.',
DateTimeImmutable::class,
get_debug_type($value),
));
}
return $value;
}
public function compare(mixed $left, mixed $right): int
{
$left = $this->normalize($left);
$right = $this->normalize($right);
return $left->getTimestamp() <=> $right->getTimestamp();
}
public static function getValueType(): string
{
return DateTimeImmutable::class;
}
}
$list = SortedLinkedListFactory::usingStrategy(
new DateTimeImmutableStrategy(),
[
new DateTimeImmutable('2024-12-01'),
new DateTimeImmutable('2024-01-01'),
],
);
$list->insert(new DateTimeImmutable('2024-06-01'));
$list->toArray(); // 2024-01-01, 2024-06-01, 2024-12-01
use Agrzelec\SortedLinkedList\Exception\InvalidValueTypeException;
use Agrzelec\SortedLinkedList\SortedLinkedListFactory;
$list = SortedLinkedListFactory::forIntegers([1, 2, 3]);
try {
$list->insert('4');
} catch (InvalidValueTypeException $exception) {
// The list still contains [1, 2, 3].
}