letsgoi/custom-collection

This package is abandoned and no longer maintained. The author suggests using the letsgoi/custom-list package instead.

PHP Custom List package

2.3.0 2024-01-12 06:58 UTC

README

Class to wrap array of items to force same type. This is for avoid the php array type hinting problem.

Requirements

  • PHP >= 8.2

Usage

Extends the CustomList abstract class and set the type of the items with getListType method:

use Letsgoi\CustomList\CustomList;

class ItemList extends CustomList
{
    protected function getListType(): string
    {
        return Item::class;  
    }
}

//

$items = [new Item(), new Item(), ...];
$list = new ItemList($items);

To iterate items:

foreach ($list as $item) {
    //
}

You can use the list as an array (set, get, ...)

Available methods

get($key = null)

Return item by key or all list without it:

$list->get(0); // 'item'

$list->get(); // ['item', 'item', ...]

add($item)

Append item to list:

$list->add($item);

merge(... $customLists)

Merges the elements of one or more custom lists together

$list->merge($list1, $list2 ...);

Testing

Run tests:

composer test

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT