1. Go to this page and download the library: Download seboettg/collection 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/ */
seboettg / collection example snippets
use function Seboettg\Collection\Lists\listOf;
use function Seboettg\Collection\Lists\listFromArray;
//create a simple list
$list = listOf("a", "b", "c", "d");
print_r($list);
namespace Vendor\App\Model;
use Seboettg\Collection\Comparable\Comparable;
class Element implements Comparable
{
private $attribute1;
private $attribute2;
//contructor
public function __construct($attribute1, $attribute2)
{
$this->attribute1 = $attribute1;
$this->attribute2 = $attribute2;
}
// getter
public function getAttribute1() { return $this->attribute1; }
public function getAttribute2() { return $this->attribute2; }
//compareTo function
public function compareTo(Comparable $b): int
{
return strcmp($this->attribute1, $b->getAttribute1());
}
}
namespace Vendor\App\Util;
use Seboettg\Collection\Comparable\Comparator;
use Seboettg\Collection\Comparable\Comparable;
class Attribute1Comparator extends Comparator
{
public function compare(Comparable $a, Comparable $b): int
{
if ($this->sortingOrder === Comparator::ORDER_ASC) {
return $a->compareTo($b);
}
return $b->compareTo($a);
}
}
use Seboettg\Collection\Lists;
use Seboettg\Collection\Collections;
use Seboettg\Collection\Comparable\Comparator;
use function Seboettg\Collection\Lists\listOf;
use Vendor\App\Util\Attribute1Comparator;
use Vendor\App\Model\Element;
$list = listOf(
new Element("b","bar"),
new Element("a","foo"),
new Element("c","foobar")
);
Collections::sort($list, new Attribute1Comparator(Comparator::ORDER_ASC));
use Seboettg\Collection\Comparable\Comparator;
use Seboettg\Collection\Comparable\Comparable;
use Seboettg\Collection\Lists;
use Seboettg\Collection\Collections;
use function Seboettg\Collection\Lists\listOf;
use Vendor\App\Model\Element;
//Define a custom Comparator
class MyCustomOrderComparator extends Comparator
{
public function compare(Comparable $a, Comparable $b): int
{
return (array_search($a->getAttribute1(), $this->customOrder) >= array_search($b->getAttribute1(), $this->customOrder)) ? 1 : -1;
}
}
$list = listOf(
new Element("a", "aa"),
new Element("b", "bb"),
new Element("c", "cc"),
new Element("k", "kk"),
new Element("d", "dd"),
);
Collections::sort(
$list, new MyCustomOrderComparator(Comparator::ORDER_CUSTOM, ["d", "k", "a", "b", "c"])
);
use Seboettg\Collection\Map\Pair;
use function Seboettg\Collection\Map\pair;
use function Seboettg\Collection\Map\mapOf;
$pair1 = pair("Ceres", "Giuseppe Piazzi")
//or you use the factory, with the same result:
$pair2 = Pair::factory("Pallas", "Heinrich Wilhelm Olbers");
//Now you can add both pairs to a map
$map = mapOf($pair1, $pair2);
print_r($map);
use function Seboettg\Collection\Map\emptyMap;
$emptyMap = emptyMap();
echo $emptyMap->count();
use function Seboettg\Collection\Map\mapOf;
$asteroidExplorerMap = mapOf(
pair("Ceres", "Giuseppe Piazzi"),
pair("Pallas", "Heinrich Wilhelm Olbers"),
pair("Juno", "Karl Ludwig Harding"),
pair("Vesta", "Heinrich Wilhelm Olbers")
);
$juno = $asteroidExplorerMap->get("Juno"); //Karl Ludwig Harding
// or access elements like an array
$pallas = $asteroidExplorerMap["Pallas"]; //Heinrich Wilhelm Olbers
//get a list of all keys
$asteroids = $asteroidExplorerMap->getKeys(); //Ceres, Pallas, Juno, Vesta
//get a list of all values
$explorer = $asteroidExplorerMap
->values()
->distinct(); // "Giuseppe Piazzi", "Heinrich Wilhelm Olbers", "Karl Ludwig Harding"
$explorer = $asteroidExplorerMap
->getOrElse("Iris", fn() => "unknown"); //$explorer = "unknown"
use function Seboettg\Collection\Map\mapOf;
class Asteroid {
public string $name;
public ?string $explorer;
public ?float $diameter;
public function __construct(string $name, string $explorer, float $diameter = null)
{
$this->name = $name;
$this->explorer = $explorer;
$this->diameter = $diameter;
}
}
$asteroids = $asteroidExplorerMap
->map(fn (Pair $pair): Asteroid => new Asteroid($pair->getKey(), $pair->getValue()));
print_r($asteroids);
$asteroids = $asteroidExplorerMap
->map(fn (string $key, string $value): Asteroid => new Asteroid($key, $value));
$stack = new Stack();
$stack->push("a")
->push("b")
->push("c");
echo $stack->pop(); // outputs c
echo $stack->count(); // outputs 2
// peek returns the element at the top of this stack without removing it from the stack.
echo $stack->peek(); // outputs b
echo $stack->count(); // outputs 2
echo $stack->search("c"); //outputs 0 since c does not exist anymore
echo $stack->search("a"); //outputs 2
echo $stack->search("b"); //outputs 1
$queue = new Queue();
$queue->enqueue("d")
->enqueue("z")
->enqueue("b")
->enqueue("a");
echo $queue->dequeue(); // outputs d
echo $queue->dequeue(); // outputs z
echo $queue->dequeue(); // outputs b
echo $queue->count(); // outputs 1