v1.0.0 2022-05-30 20:28 UTC

This package is not auto-updated.

Last update: 2024-11-19 19:55:20 UTC


README

Code Coverage Badge

This component is simple implementation of immutable ordered sequence of elements with strict types. Which means that once tuple created it can't be immuted. Tuple also provide elements of the same type.

Installation

composer require rocketfellows/tuple

Usage example

Typed item class:

class TupleItem
{
    private $id;

    public function __construct(int $id)
    {
        $this->id = $id;
    }

    public function getId(): int
    {
        return $this->id;
    }
}

Items tuple implementation:

use rocketfellows\tuple\Tuple;

class Items extends Tuple
{
    public function __construct(TupleItem ...$items)
    {
        parent::__construct(...$items);
    }

    public function current(): ?TupleItem
    {
        return parent::current();
    }
}

Load tuple and loop through:

$firstTupleItem = new TupleItem(1);
$secondTupleItem = new TupleItem(2);
$thirdTupleItem = new TupleItem(3);

$items = new Items($firstTupleItem, $secondTupleItem, $thirdTupleItem);

foreach ($items as $item) {
    print_r($item->getId());
}

Result:

1
2
3

Contributing

Welcome to pull requests. If there is a major changes, first please open an issue for discussion.

Please make sure to update tests as appropriate.